100% SET 4: 6(a) Prog to add and subtract two 2D arrays.
#include <stdio.h>
void main()
{
int arr1[2][3]={{1,-1,2},{0,1,2}};
int arr2[][3]={0,1,-3,4, 7,1};
int i,j,sum_arr[2][3],diff_arr[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
sum_arr[i][j]=arr1[i][j]+arr2[i][j]; //addition of arrays
diff_arr[i][j]=arr1[i][j]-arr2[i][j]; //substraction of arrays
}
}
/* Printing the two given matrices begins*/
printf("\n arr1= \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",arr1[i][j]);
}
printf("\n");
}
printf("\n arr2= \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",arr2[i][j]);
}
printf("\n");
}
/*sum of the arrays follows:*/
printf("\n the sum is:\n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",sum_arr[i][j]);
}
printf("\n");
}
/*difference of the arrays follows:*/
printf("\n the difference is:\n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",diff_arr[i][j]);
}
printf("\n");
}
}
{
int arr1[2][3]={{1,-1,2},{0,1,2}};
int arr2[][3]={0,1,-3,4, 7,1};
int i,j,sum_arr[2][3],diff_arr[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
sum_arr[i][j]=arr1[i][j]+arr2[i][j]; //addition of arrays
diff_arr[i][j]=arr1[i][j]-arr2[i][j]; //substraction of arrays
}
}
/* Printing the two given matrices begins*/
printf("\n arr1= \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",arr1[i][j]);
}
printf("\n");
}
printf("\n arr2= \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",arr2[i][j]);
}
printf("\n");
}
/*sum of the arrays follows:*/
printf("\n the sum is:\n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",sum_arr[i][j]);
}
printf("\n");
}
/*difference of the arrays follows:*/
printf("\n the difference is:\n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %2d",diff_arr[i][j]);
}
printf("\n");
}
}
తెరమీద
arr1=
1 -1 2
0 1 2
arr2=
0 1 -3
4 7 1
the sum is:
1 0 -1
4 8 3
the difference is:
1 -2 5
-4 -6 1
1 -1 2
0 1 2
arr2=
0 1 -3
4 7 1
the sum is:
1 0 -1
4 8 3
the difference is:
1 -2 5
-4 -6 1
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.