0% found this document useful (0 votes)
6 views3 pages

Matrix Programming

This C program multiplies multiple matrices input by the user, allowing for 2 to 4 matrices. It includes functions for inputting matrix values, printing the resulting matrix, and performing matrix multiplication. The program checks for valid dimensions for multiplication and outputs the final result.

Uploaded by

X Y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Matrix Programming

This C program multiplies multiple matrices input by the user, allowing for 2 to 4 matrices. It includes functions for inputting matrix values, printing the resulting matrix, and performing matrix multiplication. The program checks for valid dimensions for multiplication and outputs the final result.

Uploaded by

X Y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

void inputValues(int row, int col, int matrix[100][100]) {


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("Enter element %d %d - ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}

void printMatrix(int row, int col, int resultMatrix[100][100]) {


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("%d ", resultMatrix[i][j]);
}
printf("\n");
}
printf("\n");
}

void multiplyMatrix(int row1, int col1, int col2, int A1[100][100], int A2[100][100], int resultMatrix[100]
[100]) {
int sum;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
sum = 0;
for (int k = 0; k < col1; k++)
sum = sum + A1[i][k] * A2[k][j];
resultMatrix[i][j] = sum;
}
}
}
int main() {
int matrices[4][100][100];
int rows[4], cols[4];
int numMatrices;

printf("Enter the number of matrices (2, 3, or 4): ");


scanf("%d", &numMatrices);

if (numMatrices < 2 || numMatrices > 4) {


printf("Invalid number of matrices. Please enter 2, 3, or 4.\n");
return 1;
}

for (int i = 0; i < numMatrices; i++) {


printf("Enter the size of Matrix %d:\n", i + 1);
printf("Enter no. of rows : ");
scanf("%d", &rows[i]);
printf("Enter no. of columns : ");
scanf("%d", &cols[i]);
inputValues(rows[i], cols[i], matrices[i]);
}

printf("\nMultiplying the matrices...\n");


int resultMatrix[100][100];
for (int i = 0; i < numMatrices - 1; i++) {
if (cols[i] != rows[i + 1]) {
printf("Multiplication not possible\n");
return 1;
}
}
multiplyMatrix(rows[0], cols[0], cols[1], matrices[0], matrices[1], resultMatrix);

for (int i = 2; i < numMatrices; i++) {


int temp[100][100];
multiplyMatrix(rows[0], cols[i - 1], cols[i], resultMatrix, matrices[i], temp);
for (int j = 0; j < rows[0]; j++) {
for (int k = 0; k < cols[i]; k++) {
resultMatrix[j][k] = temp[j][k];
}
}
}

printf("Multiplication result:\n");
printMatrix(rows[0], cols[numMatrices - 1], resultMatrix);

return 0;
}

You might also like