C Online Compiler
Example: C program to Multiply two Matrices by Passing Matrix to a Function
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// 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"); } }
2 2 1 1 2 2 3 3 4 4
Output
Clear
ADVERTISEMENTS