-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaxtrixMultiplication.c
93 lines (77 loc) · 2.86 KB
/
MaxtrixMultiplication.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// MatrixOps.c
// To compute the matrix sum and matrix product of two matrices.
#include <stdio.h>
#define MAX_ROW 10
#define MAX_COL 10
void scanMatrix(float [][MAX_COL], int, int);
void printMatrix(float [][MAX_COL], int, int);
void sumMatrix(float [][MAX_COL], float [][MAX_COL], float [][MAX_COL], int, int);
void prodMatrix(float [][MAX_COL], float [][MAX_COL], float [][MAX_COL], int, int, int);
int main(void) {
float matrixA[MAX_ROW][MAX_COL]; // input matrix
float matrixB[MAX_ROW][MAX_COL]; // input matrix
float matrixC[MAX_ROW][MAX_COL]; // sum matrix
int matrixArows, matrixAcols; // number of rows and columns for matrix A
int matrixBrows, matrixBcols; // number of rows and columns for matrix B
printf("Matrix A:\n");
printf("Enter number of rows and columns: ");
scanf("%d %d", &matrixArows, &matrixAcols);
scanMatrix(matrixA, matrixArows, matrixAcols);
printf("Matrix B:\n");
printf("Enter number of rows and columns: ");
scanf("%d %d", &matrixBrows, &matrixBcols);
scanMatrix(matrixB, matrixBrows, matrixBcols);
if ((matrixArows == matrixBrows) && (matrixAcols == matrixBcols)) {
sumMatrix(matrixA, matrixB, matrixC, matrixArows, matrixAcols);
printf("Sum matrix:\n");
printMatrix(matrixC, matrixArows, matrixAcols);
}
else
printf("Unmatched dimensions; cannot be added.\n");
if (matrixAcols == matrixBrows) {
prodMatrix(matrixA, matrixB, matrixC, matrixArows, matrixAcols, matrixBcols);
printf("Product matrix:\n");
printMatrix(matrixC, matrixArows, matrixBcols);
}
else
printf("Unmatched dimensions; cannot be multiplied.\n");
return 0;
}
// To sum mtxA and mtxB to obtain mtxC
void sumMatrix(float mtxA[][MAX_COL], float mtxB[][MAX_COL], float mtxC[][MAX_COL], int row_size, int col_size) {
int row, col;
for (row=0; row<row_size; row++)
for (col=0; col<col_size; col++)
mtxC[row][col] = mtxA[row][col] + mtxB[row][col];
}
// To multiply mtxA and mtxB to obtain mtxC
void prodMatrix(float mtxA[][MAX_COL], float mtxB[][MAX_COL], float mtxC[][MAX_COL], int mtxA_row_size, int mtxA_col_size, int mtxB_col_size) {
int row,col,index;
float temp;
for(row = 0; row <mtxA_row_size; row++){
for(col = 0; col < mtxB_col_size; col++){
temp = 0;
for(index = 0; index < mtxA_col_size;index++){
temp = temp + mtxA[row][index] * mtxB[index][col];
}
mtxC[row][col] = temp;
}
}
}
// To read values into mtx
void scanMatrix(float mtx[][MAX_COL], int row_size, int col_size) {
int row, col;
printf("Enter values for matrix:\n");
for (row=0; row<row_size; row++)
for (col=0; col<col_size; col++)
scanf("%f", &mtx[row][col]);
}
// To print values of mtx
void printMatrix(float mtx[][MAX_COL], int row_size, int col_size) {
int row, col;
for (row=0; row<row_size; row++) {
for (col=0; col<col_size; col++)
printf("%.2f\t", mtx[row][col]);
printf("\n");
}
}