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

Matrix

A programm in Language C for Matrix Multiplication

Uploaded by

Gopakumar Pb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views1 page

Matrix

A programm in Language C for Matrix Multiplication

Uploaded by

Gopakumar Pb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <stdio.

h>

#define SIZE 3

void readMatrix(int matrix[SIZE][SIZE], char name) {


printf("Enter elements of matrix %c (3x3):\n", name);
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
printf("%c[%d][%d]: ", name, i, j);
scanf("%d", &matrix[i][j]);
}
}
}

void addMatrices(int B[SIZE][SIZE], int C[SIZE][SIZE], int result[SIZE][SIZE]) {


for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j)
result[i][j] = B[i][j] + C[i][j];
}

void multiplyMatrices(int A[SIZE][SIZE], int E[SIZE][SIZE], int D[SIZE][SIZE]) {


for (int i = 0; i < SIZE; ++i)
for (int j = 0; j < SIZE; ++j) {
D[i][j] = 0;
for (int k = 0; k < SIZE; ++k)
D[i][j] += A[i][k] * E[k][j];
}
}

void printMatrix(int matrix[SIZE][SIZE], char name) {


printf("Matrix %c:\n", name);
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j)
printf("%5d", matrix[i][j]);
printf("\n");
}
}

int main() {
int A[SIZE][SIZE], B[SIZE][SIZE], C[SIZE][SIZE];
int E[SIZE][SIZE], D[SIZE][SIZE]; // E = B + C, D = A * E

readMatrix(A, 'A');
readMatrix(B, 'B');
readMatrix(C, 'C');

addMatrices(B, C, E);
multiplyMatrices(A, E, D);

printMatrix(E, 'E'); // Optional: intermediate result B + C


printMatrix(D, 'D');

return 0;
}

You might also like