Multidimensional array in c

Multidimensional array in c

Multidimensional array in c programming

We give a overview of 3-d arrays. so we can think of a three-d array as an array of 2-D arrays let us take example if we have an array- int arr[2][4][4];
we can think this array consist of two (2-D) array where each of those 2-D array has 4 rows and 4 columns

Italian Trulli

The individual elements are-
arr[0][0][0], arr[0][0][1], arr[0][0][3], arr[0][1][0]……….arr[0][3][3] arr[1][0][0], arr[1][0][1], arr[1][0][3], arr[1][1][0]……….arr[1][3][3] The total elements in the above 3-D array are
=2*4*4 =32

Lets take example,This array can be Initialize as

int arr[2][4][4]={
{1,5,6,7} /* matrix 0 and row 0 */
{4,3,2,6} /* matrix 0 and row 1 */
{2,3,4,5} /* matrix 0 and row 2 */
{0,2,3,5} /* matrix 0 and row 3 */
},
{
{0,9,8,7} /* matrix 1 and row 0 */
{1,2,3,4} /* matrix 1 and row 1 */
{23,44,5,6} /* matrix 1 and row 2 */
{3,2,7,8} /* matrix 1 and row 3 */
};

Lets take example,This array can be Initialize as

// C program to find the sum of two matrices of order 2*2
#include<stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
// Taking input using nested for loop
printf("Enter elements of 1st matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter The Matrix a %d%d: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
// get input using nested for loop
printf("Enter the elements of 2nd matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter the matrix b %d%d: ", i + 1, j + 1);
scanf("%f", &b[i][j]);
}
// adding the corresponding elements of both arrays A and B
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
// Displaying the sum of Both Array
printf("\nSum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("%.1f\t", c[i][j]);
if (j == 1)
printf("\n");
}
return 0;
}