Two dimensional or multidimensional array

Two dimensional or multidimensional:  In the single dimensional array we can store only values in the form of list, but sometimes we needs to process the data in the form of rows and columns or we called it matrix. Then we use the two dimensional array.

 Syntax:            data-type array-name[row size][column size];

e.g       int A[2][3];

Representation of the two dimensional array: The two dimensional array is stored like in the form of rows and columns.

e.g: the representation of 2*2 matrix:

Column 0         Column 1

Row 0        15                    8           

[0][0]            [0][1]

Row 1          4               12

[1][0]           [1][1]

We can see how the elements are stored in the two dimensional array, and we have given the values and we can see their corresponding locations.

Initialization:

a.      Compile time: like the one dimensional array the initialization is almost same but we have to concentrate on the order of the row and columns.

e.g:            int A[2][2]={1,1,2,2}

the above array is of 2*2 means 2 rows and 2 columns. We can also initialize the values as follows:

int A[2][2]={{1,1},{2,2}};

b.      Run time: for run time initialization we have to take two variables, the first one for rows and another for columns.

e.g:

for(i=0;i<2;i++)

 {

for(j=0;j<2;j++)

{

scanf(“%d”,&A[i][j]);

}

}

Program:

PROGRAM TO DISPLAY A MATRIX

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,m,n,a[8][8];

clrscr();

printf(“Enter the order of the matrix: “);

scanf(“%d%d”,&m,&n);

printf(“\nEnter %d elements in the matrix:\n”,m*n);

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

scanf(“%d”,&a[i][j]);

}

printf(“\nThe entered matrix of %d*%d order is:\n”,m,n);

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

printf(“%4d”,a[i][j]);

printf(“\n”);

}

getch();

}

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *