Previous | Home | Next |
A program to add two 2-d array and place the result in the third array
The following program demonstrates the addition of the two dimensional array or matrix and store their result in the third matrix as their result.
#include<conio.h>
void main()
{
int a[3][3],b[3][3], sum[3][3], i=0, j=0;
clrscr();
printf("Enter the elements of the first array :");
for (i=0;i<=2 ;i++ )
{
for(j=0; j<=2; j++)
{
scanf("%d\n", &a[i][j]);
printf("\n");
}
}
printf("Enter the elements of the second array :")
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
scanf("%d", &b[i][j]);
}
}
/*Now placing the sum of the two matrices in the third one */
for (i=0;i<=2 ;i++ )
{
for(j=0; j<=2; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
/* Now displaying the sum of the two matrices by following code snippet */
printf("\nThe sum of the matrices obtained is as follows :");
for(i=0; i<=2; i++)
{
for(j=0; j<=2; j++)
{
printf("\n%d", sum[i][j]);
printf("\n");
}
}
getch();
}
Output :Its output will be as follows :
Enter the elements of the first array :
1 2 3
3 4 5
4 3 7
Enter the elements of the second array :
2 4 5
5 6 1
1 3 7
The sum of the matrices obtained is as follows :
3 6 8
8 10 6
5 6 14
Previous | Home | Next |