Program
Time Complexity: O(r1 * c1 * c2)
Space Compexity: 0(1)
where r1 and c1 are the number of rows and columns in matrix A, and r2 and c2 are the number of rows and columns in matrix B
#include <stdio.h>
int main()
{
printf("****************************************************");
printf("\n****************************************************");
printf("\n** WAP to multiply two matrices or 2D arrays **");
printf("\n** Created by Sheetal Garg **");
printf("\n** Assistant Professor **");
printf("\n** Phone No:9467863365 **");
printf("\n****************************************************");
printf("\n****************************************************\n");
int r1, c1, r2, c2, i, j, k, a[5][5], b[5][5], c[5][5], p = 1;
printf("Enter no. of rows and columns of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter no. of rows and columns of second matrix\n");
scanf("%d%d", &r2, &c2);
if (c1 == r2)
{
printf("enter elements of first matrix\n");
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("enter elements of second matrix\n");
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
for (i = 0; i < r1; i++)
for (j = 0; j < c2; j++)
{
c[i][j] = 0;
for (k = 0; k < c1; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
printf("\nThe first matrix is\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
printf("\nThe second matrix is\n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
printf("%d\t", b[i][j]);
printf("\n");
}
printf("\nThe resultant matrix multiplication is\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
printf("%d\t", c[i][j]);
printf("\n");
}
}
else
printf("\nmatrices cant be multiplied");
return 0;
}
Output 1
**************************************************** **************************************************** ** WAP to multiply two matrices or 2D arrays ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************** **************************************************** Enter no. of rows and columns of first matrix 3 3 Enter no. of rows and columns of second matrix 3 2 enter elements of first matrix 2 3 1 2 1 3 2 2 1 enter elements of second matrix 1 2 1 3 3 2 The first matrix is 2 3 1 2 1 3 2 2 1 The second matrix is 1 2 1 3 3 2 The resultant matrix multiplication is 8 15 12 13 7 12
Output 2
**************************************************** **************************************************** ** WAP to multiply two matrices or 2D arrays ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************** **************************************************** Enter no. of rows and columns of first matrix 2 3 Enter no. of rows and columns of second matrix 2 3 matrices cant be multiplied