TWO - DIMENSIONAL ARRAYS


TWO - DIMENSIONAL ARRAYS:
The data in a table can be considered as two-dimenstional array.
 Two-dimensional arrays are declared as follows:
                           type array_name [row_size][column_size];
Two dimensional arrays are stored in memory as shown below: As with the single-dimensional arrays, each dimension of the array is indexed from zero to its maximum size minus one; the first index selects the row and
 the second  index selects the column within that row.

INITIALIZING TWO –DIMENSIONAL ARRAYS
Like the one-dimensional arrays, two-dimensional arrays may beinitialized by following their declaration with a
 list of initial values enclosed in  braces. For example: static int table[2][3] = {0, 0, 0, 1, 1, 1};
initializes the elements of the first row to zero and the secnd row to one. The  initialization is done row by row.
 The above statement can be equivalently written as static int table[2][3] = {{0, 0, 0}, {1, 1, 1}};
by surrounding the elements of each row by braces. We can also initialize a two-dimensional array in the form of
a matrix asshown as:
                         static int table[2][3] = {
                                                      {0, 0, 0},
                                                      {1, 1, 1}
                                                       } ;
/*  Example for a Double Dimensions  */

main()
{
int a[2][2],i,j,b[2][2],c[2][2];
clrscr();
printf("enter the elements into an array a:");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("enter the elements into an array b:");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nthe elements of an array c :\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%5d",c[i][j]);
printf("\n");
}
getch();
}

/* A program is to find addition of a matrices */
#include <stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j;
clrscr();
printf("ENTER THE SIZE OF THE MATRIX A: ");
scanf("%d%d",&m,&n);
printf("ENTER THE SIZE OF THE MATRIX B: ");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("ENTER THE VALUES INTO MATRIX A: ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("ENTER THE VALUES INTO MATRIX B: ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
canf("%d",&b[i][j]);
pintf("THE MATRIX addition is: \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("ADDITION NOT POSSIBLE");
getch();
}

/* A program is to find multiplication of matrices */
#include<stdio.h>
void main()
{
int a[r1][c1],b[r2][c2],c[r][c],i,j;
printf("\nenter r1,c1,r2,c2");
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
printf("enter the 1 ele");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\nenter the 2 ele");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",b[i][j]);
if(c1==r2)
{
printf("\nmultiplication is possible");
}
else
printf("multiplication is not possible");
c[i][j]=0;
for(i=0;i<r2;i++)
{
for(j=0;j<c1;j++)
{
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
printf("%d",c[i][j]);
getch();
}

Comments