Sunday, September 2, 2012

Addition of Two Dimensional Matrix in C program

 Addition of Two Dimensional Matrix in C program


The following program is the example of 2 Dimensional Array for adding it and save it to 3rd Array.

/* Program to Addition of two Matrix */

#include<stdio.h>
void printarry(int ap[2][2]);

int main()
{
int n,i,j;
int a[2][2],b[2][2],c[2][2];
printf("Enter The A matrix");
for(i=0;i<2;i++)
{
 for(j=0;j<2;j++)
{
 scanf("%d",&a[i][j]);
}
}

printf("Enter the B Matrix");
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("\nMatrix A:\n");
printarry(a);
printf("\nMatrix B:\n");
printarry(b);
printf("Addition of Matrix is:\n");
printarry(c);
return 0;
}

void printarry(int ap[2][2])
{
int m,n;
for(m=0;m<2;m++)
{
 for(n=0;n<2;n++)
{
 printf("%d ",ap[m][n]);
}printf("\n");
}
}

0 comments:

Post a Comment