Matrix Multiplication in C
Matrix Multiplication in C
Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix, and second element matrix. Then we do multiplication on the user's entered matrices.
In the first matrix multiplication, the one-row element is multiplied by all column elements in the second matrix.
Matrix Multiplication in C
#include<stdio.h> int main(void) { int a, s, t, v, m, n, k, total = 0; int first[10][10], second[10][10], multiple[10][10]; printf(" Entert the No. of rows and columns for first matrix \n "); scanf("%d%d", &m, &n); printf(" Enter your matrix elements : \n "); for (a = 0; a < m; a++) for (s = 0; s < n; s++) scanf("%d", &first[a][s]); printf(" Enter the No. of rows and columns for second matrix\n"); scanf(" %d %d", &t, &v); if (n != t) printf(" Your matrix cannot be multiplied with each other. \n "); else { printf(" Enter your elements for second matrix \n "); for (a = 0; a < t; a++) for (s = 0; s < v; s++) scanf("%d", &second[a][s] ); for (a = 0; a < m; a++) { for (s = 0; s < v; s++) { for (k = 0; k < t; k++) { total= total + first[a][k] * second[k][s]; } multiple[a][s] = total; total = 0; } } printf(" The result of matrix multiplication or product of the matrices is: \n "); for (a = 0; a < m; a++) { for (s = 0; s < v; s++) printf("%d \t", multiple[a][s] ); printf(" \n "); } } return 0; }
Output:
Explanation
First, the standard input-output header file must be used, which is the basis for C programming. Then you must declare the main () function, and define it. Inside the main () function scope, you first have to declare some integer variables a, s, t, v, m, n, k, total = 0 followed by some 2D integer arrays (which will serve as matrices in your program) first[10][10], second[10][10], multiple[10][10]. Then use a print statement to inform the user to include a no. of rows and columns of the first matrix with or give data.
Inserting all the elements into your loop needs array one by one, followed by a scanf ().
for (a = 0; a < m; a++) for (s = 0; s < n; s++) scanf("%d", &first[a][s]);
This is a nested loop that takes values for "m" number of rows and "n" number of columns, i.e., iterates "m" and "n" number of times in the first[] array to feed the values. Again, use a separate print statement to instruct the user to provide or provide input for a number of second matrix rows and columns.
for (a = 0; a < t; a++) for (s = 0; s < v; s++) scanf("%d", &second[a][s] );
Once more, the same nested loops are used to feed values into the second[] list. Now the condition is checked if the number of columns in the first matrix is equal to that of rows in the second matrix. If this condition satisfies you must write the multiplication logic. For loop this would need to be nested or nested.
for (a = 0; a < m; a++) { for (s = 0; s < v; s++) { for (k = 0; k < t; k++) { total= total + first[a][k] * second[k][s]; } multiple[a][s] = total; total = 0; } }
Here the three loops were used which store the first[] and second[] multiplicative value in the tot variable, and this addition of multiplicative values will continue until it passes through all the array values. Place through the measured value of tot in the multiple[] array at the same time, which will place the resulting multiplication. You now need to print the resulting 2D array using loop nesting.
printf(" The result of matrix multiplication or product of the matrices is: \n "); for (a = 0; a < m; a++) { for (s = 0; s < v; s++) printf("%d \t", multiple[a][s] ); printf(" \n "); } } return 0; }