C Program to Multiply to Matrix Using Multi-dimensional Arrays
To multiply two matrices, the number of columns in the first matrix should be equal to the number of rows in the second matrix. This program displays the error until the number of columns of the first matrix is equal to the number of rows of the second matrix.
Example: Program to Multiply Two Matrices
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
return 0;
}
Output
Enter rows and column for first matrix: 3 2 Enter rows and column for second matrix: 3 2 Error! column of first matrix not equal to row of second. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a11: 3 Enter elements a12: -2 Enter elements a13: 5 Enter elements a21: 3 Enter elements a22: 0 Enter elements a23: 4 Enter elements of matrix 2: Enter elements b11: 2 Enter elements b12: 3 Enter elements b21: -9 Enter elements b22: 0 Enter elements b31: 0 Enter elements b32: 4 Output Matrix: 24 29 6 25
C program to multiply two matrices using multi-dimensional arrays:
#include <stdio.h>
#define ROW1 3
#define COL1 3
#define ROW2 3
#define COL2 2
int main()
{
int matrix1[ROW1][COL1], matrix2[ROW2][COL2], result[ROW1][COL2];
int i, j, k;
// Input matrix1
printf("Enter elements of matrix1 (%d x %d):\n", ROW1, COL1);
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL1; j++) {
scanf("%d", &matrix1[i][j]);
}
}
// Input matrix2
printf("Enter elements of matrix2 (%d x %d):\n", ROW2, COL2);
for (i = 0; i < ROW2; i++) {
for (j = 0; j < COL2; j++) {
scanf("%d", &matrix2[i][j]);
}
}
// Multiply matrix1 and matrix2
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL2; j++) {
result[i][j] = 0;
for (k = 0; k < COL1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Output result
printf("\nResult matrix (%d x %d):\n", ROW1, COL2);
for (i = 0; i < ROW1; i++) {
for (j = 0; j < COL2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
This program first defines two matrices matrix1
and matrix2
with dimensions ROW1 x COL1
and ROW2 x COL2
respectively, as well as a result matrix result
with dimensions ROW1 x COL2
. It then prompts the user to input the elements of matrix1
and matrix2
, and multiplies them together using a nested loop. Finally, it outputs the resulting matrix result
.
Note that the program assumes that the number of columns in matrix1
is equal to the number of rows in matrix2
in order for the multiplication to be valid. If this is not the case, the program will not produce correct results.