C program for Matrix Multiplication | C code champ

C program for Matrix Multiplication | C code champ

453
1
SHARE

C program for : Let us discuss how to multiply M x N using a . First of all we need to understand how an M x N matrix multiplication takes place. Matrix multiplicationis a binary operation that takes a pair of matrices, and produces another matrix as output. Matrix is basically a two dimensional array. Let us see by an example how to multiply a matrix by another matrix :

C program for Matrix Multiplication | C code champ

As above shown, matrix multiplication consists a concept of dot products of rows and columns.

The “Dot Product” is where you multiply matching members, then sum up:

(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58

We match the 1st members (1 and 7), multiply them, likewise for the 2nd members (2 and 9) and the 3rd members (3 and 11), and finally sum them up.

Lets see for another row ..

(1, 2, 3) • (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64

 

C program for Matrix Multiplication | C code champ

 

We can do the same thing for the 2nd row and 1st column:

(4, 5, 6) • (7, 9, 11) = 4×7 + 5×9 + 6×11 = 139

And for the 2nd row and 2nd column:

(4, 5, 6) • (8, 10, 12) = 4×8 + 5×10 + 6×12 = 154

So this is the resultant matrix obtained after multiplication :

C program for Matrix Multiplication | C code champ

 

Now lets see how we are going to implement above logic of multiplying matrices in C. The below program accepts two things from user:

1. Order of Matrices

2. Content of matrices

Note : If the order of input matrices is incorrect then program will throw an error saying ” Matrices Orders are incorrect, they cannot be multiplied”.

If the order is correct then program will ask you to enter the elements of a matrix, say you defined order as [3,3] then you need to enter 9 elements.

Note : Enter elements as per order say for [3,3] enter elements like below:

7 6 1

4 2 1

1 5 7

That’s all now let see the C program.

C program for Matrix Multiplication using arrays :

#include <stdio.h>
#include <conio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
  printf("***************************************************\n");
  printf("**************written by C code champ**************\n");
  printf("***************************************************\n\n\n");
  printf("Enter the number of rows and columns of first matrix : \n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);

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

  if ( n != p )
    printf("\t \t Matrices Orders are incorrect \n \t \t They cannot be multiplied.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

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

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);

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

 

I hope you all have enjoyed the code and concept of multiplication of . If you face any issues regarding code then feel free to contact us in form of comments.

 

1 COMMENT

  1. Hi my family member! I wish to say that this post is amazing, great written and include almost all vital infos. I would like to look extra posts like this .

LEAVE A REPLY