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
|
//cst 116
//dec 7, 2021
//Project 3 (Isabella Mon, Tyler Taormina)
#include <iostream>
using namespace std;
int const SIZE = 3;
void FillMat (int size, float mat[SIZE][SIZE]);
void PrintMat (int size, float mat[SIZE][SIZE]);
void AddMat (int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matSum[SIZE][SIZE]);
void TransMat (int size, float mat[SIZE][SIZE], float tranMat[SIZE][SIZE]);
void MultMat (int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE]);
void ClearBuffer();
void DisplayMenu(int& menu_choice);
void ProcessChoice (int& flag, int menu_choice, int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE], float matSum[SIZE][SIZE]);
int main()
{
float mat1 [SIZE][SIZE];
float mat2 [SIZE][SIZE];
float matSum [SIZE][SIZE];
float matMult [SIZE][SIZE];
float tranMat [SIZE][SIZE];
int menu_choice;
int flag = 1;
FillMat(SIZE, mat1);
FillMat(SIZE, mat2);
while(flag != 0)
{
DisplayMenu(menu_choice);
ProcessChoice(flag, menu_choice, SIZE, mat1, mat2, matMult, tranMat, matSum);
}
cout << "Thanks for checking out the program!" << endl;
return 0;
}
void AddMat(int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matSum[SIZE][SIZE])
{
int row, col;
float sum;
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
sum = mat1[row][col] + mat2[row][col];
matSum[row][col] = sum;
}
}
}
void FillMat(int size, float mat[SIZE][SIZE])
{
int row, col;
float temp;
cout << endl << endl;
cout << "Lets build a 3x3 matrix..." << endl;
cout << "We will fill in the matrix by row..." << endl;
cout << "Input prompts will stop automatically once the matrix is full..." << endl;
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
cout << "Please enter a number: ";
cin >> temp;
mat[row][col] = temp;
}
}
}
void PrintMat(int size, float mat[SIZE][SIZE])
{
int row, col;
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
cout << mat[row][col] << " ";
}
cout << endl;
}
}
void TransMat (int size, float mat[SIZE][SIZE], float tranMat[SIZE][SIZE])
{
int row, col;
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
tranMat[row][col] = mat[col][row];
}
}
}
void MultMat (int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE])
{
int row, col;
int i = 0;
int k;
float temp = 0;
float total = 0;
TransMat(size, mat2, tranMat);
for (k = 0; k < size; k++)
{
for (row = 0; row < size; row++)
{
for (col = 0; col < size; col++)
{
temp = mat1[k][col] * tranMat[i][col];
total += temp;
}
matMult[k][i] = total;
i++;
total = 0;
}
i = 0;
}
}
void DisplayMenu(int& menu_choice)
{
//Displays the menu of functions for the user to choose from.
menu_choice = 0;
cout << "==================================================================\n";
cout << " MENU" << endl;
cout << "==================================================================\n";
cout << "1) Get New Matrices.\n";
cout << "2) Multiply Matrices.\n";
cout << "3) Add Matrices.\n";
cout << "4) Exit.\n";
cout << "Enter: ";
cin >> menu_choice;
if (menu_choice > 4 || menu_choice < 1)
{
cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl;
DisplayMenu(menu_choice);
}
}
void ProcessChoice (int& flag, int menu_choice, int size, float mat1[SIZE][SIZE], float mat2[SIZE][SIZE], float matMult[SIZE][SIZE], float tranMat[SIZE][SIZE], float matSum[SIZE][SIZE])
{
//Takes in user input for menu choice and calls the appropriate function.
string temp;
int check = 1;
ClearBuffer();
switch(menu_choice)
{
case 1:
FillMat(size, mat1);
FillMat(size, mat2);
cout << "MATRIX ONE..." << endl;
cout << "==================" << endl;
PrintMat(size, mat1);
cout << "==================" << endl;
cout << endl << endl;
cout << "MATRIX TWO..." << endl;
cout << "==================" << endl;
PrintMat(size, mat2);
cout << "==================" << endl;
break;
case 2:
MultMat(size, mat1, mat2, matMult, tranMat);
cout << "MULTIPLIED MATRIX..." << endl;
cout << "==================" << endl;
PrintMat(size, matMult);
cout << "==================" << endl;
break;
case 3:
AddMat(size, mat1, mat2, matSum);
cout << "ADDED MATRIX..." << endl;
cout << "==================" << endl;
PrintMat(size, matSum);
cout << "==================" << endl;
break;
case 4:
cout << "Are you sure you want to exit?" << endl;
check = 0;
break;
default:
break;
}
if (check == 0)
{
cout << "Enter 0 to end program.\n" << endl;
cout << "Enter any other number to continue program.\n" << endl;
cin >> flag;
}
}
void ClearBuffer()
{
// clears buffer after menu choice so as not to interfere with the following user inputs.
char c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
|