Matrix

SemiColon
By -
0


Question:   Input instruction: Take a (5*3) matrix and (3*4) matrix as input from user. The first dimension indicate the number of rows and the second dimension indicate the number of columns.
Output instruction: Show the multiplied matrix as output. 


Solution:


#include <stdio.h>

int main()
{
  int a, b, x, y, i, j, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

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

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

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

  if (b != x)
    printf("\nThe matrices can't be multiplied with each other!\n");
  else
  {
    printf("Enter elements of second matrix\n");

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

    for (i = 0; i < a; i++) {
      for (j = 0; j < y; j++) {
        for (k = 0; k < x; k++) {
          sum = sum + first[i][k]*second[k][j];
        }

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

    printf("Product of the matrices:\n");

    for (i = 0; i < a; i++) {
      for (j = 0; j < y; j++)
        printf("%d\t", multiply[i][j]);

      printf("\n");
    }
  }

  return 0;

}

Tags:

Post a Comment

0Comments

Post a Comment (0)