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

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

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.

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.

`/*`

`* Develop functions to read a matrix, display a matrix and compute`

`* product of two matrices.`

`* Use these functions to read two MxN matrices and compute their`

`* product & display the result`

`*/`

`#include <stdio.h>`

`#define MAXROWS 10`

`#define MAXCOLS 10`

void readMatrix(int arr[][MAXCOLS], int m, int n);

void printMatrix(int arr[][MAXCOLS], int m, int n);

void productMatrix(int array1[][MAXCOLS], int array2[][MAXCOLS],

int array3[][MAXCOLS], int m, int n);

void main()

`{`

int array1[MAXROWS][MAXCOLS], array2[MAXROWS][MAXCOLS],

array3[MAXROWS][MAXCOLS];

int m, n;

printf("Enter the value of m and n \n");

scanf("%d %d", &m, &n);

printf("Enter Matrix array1 \n");

readMatrix(array1, m, n);

printf("Matrix array1 \n");

printMatrix(array1, m, n);

printf("Enter Matrix array2 \n");

readMatrix(array2, m, n);

printf("Matrix B \n");

printMatrix(array2, m, n);

productMatrix(array1, array2, array3, m, n);

printf("The product matrix is \n");

printMatrix(array3, m, n);

`}`

`/* Input Matrix array1 */`

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("%3d", arr[i][j]);

`}`

printf("\n");

`}`

`}`

`/* Multiplication of matrices */`

void productMatrix(int array1[][MAXCOLS], int array2[][MAXCOLS],

int array3[][MAXCOLS], int m, int n)

`{`

int i, j, k;

for (i = 0; i < m; i++)

`{`

for (j = 0; j < n; j++)

`{`

array3[i][j] = 0;

for (k = 0; k < n; k++)

`{`

array3[i][j] = array3[i][j] + array1[i][k] * array2[k][j];

`}`

`}`

`}`

`}`

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.

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.

**Related Posts:**

- Apply for Computer Science Internship
- Apply for C Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Check Computer Science Books