« Previous                                                                  Next Chapter »
Two Dimensional Arrays

An array with two dimensions/subscripts can be called as two-dimensional array. The first dimension/subscript stands for row size which specifies the number of rows in the two dimensional array. The second dimension/subscript stands for number of columns in each row in the two dimensional array. The number of elements in the two dimensional array is the product of row size and column size. The total number of bytes of memory that has to be allotted for the two dimensional array is the product of number of elements and the number of bytes that has to be allotted for each variable of that data type. To refer each row number will be provided which can be called as row index. The row index starts with 0 and ends with (row size - 1). To refer each column number will be provided which can be called as column index. The column index starts with 0 and ends with (column size - 1).


To refer an element in the two dimensional array:

Syntax: Array_Name[row index][column index];

To declare the two dimensional array: 

Syntax: Data_Type Array_Name[row size][column size];

Ex: int a[2][3];


For the above declaration automatically 12 bytes of memory will be allocated for 6 cells. Immediately  it will be divided into 2 rows and 3 columns. To identify rows row index will be provided which starts with 0 and ends with 1. To identify  columns column index will be provided which starts with 0 ends with 2.


More Examples
/* program to print 2D array */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[2][2]={10,20,30,40};
clrscr();
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
}
}

/* program to read and print 2D array */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[2][2];
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
}
}

/* program to read m,n,array and print array */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
}

/* program to print diagonal elements and find their sum */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n,sum=0;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("diagonal elements are ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
printf("%d ",a[i][j]);
sum=sum+a[i][j];
}
else
{
printf(" ");
}
}
}
printf("\n");
printf("the sum is %d\n",sum);
}

/* program to print lower triangle and find sum of their elements */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n,sum=0;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("lower triangle is ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
if(i<j)
{
printf("%d ",a[i][j]);
sum=sum+a[i][j];
}
else
{
printf(" 0 ");
}
}
}
printf("\n");
printf("the sum is %d\n",sum);
}

/* program to print upper triangle and sum of their elements */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n,sum=0;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("upper triangle is ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
if(i>j)
{
printf("%d ",a[i][j]);
sum=sum+a[i][j];
}
else
{
printf(" 0 ");
}
}
}
printf("\n");
printf("the sum is %d\n",sum);
}

/* program to read array and print opposite of diagonal elements */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n,sum=0;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("reverse diagonal matrix is ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
if((i+j)==n-1)
{
printf("%d ",a[i][j]);
sum=sum+a[i][j];
}
else
{
printf("0 ");
}
}
}
printf("\n");
printf("the sum is %d\n",sum);
}

/* program to find the sum of the elements in the matrices */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n,sum=0;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
sum=sum+a[i][j];
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("the sum is %d\n",sum);
}

/* program to find the tranpose of given matrix */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],n,b[5][5];
clrscr();
printf("enter the n rows and n columns");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
b[j][i]=a[i][j];
}
}
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("the tranpose of matrix is");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",b[i][j]);
}
}
getch();
}

/* program to find whether the given matrix is symmetric or not */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],n,b[5][5],found=1;
clrscr();
printf("enter the n rows and n columns");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
b[j][i]=a[i][j];
}
}
printf("a matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("b matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",b[i][j]);
if(a[i][j]!=b[i][j])
{
found=0;
}
}
}
printf("\n");
if(found==1)
printf("symmetric");
else
printf("not symmetric");
getch();
}

/* program to find the addition of two matrices */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],n,b[5][5],c[5][5];
clrscr();
printf("enter the n rows and n columns");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of b[%d]a[%d]",i,j);
scanf("%d",&b[i][j]);
}
}
printf("a matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("b matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",b[i][j]);
}
}
printf("\n");
printf("c matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%2d ",c[i][j]);
}
}
getch();
}

/* program to find the multiplication of two matrices */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],b[5][5],c[5][5],k,n;
clrscr();
printf("enter the n rows and n columns");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of b[%d]a[%d]",i,j);
scanf("%d",&b[i][j]);
}
}

printf("a matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
printf("b matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",b[i][j]);
}
}
printf("\n");
printf("c matrix");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("%5d ",c[i][j]);
}
}
getch();
}

/* program to print sum of the each row and column */
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,a[5][5],m,n,sumr=0,sumc=0;
clrscr();
printf("enter the m rows and n columns");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("enter the value of a[%d]a[%d]",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
}
printf("\n");
for(i=0;i<m;i++)
{
sumr=0;
sumc=0;
for(j=0;j<n;j++)
{
sumr=sumr+a[i][j];
sumc=sumc+a[j][i];
}
printf("the sum of %d row is %d\n",i,sumr);
printf("the sum of %d column is %d\n",i,sumc);
}


  « Previous                                                                  Next Chapter »