Q : write a program to sum 3x3 matrix using an array.
Solution :
#include<stdio.h>
#include<conio.h>
/*============================
Girfa Student Help
Adding 3x3 matrix using array
==============================*/
void print(int ar[][3])
{
int r,c;
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf("\t%d",ar[r][c]);
printf("\n");
}
}
void main()
{
int ar1[3][3],ar2[3][3],arr[18],i,sum,r,c;
clrscr();
printf("\nEnter
for first array\n\n");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
printf("Enter value for [%d][%d]>> ",r+1,c+1);
scanf("%d",&ar1[r][c]);
}
}
printf("\nEnter
for second array\n\n");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
printf("Enter value for [%d][%d]>> ",r+1,c+1);
scanf("%d",&ar2[r][c]);
}
}
// input
in array for sum
for(r=0,i=0;r<3;r++)
{
for(c=0;c<3;c++)
{
arr[i]=ar1[r][c];
i++;
}
}
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
{
arr[i]=ar2[r][c];
i++;
}
}
//sum
for(r=0,sum=0;r<i;r++)
sum=sum+arr[r];
clrscr();
printf("\nArray
1\n\n");
print(ar1);
printf("\n\nArray
2\n\n");
print(ar2);
printf("\n\nSum=%d",sum);
getch();
}
No comments:
Post a Comment