Question:9.
Sum of two matrices (Sum of two double dimension arrays).
Conditions:
The dimensions of both matrices should be same. i.e. the number of rows and columns in both should be the same.
Sol:
/* Sum of two matrix */ #include<stdio.h> #include<stdlib.h> int main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j; /* clrscr(); */ /* input total rows and cols */ printf("Enter total rows and cols of 1st matrix "); scanf("%d %d",&r1,&c1); printf("Enter total rows and cols in 2nd matrix "); scanf("%d %d",&r2,&c2); /* condition */ if(r1==r2 && c1==c2) printf("sum can be calculated\n"); else { printf("Sum cannot be calculated\n"); getch(); exit(1); //or //return; } /* input 1st matrix*/ printf("Enter elements of 1st matrix \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("Enter [%d][%d] element ",i,j); scanf("%d",&a[i][j]); } } /* input 2nd matrix */ printf("Enter elements of 2nd matrix\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("Enter [%d][%d] element ",i,j); scanf("%d",&b[i][j]); } } //display 1st matrix printf("1st matrix is \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%4d",a[i][j]); } printf("\n"); } //display 2nd matrix printf("2nd matrix is\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("%4d",b[i][j]); } printf("\n"); } //cal and display sum matrix printf("sum is \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j]=a[i][j]+b[i][j]; printf("%4d",c[i][j]); } printf("\n"); } getch(); return(0); }
/* Output */ Case:1 Enter total rows and cols of 1st matrix 2 3 Enter total rows and cols in 2nd matrix 3 6 Sum cannot be calculated Case:2 Enter total rows and cols of 1st matrix 3 3 Enter total rows and cols in 2nd matrix 3 3 sum can be calculated Enter elements of 1st matrix Enter [0][0] element 1 Enter [0][1] element 2 Enter [0][2] element 3 Enter [1][0] element 4 Enter [1][1] element 5 Enter [1][2] element 6 Enter [2][0] element 7 Enter [2][1] element 8 Enter [2][2] element 9 Enter elements of 2nd matrix Enter [0][0] element 3 Enter [0][1] element 6 Enter [0][2] element 9 Enter [1][0] element 5 Enter [1][1] element 2 Enter [1][2] element 3 Enter [2][0] element 6 Enter [2][1] element 4 Enter [2][2] element 2 1st matrix is 1 2 3 4 5 6 7 8 9 2nd matrix is 3 6 9 5 2 3 6 4 2 sum is 4 8 12 9 7 9 13 12 11
Question:10.
Difference of two matrices (Difference of two double dimension arrays).
Conditions:
The dimensions of both matrix should be same. i.e. number of rows and columns in both should be same.
Sol:
/* Difference of two matrix */ #include<stdio.h> #include<stdlib.h> int main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j; /* clrscr(); */ /* input total rows and cols */ printf("Enter total rows and cols of 1st matrix "); scanf("%d %d",&r1,&c1); printf("Enter total rows and cols in 2nd matrix "); scanf("%d %d",&r2,&c2); /* condition */ if(r1==r2 && c1==c2) printf("Difference can be calculated\n"); else { printf("Difference cannot be calculated\n"); getch(); exit(1); //or //return; } /* input 1st matrix*/ printf("Enter elements of 1st matrix \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("Enter [%d][%d] element ",i,j); scanf("%d",&a[i][j]); } } /* input 2nd matrix */ printf("Enter elements of 2nd matrix\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("Enter [%d][%d] element ",i,j); scanf("%d",&b[i][j]); } } //display 1st matrix printf("1st matrix is \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%4d",a[i][j]); } printf("\n"); } //display 2nd matrix printf("2nd matrix is\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("%4d",b[i][j]); } printf("\n"); } //cal and display sum matrix printf("Difference between two matrix \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { c[i][j]=a[i][j]-b[i][j]; printf("%4d",c[i][j]); } printf("\n"); } getch(); return(0); }
/* Output */ case:1 Enter total rows and cols of 1st matrix 3 2 Enter total rows and cols in 2nd matrix 6 3 Difference cannot be calculated case:2 Enter total rows and cols of 1st matrix 3 3 Enter total rows and cols in 2nd matrix 3 3 Difference can be calculated Enter elements of 1st matrix Enter [0][0] element 2 Enter [0][1] element 3 Enter [0][2] element 6 Enter [1][0] element 5 Enter [1][1] element 9 Enter [1][2] element 6 Enter [2][0] element 3 Enter [2][1] element 2 Enter [2][2] element 5 Enter elements of 2nd matrix Enter [0][0] element 1 Enter [0][1] element 2 Enter [0][2] element 3 Enter [1][0] element 6 Enter [1][1] element 5 Enter [1][2] element 9 Enter [2][0] element 6 Enter [2][1] element 3 Enter [2][2] element 5 1st matrix is 2 3 6 5 9 6 3 2 5 2nd matrix is 1 2 3 6 5 9 6 3 5 Difference between two matrix 1 1 3 -1 4 -3 -3 -1 0
Question:11.
Product of two matrices (Product of two double dimension arrays).
Conditions:
the number of columns in the 1st matrix should be equal to the number of rows in the 2nd matrix.
Sol:
//matrix product /* Product Of two matrix Condition: number of columns in 1st matrix should be equal to the numbers of rows in the 2nd matrix. */ #include<stdio.h> #include<stdlib.h> int main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k; /* clrscr(); */ printf("Enter total rows and cols of 1st matrix "); scanf("%d %d",&r1,&c1); printf("Enter total rows and cols in 2nd matrix "); scanf("%d %d",&r2,&c2); if(c1==r2) printf("Product can be calculated\n"); else { printf("Product cannot be calculated\n"); getch(); exit(1); //or //return; } printf("Enter elements of 1st matrix \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("Enter [%d][%d] element ",i,j); scanf("%d",&a[i][j]); } } printf("Enter elements of 2nd matrix\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("Enter [%d][%d] element ",i,j); scanf("%d",&b[i][j]); } } printf("1st matrix is \n"); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { printf("%4d",a[i][j]); } printf("\n"); } printf("2nd matrix is\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { printf("%4d",b[i][j]); } printf("\n"); } /* to calculate product*/ for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<c1;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf("product is \n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf("%4d",c[i][j]); } printf("\n"); } getch(); return(0); }
/* Output */ case:1 Enter total rows and cols of 1st matrix 2 3 Enter total rows and cols in 2nd matrix 6 5 Product cannot be calculated case:2 Enter total rows and cols of 1st matrix 3 3 Enter total rows and cols in 2nd matrix 3 3 Product can be calculated Enter elements of 1st matrix Enter [0][0] element 1 Enter [0][1] element 2 Enter [0][2] element 3 Enter [1][0] element 4 Enter [1][1] element 5 Enter [1][2] element 6 Enter [2][0] element 3 Enter [2][1] element 2 Enter [2][2] element 6 Enter elements of 2nd matrix Enter [0][0] element 3 Enter [0][1] element 2 Enter [0][2] element 5 Enter [1][0] element 6 Enter [1][1] element 3 Enter [1][2] element 2 Enter [2][0] element 5 Enter [2][1] element 1 Enter [2][2] element 2 1st matrix is 1 2 3 4 5 6 3 2 6 2nd matrix is 3 2 5 6 3 2 5 1 2 product is 30 11 15 72 29 42 51 18 31