i got a assignment to submit and want to learn code for matrix multiplication in c. can anyone drop code for matrix multiplication in c, with a example program and some easy explanation. Because as im New to C language looking for code for matrix multiplication in c.
Share
Here is the example program with code for matrix multiplication in c and the program below is easy to undersstand and you may copy this code and execute on your computer directly.
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], product[10][10];
int i, j, k, rows1, cols1, rows2, cols2;
printf("Enter the number of rows and columns of first matrix: ");
scanf("%d %d", &rows1, &cols1);
printf("Enter the elements of first matrix:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the number of rows and columns of second matrix: ");
scanf("%d %d", &rows2, &cols2);
if (cols1 != rows2) {
printf("Matrices cannot be multiplied\n");
return 0;
}
printf("Enter the elements of second matrix:\n");
for (i = 0; i < rows2; i++) {
for (j = 0; j < cols2; j++) {
scanf("%d", &b[i][j]);
}
}
// Initializing elements of product matrix to 0
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
product[i][j] = 0;
}
}
// Multiplying matrices
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
for (k = 0; k < rows2; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
// Printing the product matrix
printf("Product of the matrices:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
You may ask for different sort of programming questions further and happy here to answer them and you may look at how to write a console program to store employee details on C#.