multi-dimensional array

It is a collection of single dimensional array. value is stored as a table from in it. here is syntex below-

SYNTEX:-

Storage-class data-type array-name[size1][size2][size3].....;

avoid complexity, here we will discuss about two dimensional array, 

data-type array-name[size1][size2];

in two-dimensional array  size1 represents row and size2 column.

such as,

Example

int arr[2][3];

to know how many element store in the two-dimensional array we use size row X column.-

int arr[2][3];

row × column, then

2×3=6 it will store 6 elements, such as in table form below-

 

initialization of two-dimensional array

SYNTEX:-

multi-dimensional array initialization is similar to normal array. i.e-

data_type array_name[size1][size2]={list};

Example:-

int arr[2][3]={3,2,4,1,6,7};

or

int arr[2][3]={ 
                 {3,4.6}, 
                 {2,1,7} 
               };

as we know, first size as row while second as column, such as,

multi-dimensional-array

means,

x[0][0] = 3
x[0][1] = 4
x[0][2] = 6
x[1][0] = 2
x[1][1] = 1
x[1][2] = 7

In below program, we take the elements from the user and accessing them,

accessing of two-dimensional array element

Like the single dimensional array, the element of the multi dimensional array is accessed from their index value. But here we use 2 loop.

int arr[2][3];

You can understand this with the help of the program given below.

#include<iostream.h>
#include<conio.h>

void main()
{
int i,j,r=1,s=1,x[2][3];              // array variable declaration
clrscr();

for(i= 0; i<2; i++)
{
 for(j= 0; j<3; j++)
 {
  cout<<"Enter "<<r++<<" element in x["<<i<<"]["<<j<<"]: ";
  cin>>x[i][j];
 }
}

cout<<"Displaying array element: ";
for(i= 0; i<2; i++)
{
  for(j= 0; j<3; j++)
  {
cout<<endl<<s++<<" Element store array in x["<<i<<"]["<<j<<"] is: "<<x[i][j];      // store 5 value at a time
  }
}

getch();
}
OUTPUT:-
Enter 1 element in x[0][0]: 2
Enter 2 element in x[0][1]: 3
Enter 3 element in x[0][2]: 1
Enter 4 element in x[1][0]: 5
Enter 5 element in x[1][1]: 6
Enter 6 element in x[1][2]: 4

Displaying array element: 
1 Element store array in x[0][0] is: 2
2 Element store array in x[0][1] is: 3
3 Element store array in x[0][2] is: 1
4 Element store array in x[1][0] is: 5
5 Element store array in x[1][1] is: 6
6 Element store array in x[1][2] is: 4

 

Related Exercise

More about array


previous-array and their types

next-Structure in C++