-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
265 lines (203 loc) · 5.99 KB
/
main.c
File metadata and controls
265 lines (203 loc) · 5.99 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define MAX_SIZE 35
// Matrices and variables for calculations
int A[MAX_SIZE][MAX_SIZE],
B[MAX_SIZE][MAX_SIZE],
C[MAX_SIZE][MAX_SIZE],
SUM[MAX_SIZE][MAX_SIZE],
PROD[MAX_SIZE][MAX_SIZE],
TRANSP[MAX_SIZE][MAX_SIZE];
int TRC;
// Menu options
char menu[][MAX_SIZE] = {"1. Sum and product of 2 matrices", "2. Matrix trace", "3. Check symmetry"};
// Function to read matrix elements from user input
void read(char m1, int m[][MAX_SIZE], int lc) {
printf("\nIntroduce elements of matrix %c:\n", m1);
for (int i = 0; i < lc; i++) {
for (int j = 0; j < lc; j++) {
printf("\n%c[%d][%d]= ", m1, i, j);
scanf("%d", &m[i][j]);
}
}
system("cls");
}
// Function to display (contents of) a matrix
void display(char m1[], int m[][MAX_SIZE], int lc) {
printf("%s =\n", m1);
for (int i = 0; i < lc; i++) {
printf("\t");
for (int j = 0; j < lc; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
}
// Function to display two matrices side by side
void display_1(char m1, char m2, int mat1[][MAX_SIZE], int mat2[][MAX_SIZE], int n) {
printf("%c, %c =\n", m1, m2);
for (int i = 0; i < n; i++) {
int ok = 0,
k = 0;
printf("\t");
while (k < 2) {
if (ok) {
printf(" ");
}
if (k % 2 == 0) {
for (int j = 0; j < n; j++) {
printf("%d ", mat1[i][j]);
}
}
else {
for (int j = 0; j < n; j++) {
printf("%d ", mat2[i][j]);
}
}
ok = 1;
k++;
}
printf("\n");
}
}
// Function to print the menu options
void printMenu(char menu[][MAX_SIZE], int n) {
for (int i = 0; i < n; i++) {
printf("%s\n", menu[i]);
}
printf("\n");
}
// Function to ask the user if they want to return to the menu
void comeBack() {
int ans;
printf("\nDo you want to return to the menu? (y/n) ");
while ((ans = _getch()) != ('y' && 'n')) {
if (ans == 'n') {
exit(0);
}
else {
if (ans == 'y') {
break;
}
}
printf("\nUnknown command!");
Sleep(1000);
printf("\r ");
CONSOLE_SCREEN_BUFFER_INFO coninfo; // structure for cursor position
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // get console handle
GetConsoleScreenBufferInfo(hConsole, &coninfo); // get cursor position
coninfo.dwCursorPosition.Y -= 1; // moving one line up
coninfo.dwCursorPosition.X += 15; // moving right with 15 characters
SetConsoleCursorPosition(hConsole, coninfo.dwCursorPosition); // set cursor position
}
}
// Function to calculate the sum of two matrices
void sum(int lc) {
for (int i = 0; i < lc; i++) {
for (int j = 0; j < lc; j++) {
SUM[i][j] = A[i][j] + B[i][j];
}
}
}
// Function to calculate the product of two matrices
void product(int lc) {
for (int i = 0; i < lc; i++) {
for (int j = 0; j < lc; j++) {
PROD[i][j] = 0;
for (int k = 0; k < lc; k++) {
PROD[i][j] += A[i][k] * B[k][j];
}
}
}
}
// Function to calculate the trace of a square matrix
void trace(int m[][MAX_SIZE], int lc) {
TRC = 0;
for (int i = 0; i < lc; i++) {
TRC += m[i][i];
}
}
// Function to calculate the transpose of a matrix
void transpose(int m[][MAX_SIZE], int lc) {
for (int i = 0; i < lc; i++) {
for (int j = 0; j < lc; j++) {
TRANSP[i][j] = m[j][i];
}
}
}
// Function to check if a matrix is symmetric or antisymmetric
int check_sym(int m[][MAX_SIZE], int lc) {
int ok = 1;
for (int i = 0; i < lc; i++) {
for (int j = 0; j < lc; j++) {
if (TRANSP[i][j] != m[i][j]) {
ok = 0;
break;
}
}
}
return ok;
}
// Function for the "Sum and product of 2 matrices" menu option
void do1(int *n) {
system("cls");
printf("Introduce a number of rows and columns: ");
scanf("%d", &*n);
read('A', A, *n);
read('B', B, *n);
sum(*n);
product(*n);
display_1('A', 'B', A, B, *n);
display("Sum", SUM, *n);
display("Product", PROD, *n);
comeBack();
}
// Function for the "Matrix trace" menu option
void do2(int *n) {
system("cls");
printf("Introduce a number of rows and columns: ");
scanf("%d", &*n);
read('C', C, *n);
display("C", C, *n);
trace(C, *n);
printf("\nTrace of matrix is: %d\n", TRC);
comeBack();
}
// Function for the "Check symmetry" menu option
void do3(int *n) {
system("cls");
printf("Introduce a number of rows and columns: ");
scanf("%d", &*n);
read('C', C, *n);
display("C", C, *n);
transpose(C, *n);
display("Transpose", TRANSP, *n);
if (check_sym(C, *n)) {
printf("\nMatrix is symmetric!\n");
}
else {
printf("\nMatrix is antisymmetric!\n");
}
comeBack();
}
int main() {
int *n, ch;
system("cls");
printMenu(menu, sizeof(menu) / sizeof(menu[0]));
while((ch = _getch()) != '\r') {
switch(ch) {
case '1': do1(&n);
break;
case '2': do2(&n);
break;
case '3': do3(&n);
break;
default: printf("Unknown command!\n");
Sleep(1000);
}
system("cls");
printMenu(menu, sizeof(menu) / sizeof(menu[0]));
}
return 0;
}