C program to Multiply two Matrices by Passing Matrix to a Function
C program to Multiply two Matrices by Passing the Matrix to a Function.
In this article, you will learn how to multiply the two matrices by passing the matrices into the functions.
Example
Enter No. of Row & Column:
2
4
Enter the values of the first matrix - [2 x 4]
Enter value - M1[0][0]: 1
Enter value - M1[0][1]: 2
Enter value - M1[0][2]: 3
Enter value - M1[0][3]: 4
Enter value - M1[1][0]: 5
Enter value - M1[1][1]: 6
Enter value - M1[1][2]: 7
Enter value - M1[1][3]: 8
Enter the values of the second matrix - [4 x 2]
Enter value - M2[0][0]: 0
Enter value - M2[0][1]: 9
Enter value - M2[1][0]: 8
Enter value - M2[1][1]: 7
Enter value - M2[2][0]: 6
Enter value - M2[2][1]: 5
Enter value - M2[3][0]: 4
Enter value - M2[3][1]: 3
Multiplication of two matrices matrix_1 & matrix_2
50 50
122 146
You should have knowledge of the following topics in c programming to understand this program:
- C Functions
- C For Loop
- C Arrays
Source
// C program to Multiply two Matrices by Passing Matrix to a Function
#include <stdio.h>
void Input_Matrices(int matrix_1[100][100], int matrix_2[100][100], int rows_1, int col_1, int rows_2, int col_2);
void Initialize_Matrix(int matrix_mul[100][100], int rows_1, int rows_2);
void Multiply_Matrices(int matrix_1[100][100], int matrix_2[100][100], int matrix_mul[100][100], int rows_1, int col_1, int rows_2, int col_2);
void Display_Matrices(int matrix_mul[100][100], int rows_1, int col_2);
// It's the main function of the program
int main() {
int rows_1, col_1;
printf("Enter No. of Row & Column:\n");
scanf("%d %d", &rows_1, &col_1);
// Transpose the dimension of the first matrix into the second matrix
// M1[2 x 3] can multiply with M2[3 x 2]
int rows_2 = col_1, col_2 = rows_1;
int matrix_1[rows_1][col_1];
int matrix_2[rows_2][col_2];
int matrix_mul[rows_1][col_2];
// Step-1 Call the Input_Matrices() function to enter the values of first & second matrices
Input_Matrices(matrix_1, matrix_2, rows_1, col_1, rows_2, col_2);
// Step-2 Call the Initialize_Matrix() function to initialize the multiplication matrix
Initialize_Matrix(matrix_mul, rows_1, rows_2);
// Step-3 Call the Multiply_Matrices() function to multiply the matrices M1 and M2
Multiply_Matrices(matrix_1, matrix_2, matrix_mul, rows_1, col_1, rows_2, col_2);
// Step-4 Call the Display_Matrices() function to print the final ouput matrix
Display_Matrices(matrix_mul, rows_1, col_2);
return 0;
}
// Function - Take input values of the two matrices
void Input_Matrices(int matrix_1[100][100], int matrix_2[100][100], int rows_1, int col_1, int rows_2, int col_2) {
// Enter the values of first matrix
printf("\nEnter the values of the first matrix - [%d x %d]\n", rows_1, col_1);
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < col_1; j++) {
printf("Enter value - M1[%d][%d]: ", i, j);
scanf("%d", &matrix_1[i][j]);
}
}
// Enter the values of second matrix
printf("\nEnter the values of the second matrix - [%d x %d]\n", rows_2, col_2);
for (int i = 0; i < rows_2; i++) {
for (int j = 0; j < col_2; j++) {
printf("Enter value - M2[%d][%d]: ", i, j);
scanf("%d", &matrix_2[i][j]);
}
}
}
// Function - To initialize the multiplication matrix with 0 values
void Initialize_Matrix(int matrix_mul[100][100], int rows_1, int rows_2) {
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < rows_2; j++) {
matrix_mul[i][j] = 0;
}
}
}
// Function - To multiply the two matrices
void Multiply_Matrices(int matrix_1[100][100], int matrix_2[100][100], int matrix_mul[100][100], int rows_1, int col_1, int rows_2, int col_2) {
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < col_2; j++) {
for (int k = 0; k < col_1; k++) {
matrix_mul[i][j] += matrix_1[i][k] * matrix_2[k][j];
}
}
}
}
// Function - To display the result matrices
void Display_Matrices(int matrix_mul[100][100], int rows_1, int col_2) {
printf("\nMultiplication of two matrices matrix_1 & matrix_2\n");
for (int i = 0; i < rows_1; i++) {
for (int j = 0; j < col_2; j++) {
printf("%d\t", matrix_mul[i][j]);
}
printf("\n");
}
}
Output
Enter No. of Row & Column:
2
2
Enter the values of the first matrix - [2 x 2]
Enter value - M1[0][0]: 1
Enter value - M1[0][1]: 1
Enter value - M1[1][0]: 2
Enter value - M1[1][1]: 2
Enter the values of the second matrix - [2 x 2]
Enter value - M2[0][0]: 3
Enter value - M2[0][1]: 3
Enter value - M2[1][0]: 4
Enter value - M2[1][1]: 4
Multiplication of two matrices matrix_1 & matrix_2
7 7
14 14