C Program To Perform Matrix Multiplication

Write a Program To Perform Matrix Multiplication in C.

The Program takes 2 Matrix as it’s input, MatrixA and MatrixB.

First the Program ask user to enter the Number of Rows and Number of Columns of the Matrix.

After that, the user can input the matrix (row – wise).

If the Order of MatrixA is n x m then the order of MatrixB should be in form m x p, where n,m,p are integers.

Matrix Multiplication can be implemented in 2 ways:

  1. Without Recursion (Iteratively)
  2. With Recursion (Recursively)

C Program To Perform Matrix Multiplication

#include<stdio.h>

// The function multiply matrixA and matrixB
// and stores the result of multiplication in matrixC
// rA: number of rows in matrixA
// cA: number of columns in matrixA
// rB: number of rows in matrixB
// cB: number of columns in matrixB
void MatrixMultiplication(int matrixA[][50], int rA, int cA, int matrixB[][50], int rB, int cB, int matrixC[][50]) {

	int i, j, k, sum = 0;

	if (cA != rB) {
		printf("Order of Input Matricis is Invalid");
		return;
	}

	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cB; ++j) {
			sum = 0;
			for (k = 0; k < rA; ++k) {
				sum += matrixA[i][k] * matrixB[k][j];
			}
			matrixC[i][j] = sum;
		}
	}

}

int main() {

	int i, j;
	int rA, cA, rB, cB;
	int matrixA[50][50], matrixB[50][50], matrixC[50][50];


	// Enter Matrix A
	printf("Enter Number of Rows in MatrixA: ");
	scanf("%d", &rA);
	printf("Enter Number of columns in MatrixA: ");
	scanf("%d", &cA);

	printf("Enter MatrixA: \n");
	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cA; ++j) {
			scanf("%d", &matrixA[i][j]);
		}
	}

	// Enter Matrix B
	printf("Enter Number of Rows in MatrixB: ");
	scanf("%d", &rB);
	printf("Enter Number of columns in MatrixB: ");
	scanf("%d", &cB);

	printf("Enter MatrixB: \n");
	for (i = 0; i < rB; ++i) {
		for (j = 0; j < cB; ++j) {
			scanf("%d", &matrixB[i][j]);
		}
	}

	// the function multiply matrixA and matrixB and stores
	// the result in matrixC
	// the order of matrixC is rA X cB
	MatrixMultiplication(matrixA, rA, cA, matrixB, rB, cB, matrixC);

	// Printing MatrixC
	printf("\nThe Matrix Multiplication of MatrixA and MatrixB: \n");
	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cB; ++j) {
			printf("%d ", matrixC[i][j]);
		}
		printf("\n");
	}

}

Output

matrix multiplication recursion output

C Program To Perform Matrix Multiplication using Recursion

The previous method uses 3 loops.

We can eliminate the outer loop by using recursion.

In this method, we pass an additional parameter i to MatrixMultiplication().

Each recursive call calculates the ith row of resultant matrix ( MatrixC ).

#include<stdio.h>

// The function multiply matrixA and matrixB
// and stores the result of multiplication in matrixC
// each recursive call calcualtes
// all the elements of ith row of MatrixC
void MatrixMultiplication(int matrixA[][50], int rA, int cA, int matrixB[][50], int rB, int cB, int matrixC[][50], int i) {

	int j, k, sum;

	if (cA != rB) {
		printf("Order of Input Matrices is Invalid");
		return;
	}

	if (i >= rA)
		return;

	for (j = 0; j < cB; ++j) {
		sum = 0;
		for (k = 0; k < rA; ++k) {
			sum += matrixA[i][k] * matrixB[k][j];
		}
		matrixC[i][j] = sum;
	}

	MatrixMultiplication(matrixA, rA, cA, matrixB, rB, cB, matrixC, i + 1);

}

int main() {

	int i, j;
	int rA, cA, rB, cB;
	int matrixA[50][50], matrixB[50][50], matrixC[50][50];


	// Enter Matrix A
	printf("Enter Number of Rows in MatrixA: ");
	scanf("%d", &rA);
	printf("Enter Number of columns in MatrixA: ");
	scanf("%d", &cA);

	printf("Enter MatrixA: \n");
	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cA; ++j) {
			scanf("%d", &matrixA[i][j]);
		}
	}

	// Enter Matrix B
	printf("Enter Number of Rows in MatrixB: ");
	scanf("%d", &rB);
	printf("Enter Number of columns in MatrixB: ");
	scanf("%d", &cB);

	printf("Enter MatrixB: \n");
	for (i = 0; i < rB; ++i) {
		for (j = 0; j < cB; ++j) {
			scanf("%d", &matrixB[i][j]);
		}
	}

	// the function multiply matrixA and matrixB and stores
	// the result in matrixC
	// the order of matrixC is rA X cB
	MatrixMultiplication(matrixA, rA, cA, matrixB, rB, cB, matrixC, 0);

	// Printing MatrixC
	printf("\nThe Matrix Multiplication of MatrixA and MatrixB: \n");
	for (i = 0; i < rA; ++i) {
		for (j = 0; j < cB; ++j) {
			printf("%d ", matrixC[i][j]);
		}
		printf("\n");
	}

}

Output

matrix multiplication recursion output

What to Study Next?

  1. Program to add 2 Matrices.

References

Matrix Multiplication Wiki

Leave Comment if you face any problem

Leave a Comment

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