Matrix Multiplication in C Program

In this post we will see Matrix Multiplication in C programming language. We will see 2×2 and 3×3 matrix multiplication in C. What is matrix multiplication in C algorithm? How to write program for matrix multiplication in C using function.

Matrix Multiplication

Let A be an m×k matrix and B be a k ×n matrix. The product of A and B, denoted by AB, is the m × n matrix with its (i, j )th entry equal to the sum of the products of the corresponding elements from the ith row of A and the jth column of B. In other words, if AB =[cij], then cij = ai1b1j + ai2b2j +···+aikbkj.

Condition for the Matrix multiplication:- The product of two matrices is not defined when the number of columns in the first matrix and the number of rows in the second matrix are not the same.

Example of Matrix Multiplication,

Matrix A =
a11 a12
a21 a22

Matrix B =
b11 b12
b21 b22

The product of A and B is denoted as AB and it can be calculated as, AB=
(a11*b11 + a12b21) (a11*b12 + a12*b22)
(a21*b11 + a22b21) (a21*b12 + a22*b22)

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Example using 2×2 matrices,

A = 
1 3
7 5
B = 
6 8
4 2
C = 
1*6+3*4  1*8+3*2
7*6+5*4  7*8+5*2

=
18 14
62 66

2×2 Matrix Multiplication in C

It is a simple C program to find the 2×2 matrix multiplication in C. The values for the matrix is given inside the program.

