Saturday, September 17

Coding Contest- Matrix multiplication

PROBLEM : Write a program to multiply two 3 x 3 matrices where resulting matrix should produce
  • If cell element is even '0'
  • If cell element is odd '1'
eg: If resulting matrix of multiplication is

My code:


#include<stdio.h>
main()
{
      int a[3][3],b[3][3],c[3][3],d[3][3];
      int i,j,k;
      printf("Enter First matrix:...\n");
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      scanf("%d",&a[i][j]);
       printf("Enter Second matrix:...\n");
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      scanf("%d",&b[i][j]);
     
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      {
      c[i][j]=0;
      for(k=0;k<3;k++)
      c[i][j]+=a[i][k]*b[k][j];
      }
      for(i=0;i<3;i++)
      for(j=0;j<3;j++)
      {
      if(c[i][j]%2)d[i][j]=1;
      else d[i][j]=0;
      }
      printf("\n Product is..\n");
      for(i=0;i<3;i++)
      {
      for(j=0;j<3;j++)
      printf("%d\t",c[i][j]);
      printf("\n");
      }
       printf("\n Response is..\n");
      for(i=0;i<3;i++)
      {
      for(j=0;j<3;j++)
      printf("%d\t",d[i][j]);
      printf("\n");
      }
     
      getch();
}
     
     

0 comments :

Post a Comment