Search This Blog

Thursday, October 13, 2011

Write a program to read two matrices from the keyboard and find its product

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  #include<stdlib.h>
   4:  void getproduct(int,int,int,float[10][10],float[10][10]);
   5:  void main()
   6:  {
   7:  int i,j,k,m1,n1,m2,n2;
   8:  float x[10][10],y[10][10];
   9:  printf("\nEnter the rows of matrix X: ");
  10:  scanf("%d",&m1);
  11:  printf("\nEnter the clomuns of matrix X: ");
  12:  scanf("%d",&n1);
  13:  printf("\nEnter the rows of matrix Y: ");
  14:  scanf("%d",&m2);
  15:  printf("\nEnter the columns of matrix y: ");
  16:  scanf("%d",&n2);
  17:  if(n1==m2)
  18:  k=n1;
  19:  else
  20:  {
  21:  printf("\nMultiplication of X and Y is not possible:");
  22:  getch();
  23:  exit(0);
  24:  }
  25:  printf("\nEnter the elements of first matrix:\n");
  26:  for(i=0;i<m1;i++)
  27:  for(j=0;j<n1;j++)
  28:  scanf("%f",&x[i][j]);
  29:  printf("\nEnter the elements of second matrix:\n");
  30:  for(i=0;i<m2;i++)
  31:  for(j=0;j<n2;j++)
  32:  scanf("%f",&y[i][j]);
  33:  getproduct(m1,k,n2,x,y);
  34:  getch();
  35:  }
  36:  void getproduct(int m1,int k,int n2,float x[10][10],float y[10][10])
  37:  {
  38:  int u,v,i;
  39:  float z[10][10];
  40:  for(u=0;u<m1;u++)
  41:  for(v=0;v<n2;v++)
  42:  {
  43:  z[u][v]=0.0;
  44:  for(i=0;i<k;i++)
  45:  z[u][v]+=x[u][i]*y[i][v];
  46:  }
  47:  printf("MATRIX Z=X*Y\n\n");
  48:  for(u=0;u<m1;u++)
  49:  {
  50:  for(v=0;v<n2;v++)
  51:  {
  52:  printf("%8.3f",z[u][v]);
  53:  }
  54:  printf("\n");
  55:  }
  56:  getch();
  57:  }

No comments:

Post a Comment