Wednesday 19 April 2017

Example of Multiplication of two matrix in C

Dear Students ,

    Let's see your Answer with code .


#include <stdio.h>
#include<conio.h>
int main()
{
 // these are variables declared

  int row, col, row1, col1, i, j, k, sum = 0;

// These are array to store the value
  int first[5][5], second[5][5], multiply[5][5];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &row, &col);
  printf("Enter the elements of first matrix\n");

  for (i = 0; i < row; i++)
    for (j = 0; j < col; j++)
      scanf("%d", &first[i][j]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &row1, &col1);

  if (col != row1)
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for (i = 0; i < row1; i++)
      for (j = 0; j < col1; j++)
        scanf("%d", &second[i][j]);

    for (i = 0; i < row; i++) {
      for (j = 0; j < col1; j++) {
        for (k = 0; k < row1; k++) {
          sum = sum + first[i][k]*second[k][j];
        }

        multiply[i][j] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for (i = 0; i < row; i++) {
      for (j = 0; j < col1; j++)
        printf("%d ", multiply[i][j]);

      printf("\n");
    }
  }
     getch();
  return 0;
}



No comments:

Post a Comment