#include <stdio.h>
#define MAX 10 // Maximum size of the matrices
void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
int main() {
int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];
int rowFirst, columnFirst, rowSecond, columnSecond;
// Input for the first matrix
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &rowFirst, &columnFirst);
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnFirst; j++) {
scanf("%d", &first[i][j]);
}
}
// Input for the second matrix
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &rowSecond, &columnSecond);
// Check if multiplication is possible
if (columnFirst != rowSecond) {
printf("Matrix multiplication not possible.\n");
return 0;
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rowSecond; i++) {
for (int j = 0; j < columnSecond; j++) {
scanf("%d", &second[i][j]);
}
}
// Multiply matrices
multiplyMatrices(first, second, result, rowFirst, columnFirst, rowSecond, columnSecond);
// Display the result
printf("Resultant Matrix:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
// Initialize the result matrix
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
result[i][j] = 0; // Set initial value to zero
}
}
// Perform multiplication
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
for (int k = 0; k < columnFirst; k++) {
result[i][j] += first[i][k] * second[k][j]; // Multiply and accumulate
}
}
}
}
Header File Inclusion
#include <stdio.h>
This includes the standard input-output library needed for functions like
printf
andscanf
Macro Definition
#define MAX 10
This defines a constant
MAX
for the maximum size of the matrices. You can adjust this value as needed.Function Declaration
void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
This declares the function that will perform the multiplication of two matrices.
Main Function
Matrix Declaration and Initialization
int first[MAX][MAX], second[MAX][MAX], result[MAX][MAX];
Three matrices are declared:
first
,second
, andresult
to store the multiplication output.Input for First Matrix
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &rowFirst, &columnFirst);
The user is prompted to enter the dimensions and elements of the first matrix.
Input for Second Matrix
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &rowSecond, &columnSecond);
Similar input is taken for the second matrix.
Matrix Multiplication Feasibility Check
if (columnFirst != rowSecond) {
printf("Matrix multiplication not possible.\n");
return 0;
}
This checks if the multiplication is possible. The number of columns in the first matrix must equal the number of rows in the second.
Calling the Multiplication Function
multiplyMatrices(first, second, result, rowFirst, columnFirst, rowSecond, columnSecond);
This calls the
multiplyMatrices
function to perform the multiplication.Displaying the Result
printf("Resultant Matrix:\n");
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
Finally, the resulting matrix is printed.
Matrix Multiplication Function
void multiplyMatrices(int first[MAX][MAX], int second[MAX][MAX], int result[MAX][MAX], int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
// Initialize the result matrix
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
result[i][j] = 0;
}
}// Perform multiplication
for (int i = 0; i < rowFirst; i++) {
for (int j = 0; j < columnSecond; j++) {
for (int k = 0; k < columnFirst; k++) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}Initialization of Result Matrix Each element of the
result
matrix is initialized to zero.Nested Loops for Multiplication The outer two loops iterate through each cell of the resulting matrix. The innermost loop performs the dot product of the corresponding row of the first matrix and the column of the second matrix.
Example Input and Output
Input:
Enter rows and columns for first matrix: 2 3
Enter elements of the first matrix:
1 2 3
4 5 6
Enter rows and columns for second matrix: 3 2
Enter elements of the second matrix:
7 8
9 10
11 12
Output:
Resultant Matrix:
58 64
139 154