Pages

Sunday, 17 February 2019

Program to illustrate Matrix Multiplication

// C program to illustrate matrix multiplication
  #include <stdio.h>
  void main()
    {
       int r1,r2,c1,c2, i, j,k, first[10][10], second[10][10], mul[10][10];
       printf("Enter the number of rows (r1) and columns (c1) of first matrix\n");
       scanf("%d%d", &r1, &c1);
       printf("Enter the number of rows (r2) and columns (c2) of second matrix\n");
       scanf("%d%d", &r2, &c2);
       if(c1 == r2)
       {
       printf("Enter the %d elements of first matrix\n",r1 * c1);
       for (i = 0; i < r1; i++)
          {
          for (j = 0; j < c1; j++)
            {
             scanf("%d", &first[i][j]);
            }
          }
       printf("\nFirst matrix :\n");
       for (i = 0; i < r1; i++)
         {
          for (j = 0; j < c1; j++)
            {
             printf("%d\t", first[i][j]);
            }
           printf("\n");
         }
       printf("Enter the %d elements of second matrix\n",r2 * c2);
       for (i = 0; i < r2; i++)
          {
          for (j = 0; j < c2; j++)
            {
             scanf("%d", &second[i][j]);
            }
          }
       printf("\nSecond matrix : \n");
       for (i = 0; i < r2; i++)
         {
          for (j = 0; j < c2; j++)
            {
             printf("%d\t", second[i][j]);
            }
           printf("\n");
         }
       printf("Matrix Multiplication is :-\n");
       for (i = 0; i < r1; i++)
        {
          for (j = 0 ; j < c2; j++)
          {
          mul[i][j]=0; 
          for (k = 0 ; k < c1; k++)
            {
             mul[i][j] = mul[i][j] + (first[i][k] * second[k][j]);
            }
          }
       }
       for (i = 0; i < r1; i++)
        {
          for (j = 0 ; j < c2; j++)
          {
            printf("%d\t", mul[i][j]);
          }
         printf("\n");
       }
    }
    else
      printf("Matrix multiplication can not perform since r1 is not equal to c2 \n");
 }

output:


No comments:

Post a Comment

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...