Skip to content

Instantly share code, notes, and snippets.

@suryansh011
Created October 26, 2021 04:39
Show Gist options
  • Save suryansh011/4c9e8a4edc65fdaa8a2c1d086941f438 to your computer and use it in GitHub Desktop.
Save suryansh011/4c9e8a4edc65fdaa8a2c1d086941f438 to your computer and use it in GitHub Desktop.
Matrix Multiplication
#include<stdio.h>
#include<stdlib.h>
int main() {
int r1, c1, r2, c2;
int i, j, k; /* Counters */
printf("Enter size of the 1st matrix (m * n):\n");
scanf("%d %d", &r1, &c1);
int A[r1][c1];
printf("Now enter a %dx%d matrix:\n", r1, c1);
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter size of the 2nd matrix (m * n):\n");
scanf("%d %d", &r2, &c2);
int B[r2][c2];
printf("Now enter a %dx%d matrix:\n", r2, c2);
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &B[i][j]);
}
}
int result[r1][c2];
if (r2 == c1) {
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
result[i][j] = 0;
for(k = 0; k < r2; k++) {
result[i][j] = result[i][j] + A[i][k] * B[k][j];
}
printf("%d\t", result[i][j]);
}
printf("\n");
}
}
else {
printf("Matrix Multiplication not possible.");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment