-->

program to multiply two matrices of order mxn

// program to multiply two matrices of order 'mxn'
#include <stdio.h>
#include<conio.h>
void main()
{
    int A[3][3], B[3][3], C[3][3];
    int row, col, i, sum;

    /*
     * Reads elements in first matrix from user
     */
    printf("Enter elements in matrix A of size 2x2: \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   scanf("%d", &A[row][col]);
}
    }

    /*
     * Reads elements in second matrix from user
     */
    printf("\nEnter elements in matrix B of size 2x2: \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   scanf("%d", &B[row][col]);
}
    }

    /*
     * Multiplies both matrices A*B
     */
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   sum = 0;
   /*
    * Multiplies row of first matrix to column of second matrix
    * And stores the sum of product of elements in sum.
    */
   for(i=0; i<2; i++)
   {
sum += A[row][i] * B[i][col];
   }

   C[row][col] = sum;
}
    }

    /*
     * Prints the product of matrices
     */
    printf("\nProduct of Matrix A * B = \n");
    for(row=0; row<2; row++)
    {
for(col=0; col<2; col++)
{
   printf("%d ", C[row][col]);
}
printf("\n");
    }
getch();;
}

No comments:

Post a Comment