#include <stdio.h>
int main()
{
  // matrix A and B
  int a[2][2] = {{5,6},{9,8}};
  int b[2][2] = {{1,3},{3,2}};

  // declare matix C
  int c[2][2];

  // multiply both matrix A and B
  for(int i=0; i < 2; i++)
  {
    for(int j=0; j < 2; j++)
    {
      // assign 0
      c[i][j] = 0;
      for (int k = 0; k < 2; k++)
      {
        // find product
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }

  // display matrix
  printf("Resultant Matrix: \n");
  for(int i=0; i<2; i++)
  {
    for(int j=0; j<2; j++)
    {
      printf("%d ", c[i][j]);
    }
    printf("\n"); // new line
  }

  return 0;
}

Output:-

Resultant Matrix:
23 27
33 43

Similar to the above program, you can also find the 3×3 matrix multiplication in C. Replace 2 with 3 in the above program.

Matrix Multiplication in C using Function

Square matrix:- The matrix having equal row and column size is called square matrix. Example of square matrix:- 2×2, 3×3, 4×4, 5×5, and e.t.c. When matrix doesn’t have equal size of row and column then those matrices are called non-square matrix. Example of non-square matrix:- 2×3, 2×4, 3×2, 3×4, and e.t.c.

Since we are working with square matrix and both matrices are of same order therefore matrix multiplication is always possible, and no need to check whether matrix multiplication is possible for them or not.

C Function to Find Multiplication for Square Matrix

// function to multiply two square matrix
void multiplyMatrix(int a[10][10], int b[10][10],
                       int c[10][10], int size)
{
  for(int i=0; i<size; i++)
  {
    for(int j=0; j<size; j++)
    {
      // assign 0
      c[i][j] = 0;
      for (int k = 0; k < size; k++)
      {
        // find product
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
}

Time Complexity:- O(N3)

Why it need three for loops? For accessing all the elements of any matrix we need two for loop. But for finding the product it requires one additional for loop. Therefore the time-complexity is O(N3).

C Program to Find Multiplication of Two Square Matrix

In the previous program both matrices A and B were initialized within the program, now let us see an another C program for by taking input value from user.

#include <stdio.h>
// function to multiply two square matrix
void multiplyMatrix(int a[10][10], int b[10][10], 
              int c[10][10], int size)
{
  for(int i=0; i<size; i++)
  {
    for(int j=0; j<size; j++)
    {
      // assign 0
      c[i][j] = 0;
      for (int k = 0; k < size; k++)
      {
        // find product
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
}
// function to read square matrix
void readMatrix(int matrix[10][10], int size)
{
  for (int i = 0; i < size; i++)
  {
    for (int j = 0; j < size; j++)
    {
      scanf("%d", &matrix[i][j]);
    }
  }
}
// function to display square matrix
void displayMatrix(int matrix[10][10], int size)
{
  for (int i = 0; i < size; ++i)
  {
    for (int j = 0; j < size; ++j)
    {
      printf("%d ", matrix[i][j]);
    }
    printf("\n"); // new line
  }
}
// main function
int main()
{

  // declare matrix matrix A, B, & C
  int a[10][10]; // first matrix
  int b[10][10]; // second matrix
  int c[10][10]; // resultant matrix

  // read the size of matrices
  int size;
  printf("Enter Order of Both Matrix,\n");
  printf("Enter 2 for 2x2, 3 for 3x3 and e.t.c.: ");
  scanf("%d", &size);

  // read matrix A and B
  printf("Enter Matrix-1 Elements: \n");
  readMatrix(a, size);
  printf("Enter Matrix-2 Elements: \n");
  readMatrix(b, size);

  // multiply both matrix A and B
  multiplyMatrix(a, b, c, size);

  // display resultant matrix
  printf("Resultant Matrix: \n");
  displayMatrix(c, size);

  return 0;
}

Output:-

Enter Order of Both Matrix,
Enter 2 for 2×2, 3 for 3×3 and e.t.c.: 2
Enter Matrix-1 Elements:
1 3
7 5
Enter Matrix-2 Elements:
6 8
4 2
Matrix-1:
1 3
7 5
Matrix-2:
6 8
4 2
Resultant Matrix:
18 14
62 66

Enter Order of Both Matrix,
Enter 2 for 2×2, 3 for 3×3 and e.t.c.: 3
Enter Matrix-1 Elements:
1 2 3
4 5 6
7 8 9
Enter Matrix-2 Elements:
5 6 7
8 9 10
3 1 2
Matrix-1:
1 2 3
4 5 6
7 8 9
Matrix-2:
5 6 7
8 9 10
3 1 2
Resultant Matrix:
30 27 33
78 75 90
126 123 147

Program for Both Square and Non-Square matrix 

In the above examples, we were assuming that matrix is a square matrix, therefore those functions will work only for square matrix. For non square matrix, row and column sizes will vary. We have write different logic for them, and we must check matrix multiplication is possible between them or not Below C program works for both square and non-square matrix.

Multiplication in C for both square and non-square matrix

#include <stdio.h>
#include <stdlib.h>
// function to multiply two square matrix
void multiplyMatrix(int a[10][10], int b[10][10], 
        int c[10][10], int row3, int column3, int column1)
{
  // outer for loop upto row3
  for(int i=0; i < row3; i++)
  {
    // inner-1 for loop upto column3
    for(int j=0; j < column3; j++)
    {
      // assign 0
      c[i][j] = 0;

      // inner-2 for loop upto column2
      for (int k = 0; k < column1; k++)
      {
        // find product
        c[i][j] += (a[i][k] * b[k][j]);
      }
    }
  }
}
// function to read matrix
void readMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      scanf("%d", &matrix[i][j]);
    }
  }
}
// function to display matrix
void displayMatrix(int matrix[10][10], int row, int column)
{
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < column; ++j)
    {
      printf("%d ", matrix[i][j]);
    }
    printf("\n"); // new line
  }
}
// main function
int main()
{

  // declare matrix matrix A, B, & C
  int a[10][10]; // first matrix
  int b[10][10]; // second matrix
  int c[10][10]; // resultant matrix

  // read the size of matrix-1
  int row1, column1;
  printf("Enter row and column for matrix-1: ");
  scanf("%d %d", &row1, &column1);
  // read matrix A 
  printf("Enter Matrix-1 Elements: \n");
  readMatrix(a, row1, column1);

  // read the size of matrix-2
  int row2, column2;
  printf("Enter row and column for matrix-2: ");
  scanf("%d %d", &row2, &column2);
  // read matrix B
  printf("Enter Matrix-2 Elements: \n");
  readMatrix(b, row2, column2);

  // check matrix multiplication is possible or not
  if (column1 != row2) {
    printf("Matrix Multiplication is not possible.\n");
    printf("Column size of first matrix and \n");
    printf("row size of second matrix should be the same. \n");
    exit(0); // stop execution
  }

  // assign row and column for matrix-3
  /** Note:-The product of matrices 3x2 and 2x4
    * will become matrix of size 3x4 
    **/
  int row3 = row1;
  int column3 = column2;

  // multiply both matrix A and B
  /** Pass matrix1, Matrix2, Matrix3, 
    * row3, column3, and column1.
    * 
    * In place of column1, you can also
    * pass row2 because both are same, else
    * matrix multiplication is not possible.
    */
  multiplyMatrix(a, b, c, row3, column3, column1);

  // display resultant matrix
  printf("Matrix-1: \n");
  displayMatrix(a, row1, column1);
  printf("Matrix-2: \n");
  displayMatrix(b, row2, column2);
  printf("Resultant Matrix: \n");
  displayMatrix(c, row3, column3);

  return 0;
}

Output:-

Enter row and column for matrix-1: 3 2
Enter Matrix-1 Elements:
1 2
3 4
5 6
Enter row and column for matrix-2: 2 4
Enter Matrix-2 Elements:
6 7 8 9
1 2 3 4
Matrix-1:
1 2
3 4
5 6
Matrix-2:
6 7 8 9
1 2 3 4
Resultant Matrix:
8 11 14 17
22 29 36 43
36 47 58 69

Enter row and column for matrix-1: 3 1
Enter Matrix-1 Elements:
4
5
6
Enter row and column for matrix-2: 2 2
Enter Matrix-2 Elements:
1 2
3 4
Matrix Multiplication is not possible.
Column size of first matrix and
row size of second matrix should be the same.

See More:- Strassen’s Matrix Multiplication Algorithm, Divide and Conquer Method of Matrix Multiplication

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *