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
|
// project3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
//Written by Jacob Knox: [email protected]
#include <iostream>
#include <string>
#include <iomanip>
#include "project3.h"
using namespace std;
int main()
{
float m1[MATRIX_SIZE][MATRIX_SIZE], m2[MATRIX_SIZE][MATRIX_SIZE];
int choice = 0;
//make sure you have matricies before giving options
getMatricies(m1, m2);
menu(choice, m1, m2);
}
void menu(int choice, float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE])
{
string menu = "\t\t--Matirx Prorgam--\n\
1. Input new matricies\n\
2. Multiply matricies\n\
3. Add matricies\n\
4. Exit Program\n\n\
Input your choice: ";
//choice == 4 means quit and retturn to main
//main program loop
while (choice != 4)
{
choice = readInt(menu, "Error please input an integer 1-4.\n");
switch (choice)
{
case 1:
getMatricies(m1, m2);
break;
case 2:
multiplyMatrix(m1, m2);
break;
case 3:
addMatrix(m1, m2);
break;
case 4:
cout << "Exiting program...";
break;
default:
cout << "Error unknown choice " << choice << ". Please try again.";
break;
}
}
}
//reads two matricies from user
void getMatricies(float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE])
{
cout << "Input the values for the first matrix:\n";
getMatrix(m1);
cout << "Input the values for the second matrix:\n";
getMatrix(m2);
}
// reads all the values for a single matrix
void getMatrix(float mat[MATRIX_SIZE][MATRIX_SIZE])
{
for (int x = 0; x < MATRIX_SIZE; x++)
{
for (int y = 0; y < MATRIX_SIZE; y++)
{
mat[x][y] = readFloat("Input the matrix value at " + to_string(x+1) + ", " + to_string(y+1) + ": ", "Error invalid value. Please input a float.\n");
}
}
}
// adds matrix 1 and 2 and shows result
void addMatrix(float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE])
{
float result[MATRIX_SIZE][MATRIX_SIZE];
for (int x = 0; x < MATRIX_SIZE; x++)
{
for (int y = 0; y < MATRIX_SIZE; y++)
{
//adding matrix is adding each spot to corresponding spot in result
result[x][y] = m1[x][y] + m2[x][y];
}
}
cout << "The result of adding\n";
printMatrix(m1);
cout << "with\n";
printMatrix(m2);
cout << "is:\n";
printMatrix(result);
}
//multiplies thw two matrices
void multiplyMatrix(float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE])
{
float result[MATRIX_SIZE][MATRIX_SIZE];
for (int x = 0; x < MATRIX_SIZE; x++)
{
for (int y = 0; y < MATRIX_SIZE; y++)
{
//result for a specific spot is the dot product of that spot in the two matrices
result[x][y] = calulateDotProduct(x, y, m1, m2);
}
}
cout << "The result of multiplying\n";
printMatrix(m1);
cout << "with\n";
printMatrix(m2);
cout << "is:\n";
printMatrix(result);
}
//dot product is multiplying the corresponding values in one matrix collunm/ row and adding the results of that
float calulateDotProduct(int x, int y, float m1[MATRIX_SIZE][MATRIX_SIZE], float m2[MATRIX_SIZE][MATRIX_SIZE])
{
float product = 0;
for (int i = 0; i < MATRIX_SIZE; i++)
{
product += m1[x][i]*m2[i][y];
}
return product;
}
//reads an int from the user, will output error if it fails and repeat asking until it gets a good answer
int readInt(const std::string& question, const std::string& error)
{
int value;
cout << question;
while (!(cin >> value))
{
cin.clear();
while (cin.get() != '\n') continue;
cout << error;
cout << question;
}
return value;
}
//reads an float from the user, will output error if it fails and repeat asking until it gets a good answer
float readFloat(const std::string& question, const std::string& error)
{
float value;
cout << question;
while (!(cin >> value))
{
cin.clear();
while (cin.get() != '\n') continue;
cout << error;
cout << question;
}
return value;
}
//prints a matrix by looping over it and formatting appropiately
void printMatrix(float mat[MATRIX_SIZE][MATRIX_SIZE])
{
for (int x = 0; x < MATRIX_SIZE; x++)
{
for (int y = 0; y < MATRIX_SIZE; y++)
{
cout << setprecision(3) << fixed << "[" << mat[x][y] << "]";
}
cout << endl;
}
}
|