6 Two
6 Two
Two-dimensional array can be thought as a rectangular display of elements with rows and
columns.
For example elements of int x[3][3];
Array Initialization
int a[][]={{1,1,2},{3,4,4},{5,4,3}};
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
printf(“\n Elements of an array\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
}
2. Read the matrix of the order up to 10x10 elements and display the same in the
matrix form.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,row,col,a[10][10];
clrscr();
printf(“\n Enter order of matrix up to (10x10) :”);
scanf(“%d%d”,&row,&col);
printf(“\n Enter elements :”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n The matrix is :\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%d\t”,a[i][j]);
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[3][3],mat2[3][3],add[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“\nenter the number :”);
scanf(“%d”,&mat1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“\nenter the number :”);
scanf(“%d”,&mat2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
add[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf(“\naddition of two matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,add[i][j]);
}
}