C Program to find the product of two matrices


February 9, 2023, Learn eTutorial
1340

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How are matrices multiplied in C?

In this c program, we need to calculate the product of two matrices. Matrix multiplication is a double operation that takes a couple of matrices and produces another matrix. Matrices are arrays of numbers, so there is no exceptional approach to characterize the multiplication of arrays. Thus, all in all, the expression "matrix multiplication" allows various diverse methods to duplicate matrices.

The key highlights of any matrix multiplication include the number of rows and columns the first matrix has (called the "size," "order" or "measurement"), and determining how the passages of the lattices produce the new matrix. That means the number of rows in the first matrix will be equal to the number of columns in the second matrix. On the off chance that A is 'n * m' matrix and B is 'm * p' matrix, their matrix product ab is "n * p" matrix.

How to implement matrix multiplication using functions?

In this c program, what we are doing g is to accept two matrices from the user. And display the matrices. Now we open a nested for loop which is used for multiplication of the matrices using the formula C[i][j] = C[i][j] + A[i][k] * B[k][j] and finally display the result as output Matrix.

The main logic of this program is to declare three matrices a, m, c. Then we have to read the order of the matrix from the user and store it into the variable m and n. Then we will accept the matrix a from the user by calling the read matrix () function. Then we display matrix a by calling the function print matrix().

Then we will accept Matrix B from the user by calling the read Matrix () function. Then we display matrix B by calling the function print Matrix(). Then we will call the Product Matrix() to find out the Product of Matrix A and B. The Product of Matrix A and B will be stored in Matrix C. Now we can print Matrix C as the Product Matrix by calling the function print Matrix().

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare three Matrices A[10][10],B[10][10],c[10][10] and two integer variables M and N.

STEP 3: Declare three functions read Matrix(), print Matrix(), product Matrix().

STEP 4: Accept the order of the Matrix from the user into M and N.

STEP 5: Accept the Matrix A from the user by calling the function read Matrix ().

STEP 6: Print the Matrix A by calling the print Matrix() function.

STEP 7: Accept Matrix B from the user by calling the function read Matrix ().

STEP 8: Print Matrix B by calling the print Matrix() function.

STEP 9: Call the function product Matrix(A, B, C, M, N) to find out the product of Matrix A and B and to store the result in Matrix C.

STEP 10: Print Matrix C by calling the function print Matrix().

Function void readMatrix(int arr[][MAXCOLS], int M, int N)

STEP 1: Declare the variables i,j and set i=0.

STEP 2: Using a for loop check i

STEP 3: Initialize j=0 and check j

STEP 4: Increment j by one and repeat step 3.

STEP 5: Increment 'i' by one and repeat step 2.

Function printMatrix(int arr[][MAXCOLS], int M, int N)

STEP 1: Declare the variables 'i, j', and set i=0.

STEP 2: Using a for loop check 'i'

STEP 3: Initialize 'j=0' and check 'j'

STEP 4: Increment 'j' by one and repeat step 3.

STEP 5: Increment 'i' by one and repeat step 2

Function productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],int M, int N))

STEP 1: Declare the variables 'i, j, k',and set i=0.

STEP 2: Using a for loop check 'i'

STEP 3: Initialize j=0.

STEP 4: Check j

STEP 5: Set C[i][j] = 0.

STEP 6: Initialize k=0 and check k

STEP 7: C[i][j] = C[i][j] + A[i][k] * B[k][j]

STEP 8: Increment k by 1 and repeat step 7 until k reaches N.

STEP 9: Increment j by 1 and repeat step 4 until k reaches 'N'

STEP 10: Increment 'i' by 1 and repeat step 2 until 'i' reaches 'N'

C Source Code

                                          #include <stdio.h>

#define MAXROWS 10
#define MAXCOLS 10

void main() {
  int A[MAXROWS][MAXCOLS], B[MAXROWS][MAXCOLS], C[MAXROWS][MAXCOLS];
  int M, N;
  void readMatrix(int arr[][MAXCOLS], int M, int N); /*Function declarations*/
  void printMatrix(int arr[][MAXCOLS], int M, int N);
  void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
    int M, int N);
  printf("Enter the value of M and N\n");
  scanf("%d %d", & M, & N);
  printf("Enter matrix A\n");
  readMatrix(A, M, N);
  printf("Matrix A\n");
  printMatrix(A, M, N);
  printf("Enter matrix B\n");
  readMatrix(B, M, N);
  printf("Matrix B\n");
  printMatrix(B, M, N);
  productMatrix(A, B, C, M, N);
  printf("The product matrix is\n");
  printMatrix(C, M, N);
}
/*Input matrix A*/
void readMatrix(int arr[][MAXCOLS], int M, int N) {
  int i, j;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      scanf("%d", & arr[i][j]);
    }
  }
}
void printMatrix(int arr[][MAXCOLS], int M, int N) {
  int i, j;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      printf("%d", arr[i][j]);
    }
    printf("\n");
  }
}
void productMatrix(int A[][MAXCOLS], int B[][MAXCOLS], int C[][MAXCOLS],
  /* Multiplication of matrices */
  int M, int N) {
  int i, j, k;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      C[i][j] = 0;
      for (k = 0; k < N; k++) {
        C[i][j] = C[i][j] + A[i][k] * B[k][j];
      }
    }
  }
}
                                      

OUTPUT

Enter the value of M and N
3 3

Enter matrix A
1 1 1
2 2 2
3 3 3

Matrix A
1  1  1
2  2  2
3  3  3

Enter matrix B
1 2 3
4 5 6
7 8 9

Matrix B
1  2  3
4  5  6
7  8  9

The product matrix is
12 15 18
24 30 36
36 45 54