Matrix Multiplication C Program, Study notes of Computer science

This c program demonstrates the implementation of matrix multiplication. The user is prompted to input the orders and elements of two matrices to check if they can be multiplied. If the number of columns of the first matrix is equal to the number of rows of the second matrix, the program performs the multiplication and displays the resulting matrix.

Typology: Study notes

2021/2022

Uploaded on 09/13/2022

prince-rai
prince-rai 🇮🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
//MULTIPLICATION OF MATRIX
#include <stdio.h>
#include<math.h>
int main()
{
int a[3][3],b[3][3],r1,c1,r2,c2,i,j,k,mul[3][3];
printf("Input order of first matrix\n");
scanf("%d\n%d",&r1,&c1);
printf("Input order of second matrix\n");
scanf("%d\n%d",&r2,&c2);
printf("Input elements of first matrix\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&a[i][j]);
}
}
printf("Input element of second matrix\n");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf("%d",&b[i][j]);
}
}
if(c1!=r1){
printf("Multiplication is not possible");
}
else{
printf("multiply of the matrix=\n");
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
mul[i][j]=0;
for(k=0;k<c1;k++){
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}
pf2

Partial preview of the text

Download Matrix Multiplication C Program and more Study notes Computer science in PDF only on Docsity!

//MULTIPLICATION OF MATRIX

#include <stdio.h> #include<math.h>

int main() { int a[3][3],b[3][3],r1,c1,r2,c2,i,j,k,mul[3][3]; printf("Input order of first matrix\n"); scanf("%d\n%d",&r1,&c1); printf("Input order of second matrix\n"); scanf("%d\n%d",&r2,&c2); printf("Input elements of first matrix\n"); for(i=0;i<r1;i++){ for(j=0;j<c1;j++){ scanf("%d",&a[i][j]); } } printf("Input element of second matrix\n"); for(i=0;i<r2;i++){ for(j=0;j<c2;j++){ scanf("%d",&b[i][j]); } }

if(c1!=r1){ printf("Multiplication is not possible"); } else{ printf("multiply of the matrix=\n"); for(i=0;i<r1;i++){ for(j=0;j<c2;j++){ mul[i][j]=0; for(k=0;k<c1;k++){ mul[i][j]+=a[i][k]*b[k][j]; } } } for(i=0;i<r1;i++){ for(j=0;j<c2;j++){ printf("%d\t",mul[i][j]); } printf("\n"); } }

return 0;

}