Multi Dimensional Array in C.
Hi Everyone,
Today we will discuss about Multi Dimensional Array in C. In Previous blog we discussed about Array so before going through Multi Dimensional Array please go through my below blog first about one dimensional Array so that it’s easy for you to understand 2D array and 3D arrays.
https://dheeraj60.blogspot.com/2020/06/array-and-its-beneifts-in-c-program.html
Let’s Discuss about Multi Dimensional Array now
Creating Array of an Arrays in C is known as Multi-Dimensional Array .Multidimensional Array is of 2D array and 3D array . Two Dimensional (2D) array also known as Matrix . Matrix can be represented as table with rows and columns.
Let’s understand the syntax of 2D array .
Int x[3][4];
In Previous blog we already know about 1D array and it’s declaration here in 2D array we declare the array in same way with another [] assuming one for rows and another for column .
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. An array as a table with 3 rows and each row has 4 columns so 3*4=13 so we ca say this array can hold 12 elements.
Here rows is 3 and 4 is column assume this in table.
Similarly we can declare three dimensional (3D) array as below .
int i[2][4][3];
Here array I can hold 24 elements 2*4 *3=24;
Let’s understand 2D array with one Program where we use 2 rows and 3 columns and takes the input from user and display the elements of array and the value which looks similar to tables with rows and columns.
Below is the 2D program with rows and columns.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int tab[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Please enter value for tab[%d][%d]:", i, j);
scanf("%d", &tab[i][j]);
}
}
printf("2D array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", tab[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
Here Output will be with 2 rows and 3 column value and user needs to give input 6 times as in array we declare 2 for rows and 3 for columns.
In above Program .
1. We declare 2D array as int tab[2][3]; where 2 is rows and 3 is columns.
2. Counter variables for the loop to take input from the users.
3. And in last we just display the array elements to see the matrix.
Below is the output of above program in compiler code- block for better understanding.
Here in output we can see user input 6 values with rows and columns and table shows 2 rows and 3 columns. You can change row and column with different value as per your requirement.
Note we can declare above array as below also
Tab[2][3]={10,12,13} , {12,13,14} as hard coded value if you want to use this value in program as here also two value passed that is row and in row we give 3 values as column.
Let’s understand with one more programs where we can display marks in array. So suppose we declare array, mark[3][3]; so total elements hold in array mark is 3*3=9 . Program is similar to above but here we use nested for loop to display the elements and take the value from user.
Below is the Program we will explain the logic after this.
int main()
{
int mark[3][3];
int i, j;
for(i=0; i<3; i++) {
for(j=0;j<3;j++) {
printf(" \n Please enter value for mark[%d][%d]:", i, j);
scanf("%d", &mark[i][j]);
}
}
printf("\n Marks values in Matrix are: \n\n");
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
printf("%d\n" , mark[i][j]);
}
printf("\n");
}
printf("\n\n");
return 0;
}
Here we declare 2D array for mark[3][3] ; that will hold 9 elements
After that we used Counter variables for the loop and we use for nested loop where inner loop display the value of marks passed by user and outer loop used to print space between the marks .
Below is the output in compiler Code Block for your understanding.
In above program for 2D Array we simply use nested for loop where inner loop displays the element value of array as outer loop used to display the space or next lineHope you are able to understand how this program take input of 2D array and display the elements by using Nested for loop. You can write several programs using 2D to store the students marks and display it by using loops and other program as well like sum of two matrixes etc.
Let’s understand one 3D matrix where we simply use 3 variable to store and display the elements of array.
Below is the Program for 3D.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int threedi[2][3][2];
printf(" Please enter the 12 values: \n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &threedi[i][j][k]);
}
}
}
printf("\n Printing the values:\n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, threedi[i][j][k]);
}
}
}
return 0;
}
Let’s understand this 3D program here we initialize array threedi[2][3][2]; it means threedi can hold 12 elements as 2*3*2 =12 so we have taken 3 variable I , j , k and by using loop we store 12 value in array passed by user and simply displaying all the array elements by using loop and printf command to display the 3D matrix.
See the below output of above program in code compiler for your understanding.
Please see this blog carefully and write as much 2D and 3D program as you can as per your requirement and let me know if you have any doubt on Array programs. Hope you are able to understand the benefits of using Array in the programs . Later we use array in pointer and functions program so please practice array program . In next blog I will come up with String uses in C and practice the program by using loops and Array.
Thanks
Comments
Post a Comment