Wednesday, October 3, 2018

CPL 8 - MATRIX MULTIPLICATION

8. Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of multiplication are checked.

ALGORITHM:

Step 1: Start

Step 2: [Read the order of both matrices M,N,P,Q]

Step 3: Check for i=0 to M  and j=0 to N for the matrix A,read A[i][j]

Step 4: Check for i=0to P and j=0 to Q for matrix B read B[i][j]

Step 5: if(N=P) then only multiplication is possible then go to step 6 otherwise go
            to step 7

Step 6: intialy set matrix C[i][j] as 0
  For k=0 to n
C[i][j]=C[i][j]+A[i][k]*B[i][k] go to step 8

Step 7: Multiplication is not possible

Step 8: Prints the multiplication of two matrix


Step 9: Stop

FLOWCHART PART - 1

FLOWCHART PART - 2

PROGRAM :

#include<stdio.h>

int main()
{
   int a[20][20],b[20][20],c[20][20];
   int m,n,p,q,i,j,k;

   printf("Enter rows and columns of matrix A\n");
   scanf("%d%d",&m,&n);
   printf("Enter rows and columns of matrix B\n");
   scanf("%d%d",&p,&q);

   if(n!=p)
   {
      printf("Matrix multiplication not possible\n");
      return 0;
   }

   printf("Enter elements of matrix A\n");
   for(i=0;i<m;i++)
   {
      for(j=0;j<n;j++)
      {
        scanf("%d",&a[i][j]);
      }
   }

   printf("Enter elements of matrix B\n");
   for(i=0;i<p;i++)
   {
      for(j=0;j<q;j++)
      {
        scanf("%d",&b[i][j]);
      }
   }

   for(i=0;i<p;i++)
   {
      for(j=0;j<q;j++)
      {
       c[i][j]=0;
       for(k=0;k<n;k++)
       {
          c[i][j]=c[i][j]+a[i][k]*b[k][j];
       }
      }
   }

   printf("Product of two matrices is\n");
   for(i=0;i<m;i++)
   {
      for(j=0;j<q;j++)
      {
      printf("%d\n",c[i][j]);
      }
   }

}


OUTPUT:


No comments:

Post a Comment