Practice Program
Solution:
void main()
{
int i,j,a[10][10],b[10][10],c[10][10],n;
int m;
printf("Enter number of rows: ");
scanf("%d",&n);
printf("Enter Number of columns: ");
scanf("%d",&m);
//taking first matrix as input from the User
for(i=0;i<i++)
{
for(j=0;j<j++)
{
printf("Enter the element of first array: ");
scanf("%d",&a[i][j]);
}
}
//Printing Second matrix
printf("First Matrix is: \n");
for(i=0;i<i++)
{
for(j=0;j<j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
//taking Second matrix as input from the User
for(i=0;i<i++)
{
for(j=0;j<j++)
{
printf("Enter the element of Second array: ");
scanf("%d",&b[i][j]);
}
}
//printing Second matrix.
printf("Second Matrix is: \n");
for(i=0;i<i++)
{
for(j=0;j<j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
//determining the sum two matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
//Printing Sum of two matrix.
printf("Sum of two matrix is :\n");
for(i=0;i<i++)
{
for(j=0;j<j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
void main() { int i,j,a[10][10],b[10][10],c[10][10],n; int m; printf("Enter number of rows: "); scanf("%d",&n); printf("Enter Number of columns: "); scanf("%d",&m); //taking first matrix as input from the User for(i=0;i<i++) { for(j=0;j<j++) { printf("Enter the element of first array: "); scanf("%d",&a[i][j]); } } //Printing Second matrix printf("First Matrix is: \n"); for(i=0;i<i++) { for(j=0;j<j++) { printf("%d\t",a[i][j]); } printf("\n"); } //taking Second matrix as input from the User for(i=0;i<i++) { for(j=0;j<j++) { printf("Enter the element of Second array: "); scanf("%d",&b[i][j]); } } //printing Second matrix. printf("Second Matrix is: \n"); for(i=0;i<i++) { for(j=0;j<j++) { printf("%d\t",b[i][j]); } printf("\n"); } //determining the sum two matrix for(i=0;i<n;i++) { for(j=0;j<m;j++) { c[i][j] = a[i][j] + b[i][j]; } } //Printing Sum of two matrix. printf("Sum of two matrix is :\n"); for(i=0;i<i++) { for(j=0;j<j++) { printf("%d\t",c[i][j]); } printf("\n"); }
Output1
Enter number of rows: 3
Enter Number of columns:3
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
First Matrix is:
1 2 3
4 5 6
7 8 9
Enter Number of columns:3
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
First Matrix is:
1 2 3
4 5 6
7 8 9
Enter the Element of Second array:1
Enter the Element of Second array:2
Enter the Element of Second array :3
Enter the Element of Second array : 4
Enter the Element of Second array:5
Enter the Element of Second array:6
Enter the Element of Second array:7
Enter the Element of Second array: 8
Enter the Element of Second array: 9
Second Matrix is:
1 2 3
4 5 6
7 8 9
Sum of two matrix is :
2 4 6
8 10 12
14 16 18
Enter the Element of Second array:2
Enter the Element of Second array :3
Enter the Element of Second array : 4
Enter the Element of Second array:5
Enter the Element of Second array:6
Enter the Element of Second array:7
Enter the Element of Second array: 8
Enter the Element of Second array: 9
Second Matrix is:
1 2 3
4 5 6
7 8 9
Sum of two matrix is :
2 4 6
8 10 12
14 16 18
Output2
Enter number of rows: 2
Enter Number of columns:2
Enter the Element:10
Enter the Element:20
Enter the Element: 30
Enter the Element:40
First Matrix is:
10 20
30 40
Enter the Element of Second array:10
Enter the Element of Second array:20
Enter the Element of Second array : 30
Enter the Element of Second array:40
Second Matrix is:
10 20
30 40
Sum of two matrix is :
20 40
60 80
Enter Number of columns:2
Enter the Element:10
Enter the Element:20
Enter the Element: 30
Enter the Element:40
First Matrix is:
10 20
30 40
Enter the Element of Second array:10
Enter the Element of Second array:20
Enter the Element of Second array : 30
Enter the Element of Second array:40
Second Matrix is:
10 20
30 40
Sum of two matrix is :
20 40
60 80
defining a function 'give_sum' which will print the sum of two matrix
void give_sum(int a[10][10],int b[10][10],int n,int m)
{ int i,j;
int c[10][10];
//determining the sum two matrix
for(i=0;i<i++)
{
for(j=0;j<j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
//Printing Sum of two matrix
printf("Sum of two matrix is :\n");
for(i=0;i<i++)
{
for(j=0;j<j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void give_sum(int a[10][10],int b[10][10],int n,int m) { int i,j; int c[10][10]; //determining the sum two matrix for(i=0;i<i++) { for(j=0;j<j++) { c[i][j] = a[i][j] + b[i][j]; } } //Printing Sum of two matrix printf("Sum of two matrix is :\n"); for(i=0;i<i++) { for(j=0;j<j++) { printf("%d\t",c[i][j]); } printf("\n"); } }
Using above define function 'give_sum()':
void main()
{
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int a[3][3] = {{1,2,3}{4,5,6}{7,8,9}}
printf("Sum of two matrix is :");
give_sum(a,b,3,3)
}
void main() { int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; int a[3][3] = {{1,2,3}{4,5,6}{7,8,9}} printf("Sum of two matrix is :"); give_sum(a,b,3,3) }
Output1
Sum of two matrix is :
2 4 6
8 10 12
14 16 18
2 4 6
8 10 12
14 16 18
Using above define function 'give_sum()':
void main()
{
int a[2][2] = {{1,2,3},{4,5,6},{7,8,9}};
int a[3][3] = {{1,2,3}{4,5,6}{7,8,9}};
give_sum(a,b,2,2)
}
void main() { int a[2][2] = {{1,2,3},{4,5,6},{7,8,9}}; int a[3][3] = {{1,2,3}{4,5,6}{7,8,9}}; give_sum(a,b,2,2) }
Output2
Sum of two matrix is :
20 40
60 80
20 40
60 80
Using above define function 'give_sum()':
void main()
{
int a[2][3] = {{10,20,30},{40,50,60}};
int a[2][3] = {{10,20,30},{40,50,60}};
give_sum(a,b,2,3)
}
void main() { int a[2][3] = {{10,20,30},{40,50,60}}; int a[2][3] = {{10,20,30},{40,50,60}}; give_sum(a,b,2,3) }
Output3
Sum of two matrix is :
20 40 60
80 100 120
20 40 60
80 100 120
0 Comments
Post a Comment