C Program to Find Product of Two Matrices

This is a program to compute the product of two matrices.

Problem Description

This C Program computes the product of two matrices. This program accepts the 2 matrices and then find the product of 2 matrices.

Problem Solution

1. Create three 2D arrays, third matrix will store the product of first two matrices.
2. Read all the elements of first two arrays using scanf() function under nested for loop.
3. Make a function for evaluating product of the matrix.
4. Inside this function, three iterators are created. First make sure that number of columns in first matrix equals number of rows in the second row.
5. Then multiply the first element in the first row(of first matrix) by the first element of the first column(in second matrix), the second element of the first row by the second element of the first column, and the third element in the first row by the third element in the first column. Then, add these products to find the dot product.
6. Continue this multiplication of first row elements(of first matrix) with second columns elements(of second matrix), then adding the products. In this way third matrix will get filled.
7. The third matrix will have size of number of rows of first matrix * number of columns in second matrix.

Program/Source Code

Here is source code of the C program to compute the product of two matrices. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2.     /*
  3.      * Develop functions to read a matrix, display a matrix and compute
  4.      * product of two matrices.
  5.      * Use these functions to read two MxN matrices and compute their
  6.      * product & display the result
  7.      */
  8.  
  9.     #include <stdio.h>
  10.     #define MAXROWS 10
  11.     #define MAXCOLS 10
  12.  
  13.     void readMatrix(int arr[][MAXCOLS], int m, int n);
  14.     void printMatrix(int arr[][MAXCOLS], int m, int n);
  15.     void productMatrix(int array1[][MAXCOLS], int array2[][MAXCOLS],
  16.     int array3[][MAXCOLS], int m, int n);
  17.  
  18.     void main()
  19.     {
  20.  
  21.         int array1[MAXROWS][MAXCOLS], array2[MAXROWS][MAXCOLS],
  22.         array3[MAXROWS][MAXCOLS];
  23.         int m, n;
  24.  
  25.         printf("Enter the value of m and n \n");
  26.         scanf("%d %d", &m, &n);
  27.  
  28.         printf("Enter Matrix array1 \n");
  29.         readMatrix(array1, m, n);
  30.         printf("Matrix array1 \n");
  31.         printMatrix(array1, m, n);
  32.  
  33.         printf("Enter Matrix array2 \n");
  34.         readMatrix(array2, m, n);
  35.         printf("Matrix B \n");
  36.         printMatrix(array2, m, n);
  37.  
  38.         productMatrix(array1, array2, array3, m, n);
  39.         printf("The product matrix is \n");
  40.         printMatrix(array3, m, n);
  41.  
  42.     }
  43.  
  44.     /*  Input Matrix array1 */
  45.  
  46.     void readMatrix(int arr[][MAXCOLS], int m, int n)
  47.     {
  48.  
  49.         int i, j;
  50.         for (i = 0; i < m; i++) 
  51.         {
  52.             for (j = 0; j < n; j++) 
  53.             {
  54.                 scanf("%d", &arr[i][j]);
  55.             }
  56.         }
  57.     }
  58.  
  59.     void printMatrix(int arr[][MAXCOLS], int m, int n)
  60.     {
  61.  
  62.         int i, j;
  63.         for (i = 0; i < m; i++) 
  64.         {
  65.             for (j = 0; j < n; j++)
  66.             {
  67.                 printf("%3d", arr[i][j]);
  68.             }
  69.             printf("\n");
  70.         }
  71.     }
  72.  
  73.     /*  Multiplication of matrices */
  74.  
  75.     void productMatrix(int array1[][MAXCOLS], int array2[][MAXCOLS],
  76.     int array3[][MAXCOLS], int m, int n)
  77.     {
  78.  
  79.         int i, j, k;
  80.         for (i = 0; i < m; i++) 
  81.         {
  82.             for (j = 0; j < n; j++) 
  83.             {
  84.                 array3[i][j] = 0;
  85.                 for (k = 0; k < n; k++) 
  86.                 {
  87.                     array3[i][j] = array3[i][j] + array1[i][k] * array2[k][j];
  88.                 }
  89.             }
  90.         }
  91.     }
Program Explanation

1. Define three 2D arrays (i.e matrices) of some fixed size.
2. The third matrix will store the multiplication result of first two matrices. The third matrix will have same number of rows as that of first matrix and same number of columns as that of second matrix.
3. Read all the elements of first two matrix using scanf() function inside nested for loop in a separate function named readMatrix().
4. Then to evaluate the product, make a function called productMatrix().
5. Inside it, make three loops with three iterators(i , j and k) , each loop under another.
6. Inside these loops the same code of multiplication will be written.
7. Code consists of multiplying the elements of first row of first matrix to elements of the first column of the second matrix respectively. Add all these products to get a single value. Now this is the first element of the third matrix.
8. To get second element, multiply first row of first matrix with second column of second matrix respectively and add. Continue with the pattern to get all the elements of the third array.
9. Print the product matrix.

advertisement
advertisement
Runtime Test Cases
Enter the value of m and n
3 3
Enter matrix array1
4 5 6
1 2 3
3 7 8
Matrix array1
  4  5  6
  1  2  3
  3  7  8
Enter matrix array2
5 6 9
8 5 3
2 9 1
Matrix array2
  5  6  9
  8  5  3
  2  9  1
The product matrix is
 72103 57
 27 43 18
 87125 56

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.