-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_ops.c
More file actions
141 lines (120 loc) · 4.31 KB
/
matrix_ops.c
File metadata and controls
141 lines (120 loc) · 4.31 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* matrix_ops.c
Matrix Addition, Multiplication and Transpose using functions and 2D arrays.
Max allowed size is controlled by MAX.
*/
#include <stdio.h>
#define MAX 10
/* Read an integer within [min..max] (re-prompts until valid) */
int readIntInRange(const char *prompt, int min, int max) {
int x, rc;
while (1) {
printf("%s", prompt);
rc = scanf("%d", &x);
if (rc != 1) {
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) { } /* clear input */
printf("Invalid input. Please enter an integer.\n");
continue;
}
if (x < min || x > max) {
printf("Please enter a value between %d and %d.\n", min, max);
continue;
}
return x;
}
}
/* Input matrix elements (with per-element validation) */
void inputMatrix(int M[MAX][MAX], int r, int c, char name) {
printf("Enter elements for matrix %c (%d x %d):\n", name, r, c);
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
while (1) {
printf(" %c[%d][%d] = ", name, i + 1, j + 1);
if (scanf("%d", &M[i][j]) == 1) break;
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) { }
printf(" Invalid number. Try again.\n");
}
}
}
}
/* Print matrix */
void printMatrix(int M[MAX][MAX], int r, int c) {
for (int i = 0; i < r; ++i) {
for (int j = 0; j < c; ++j) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Add matrices A and B (assumes r x c). Result stored in R. */
void addMatrices(int A[MAX][MAX], int B[MAX][MAX], int R[MAX][MAX], int r, int c) {
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
R[i][j] = A[i][j] + B[i][j];
}
/* Multiply A (r1 x c1) by B (r2 x c2). If possible, store in R and return 1, else return 0. */
int multiplyMatrices(int A[MAX][MAX], int B[MAX][MAX], int R[MAX][MAX],
int r1, int c1, int r2, int c2) {
if (c1 != r2) return 0;
/* initialize result to 0 */
for (int i = 0; i < r1; ++i)
for (int j = 0; j < c2; ++j)
R[i][j] = 0;
for (int i = 0; i < r1; ++i) {
for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
R[i][j] += A[i][k] * B[k][j];
}
}
}
return 1;
}
/* Transpose matrix A (r x c) into T (c x r) */
void transposeMatrix(int A[MAX][MAX], int T[MAX][MAX], int r, int c) {
for (int i = 0; i < c; ++i) /* ensure all cells of T are set when displayed */
for (int j = 0; j < r; ++j)
T[i][j] = 0;
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
T[j][i] = A[i][j];
}
int main(void) {
int A[MAX][MAX], B[MAX][MAX];
int addR[MAX][MAX], mulR[MAX][MAX], trA[MAX][MAX], trB[MAX][MAX];
int r1, c1, r2, c2;
printf("Matrix Operations (max size %dx%d)\n", MAX, MAX);
r1 = readIntInRange("Enter rows for Matrix A: ", 1, MAX);
c1 = readIntInRange("Enter columns for Matrix A: ", 1, MAX);
inputMatrix(A, r1, c1, 'A');
r2 = readIntInRange("Enter rows for Matrix B: ", 1, MAX);
c2 = readIntInRange("Enter columns for Matrix B: ", 1, MAX);
inputMatrix(B, r2, c2, 'B');
printf("\nMatrix A:\n");
printMatrix(A, r1, c1);
printf("\nMatrix B:\n");
printMatrix(B, r2, c2);
/* Addition if possible */
if (r1 == r2 && c1 == c2) {
addMatrices(A, B, addR, r1, c1);
printf("\nA + B =\n");
printMatrix(addR, r1, c1);
} else {
printf("\nAddition not possible: dimensions differ (A is %dx%d, B is %dx%d)\n", r1, c1, r2, c2);
}
/* Multiplication A x B */
if (multiplyMatrices(A, B, mulR, r1, c1, r2, c2)) {
printf("\nA x B =\n");
printMatrix(mulR, r1, c2);
} else {
printf("\nMultiplication not possible: columns of A (%d) != rows of B (%d)\n", c1, r2);
}
/* Transposes */
transposeMatrix(A, trA, r1, c1);
printf("\nTranspose of A (A^T):\n");
printMatrix(trA, c1, r1);
transposeMatrix(B, trB, r2, c2);
printf("\nTranspose of B (B^T):\n");
printMatrix(trB, c2, r2);
return 0;
}