Interview Questions   Tutorials   Discussions   Programs   

C - Find the program of matrix multiplication using arrays?




342
views
asked marvit August 18, 2014 09:26 AM  

Find the program of matrix multiplication using arrays?


           

1 Answers



 
answered By bhjahnavi   0  
/* matrix multiplication using arrays */
#include <stdio.h>
void main()
{
  int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
  printf("Enter rows and column for first matrix: ");
  scanf("%d%d", &r1, &c1);
  printf("Enter rows and column for second matrix: ");
  scanf("%d%d",&r2, &c2);
  printf("\nEnter elements of matrix 1:\n");
  for(i=0; i<r1; ++i)
  {
  for(j=0; j<c1; ++j)
  {
    printf("Enter elements a%d%d: ",i+1,j+1);
    scanf("%d",&a[i][j]);
  }
  }

  printf("\nEnter elements of matrix 2:\n");
  for(i=0; i<r2; ++i)
  {
  for(j=0; j<c2; ++j)
  {
    printf("Enter elements b%d%d: ",i+1,j+1);
    scanf("%d",&b[i][j]);
  }
   }

  for(i=0; i<r1; ++i)
  {
  for(j=0; j<c2; ++j)
  {
    mult[i][j]=0;
  }
  }

  for(i=0; i<r1; ++i)
  { 
  for(j=0; j<c2; ++j)
  {
  for(k=0; k<c1; ++k)
  {
    mult[i][j]+=a[i][k]*b[k][j];
  }
  }
  }
  printf("\nOutput Matrix:\n");
  for(i=0; i<r1; ++i)
  {  
  for(j=0; j<c2; ++j)
  {
    printf("%d ",mult[i][j]);
    if(j==c2-1)
      printf("\n\n");
  }
  } 
}
output
Enter rows and column for first matrix: 2
2
Enter rows and column for second matrix:2
2
Enter elements of matrix 1
Enter elements a11 1
Enter elements a12 0
Enter elements a21 0
Enter elements a22 1
Enter elements of matrix 2
Enter elements b11 1
Enter elements b12 0
Enter elements b21 0
Enter elements b22 1
output matrix
1    0    
0    1

flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert