multiplication of matrix in C
#include<stdio.h>
int main(){
int r1,c1,r2,c2;
printf("enter the row for matrix a : ");
scanf("%d",&r1);
printf("enter the column for matrix a : ");
scanf("%d",&c1);
printf("enter the row for matrix b : ");
scanf("%d",&r2);
printf("enter the column for matrix b : ");
scanf("%d",&c2);
int a[r1][c1];
int b[r2][c2];
int c[r1][c2];
printf("enter the elements of the matrix a : ");
for(int i=0; i<r1; i++){
for(int j=0; j<c1; j++){
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of the matrix b : ");
for(int i=0; i<r2; i++){
for(int j=0; j<c2; j++){
scanf("%d",&b[i][j]);
}
}
printf("\n");
//print matrix a
for(int i=0; i<r1; i++){
for(int j=0; j<c1; j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
//print matrix b
for(int i=0; i<r2; i++){
for(int j=0; j<c2; j++){
printf("%d ",b[i][j]);
}
printf("\n");
}
int rowcol=c1;
for(int i=0; i<r1; i++){
for(int j=0; j<c2; j++){
c[i][j]=0;
for(int k=0; k<rowcol; k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\n");
for(int i=0; i<r1; i++){
for(int j=0; j<c2; j++){
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
//output :
.png)
.png)
.png)
Comments
Post a Comment