A multidimensional array in simple terms is an array of arrays. Like we have one index in a one-dimensional array, two indices in a two-dimensional array, in the same way we have n indices in an n-dimensional array or multidimensional array.
MULTIDIMENSIONAL
ARRAYS
A
multidimensional array in simple terms is an array of arrays. Like we have one
index in a one-dimensional array, two indices in a two-dimensional array, in
the same way we have n indices in an n-dimensional array or multidimensional
array. Conversely, an n-dimensional array is specified using n indices. An
n-dimensional m1 × m2 × m3 ×….. mn
array is a collection m1 * m2
* m3 *…… *mn ements. In a multidimensional array, a
particular element is specified by using n subscripts as A[I1] [I2] [I3]..... [In],
where,
I1 <= M1 I2 <= M2 I3 <= M3 …….. In <= Mn
A
multidimensional array can contain as many indices as needed and the
requirement of the memory increases with the number of indices used. However, practically
speaking we will hardly use more than three indices in any program. Figure 5.32
shows a three-dimensional array. The array has three pages, four rows, and two
columns.
A
multidimensional array is declared and initialized similar to one- and
two-dimensional arrays.
Example 5.8
Consider
a three-dimensional array defined as int A[2][2][3]. Calculate the number of
elements in the array. Also show the memory representation of the array in row
major order and column major order.
Note
A
three-dimensional array consists of pages. Each page in turn contains m rows
and n columns.
The
three dimensional array will contain 2 × 2 × 3 = 12 elements.
33.
Write a program to read and display a 2 × 2 × 2 array.
#include <stdio.h>
#include <conio.h>
int main()
{
int array [2] [2] [2], i, j, k;
clrscr();
printf("\n Enter the elements
of the matrix");
for (i = 0; i < 2;i++)
{
for(j=0;j< 2;j++)
{
for (k=0; k < 2;k++)
scanf("%d", &array
[i] [j] [k]);
}
}
}
printf("\n The matrix is:
");
for (i=0; i < 2; i++)
{
printf("\n\n");
for(j=0;j< 2;j++) subs
{
printf("\n");
for (k=0; k < 2; k++)
printf("\tarray [%d] [%d] [%d]
= %d", i, j, k], array[i][j] [k]);
}
}
getch();
return 0;
}
Output
Enter the elements of the matrix
1 2 3 4 5 6 7 8
The matrix is:
arr [0] [0] [0] = 1 arr [0] [0] [1] = 2
arr [0] [1] [0] = 3 arr [0] [1] [1] = 4
arr [1] [0] [0] = 5 arr [1] [0] [1] = 6
arr [1] [1] [0] = 7 arr [1] [1] [1] = 8
Programming in C: Unit II (a): Arrays : Tag: : with Example C Programs - Multidimensional Arrays
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation