0% found this document useful (0 votes)
10 views1 page

Matrix Multiplication

The document contains a C program for matrix multiplication, defining functions to multiply two matrices and display the result. It prompts the user to input the dimensions and elements of the matrices, checks for compatibility, and then computes and displays the resultant matrix. The program includes error handling for incompatible matrix sizes.

Uploaded by

highonmeth890
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)
10 views1 page

Matrix Multiplication

The document contains a C program for matrix multiplication, defining functions to multiply two matrices and display the result. It prompts the user to input the dimensions and elements of the matrices, checks for compatibility, and then computes and displays the resultant matrix. The program includes error handling for incompatible matrix sizes.

Uploaded by

highonmeth890
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/ 1

MATRIX MULTIPLICATION

#include <stdio.h>
#include<conio.h>
void multiplyMatrices(int firstMatrix[10][10],int secondMatrix[10][10],int result[10][10],int
row1, int col1,int row2,int col2) {
int i,j,k;
for(i = 0; i < row1; i++) {
for(j = 0; j < col2; j++) {
result[i][j] = 0;
for ( k = 0; k < col1; k++) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void displayMatrix(int matrix[][10], int row, int col) { int i,j;


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

int main() { int i,j;


int row1, col1, row2, col2;
int firstMatrix[10][10], secondMatrix[10][10], result[10][10];
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &row1, &col1);
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &row2, &col2);
if (col1 != row2) {
printf("Error! Column of first matrix must be equal to row of second matrix.\n");
return 1;
}
printf("Enter elements of first matrix:\n");
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++)
scanf("%d", &firstMatrix[i][j]);
printf("Enter elements of second matrix:\n");
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++)
scanf("%d", &secondMatrix[i][j]);
multiplyMatrices(firstMatrix, secondMatrix, result, row1, col1, row2, col2);
printf("Resultant Matrix:\n");
displayMatrix(result, row1, col2);
getch();
return 0;
}

You might also like