Discuss in brief about multidimensional array

Estudies4you
Q26. Discuss in brief about multidimensional array.
Answer:

In C+ +, arrays can be declared with more than one index. Such arrays are called multidimensional arrays. A multidimensional array is defined as array of arrays. It has either two, three or more dimensions (indexes). A multi-dimensional array can be thought as an array of arrays. A multi-dimensional array consisting of 3 two- dimensional arrays each having 2 rows and 3 columns is given below.
Declaration of Multidimensional Array
A multi-dimensional array is initialized as shown below,
int a[3][2][3];
Here, a is declared as a 3D array that accommodates 3 tables containing 2 rows and 3 columns.
Each element is represented by the array name followed by table number, row number and column number.
Initialization of Multidimensional Array
A multi-dimensional array is initialized as shown below,
int a[3][2][3] = {
{
[4, 5, 6], [7, 10, 12]
}, /*end of 0" 2-D array*/
{
[1, 2, 4], [2, 6, 8]
} /*end of 1* 2-D array*/
{
[2,1, 3], [1, 3, 5]

} /*end of 2" 2-D array*/
};

Physically these elements are stored in memory as shown below,
Figure (2): Memory Representation of 3-D Array ‘
Initialization of a large sized array, suppose of 250 elements, is difficult at the time of declaration. In such cases, declaration must be done explicitly. In ‘C++’, loops (while, for, while or do while) are used to initialize the elements. There is no short cut to initialize array of large size in ‘C++’.

Accessing Multidimensional Arrays
Multi-dimensional array elements are stored in contiguous blocks of memory. The storage of elements is such that, the
last subscript of the array changes frequently than that of the first subscript. Consider the following 2D array,
a[3} 13];
The elements in this array will be stored in the following order,
a[0] (0], a[0] [1], a[0] [2],
a[1] [0], a[1] [1], a[1] [2],
a[2] [0], a[2] [1], a[2] [2].

Now, to access element 5 from the above array, the array subscript would be a[1] [1].
In this way, any element of the array can be accessed using array indices.

Program
#include<iostream.h>
#include<conio.h>
void main( )
{
int m1[3][3], m2[3][3],m3[3][3], i j, k, sum;
clrscr() ;
cout<<“\n Enter values for first 3 x 3 matrix:\n”;
for (i=0;i<=2;i++)
{
for (j=03;j<=2; j++)
cin>>m1 [i] [j] ;
}
cout<<“\n Enter values for second 3 x 3 matrix:\n”;
for (i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
cin>>m2[i] [j] ;
}
cout<<“\n The first 3 x 3 matrix entered is:\n”;
for (i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
cout<<“\t"<< m1 [i] [j] ;
cout<<“\n”;
}
cout<<“\n the second 3 x 3 matrix entered :\n”;
for (i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
cout<<“\t"’<< m2[i] [j] ;
cout<<“\n”;
}
for (i=0;i<=2;i++)
{
for (j=0;j<=2;j++)
{
sum = 0;
for(k=0;k<=2;k++)
sum = sum + m1[i][k] * m2[k][j];
m3[i][j]=sum;
}
}
cout<<“\nThe product of the above two matrices is:\n”;
for (i=0;i<=2;i++)
{
for.(j =0 ;j <=2; j++)
cout<<“\t"<<m3 [i] [j] ;
cout<<“\n”;
}
cout<<“\n Press any key to exit.”;
getch() ;
}

Output

To Top