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
ID: 3555880 • Letter: 1
Question
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
#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
bool getMatrixFromFile(string title, int mat[10][10], int &r, int &c);
void getMatrixInfo(ifstream &in, int &val);
void zeroMatrix(int mat[10][10], int rows, int col);
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol);
void display(ostream &out, string title, int mat[10][10], int row, int col);
int findColWidth(int mat[10][10], int row, int col);
int abs(int x);
void getFile(ofstream &out, string &name);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int matA[10][10], matB[10][10], matC[10][10],
aRow, aCol, bRow, bCol, cRow, cCol;
ifstream in;
ofstream out;
string answer, fileName;
while(getMatrixFromFile("A", matA, aRow, aCol))
{
while(!getMatrixFromFile("B", matB, bRow, bCol));
zeroMatrix(matC, 10, 10);
if(multMatrix(matA, aRow, aCol, matB, bRow, bCol, matC, cRow, cCol))
{
display(cout,"A", matA, aRow, aCol);
cout<<"times"<<endl;
display(cout,"B",matB, bRow, bCol);
cout<<"equals"<<endl;
display(cout,"C", matC, cRow, cCol);
cout<<"Do you want to write Matrix C to a file? ";
cin>>answer;
if(toupper(answer[0]) == 'Y')
{
getFile(out, fileName);
display(out,"", matC, cRow, cCol);
cout<<"Matrix C written to "<<fileName<<endl;
}
}
}
return a.exec();
}
int abs(int x)
{
return (x < 0) ? -x : x;
}
void getFile(ofstream &out, string &name)
{
ifstream in;
char ans;
bool again = true;
while(again)
{
cout<<"What is the name of the file to write data to: ";
cin>>name;
in.open(name.c_str());
if(in.fail())
{
in.close();
in.clear();
out.open(name.c_str());
again = false;
}
else
{
in.close();
cout<<"WARNING!!!! That file exists!"<<endl
<<"Do you want to (O)verwrite, (A)ppend, (R)e-enter, or (Q)uit? ";
cin>>ans;
switch(toupper(ans))
{
case 'O' : out.open(name.c_str());
again = false;
break;
case 'A' : out.open(name.c_str(), ios::app);
again = false;
break;
case 'R' :
break;
case 'Q' : cout<<"Program terminating!"<<endl;
exit(0);
default : cout<<"Unknown option. You will need to re-enter a file name to try again"<<endl;
}
}
}
}
int findColWidth(int mat[10][10], int row, int col)
{
int max = abs(mat[0][0]);
bool isNeg = mat[0][0] < 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
if(max < abs(mat[i][j]))
{
max = abs(mat[i][j]);
isNeg = mat[i][j] < 0;
}
return (int)log10(1.*max) + isNeg ? 3 : 2;
}
void display(ostream &out, string title,int mat[10][10], int row, int col)
{
int width = findColWidth(mat, row, col);
if(out == cout)
cout<<"Matrix "<<title<<":"<<endl;
else
out<<"Rows : "<<row<<endl
<<"Columns : "<<col<<endl;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
cout<<setw(width)<<mat[i][j];
out<<endl;
}
if(out == cout)
out<<endl<<endl;
}
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol)
{
int sum = 0;
if(aCol != bRow)
{
cout<<"ERROR!! Dimension mismatch!!!"<<endl;
return false;
}
cRow = aCol;
cCol = bRow;
for(int i = 0; i < aRow; i++)
{
sum = 0;
for(int j = 0; j < bCol; j++)
{
for(int k = 0; k < bRow; k++)
sum += matA[i][k] + matB[k][i];
matC[i][j] = sum;
}
}
return true;
}
void zeroMatrix(int mat[10][10], int rows, int cols)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
mat[i][j] = 0;
}
void getMatrixInfo(ifstream &in, int &val)
{
stringstream ss;
string line;
int pos;
getline(in, line);
pos = line.find(':');
ss<<line.substr(pos+1);
ss>>val;
}
bool getMatrixFromFile(string title, int mat[10][10], int &rows, int &cols)
{
string line, answer;
ifstream in;
bool repeat = true;
while(repeat)
{
cout<<"Matrix "<<title<<" file name? ";
cin>>line;
if(line == "")
return false;
in.open(line.c_str());
if(in.fail())
{
in.close();
in.clear();
cout<<"That file does not exist. Do you want (Q)uit or (R)e-enter? ";
cin>>answer;
if(toupper(answer[0]) == 'Q')
exit(0);
}
else
repeat = false;
}
getMatrixInfo(in, rows);
getMatrixInfo(in,cols);
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
in>>mat[i][j];
in.close();
}
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
#include <QtCore/QCoreApplication>
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
bool getMatrixFromFile(string title, int mat[10][10], int &r, int &c);
void getMatrixInfo(ifstream &in, int &val);
void zeroMatrix(int mat[10][10], int rows, int col);
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol);
void display(ostream &out, string title, int mat[10][10], int row, int col);
int findColWidth(int mat[10][10], int row, int col);
int abs(int x);
void getFile(ofstream &out, string &name);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int matA[10][10], matB[10][10], matC[10][10],
aRow, aCol, bRow, bCol, cRow, cCol;
ifstream in;
ofstream out;
string answer, fileName;
while(getMatrixFromFile("A", matA, aRow, aCol))
{
while(!getMatrixFromFile("B", matB, bRow, bCol));
zeroMatrix(matC, 10, 10);
if(multMatrix(matA, aRow, aCol, matB, bRow, bCol, matC, cRow, cCol))
{
display(cout,"A", matA, aRow, aCol);
cout<<"times"<<endl;
display(cout,"B",matB, bRow, bCol);
cout<<"equals"<<endl;
display(cout,"C", matC, cRow, cCol);
cout<<"Do you want to write Matrix C to a file? ";
cin>>answer;
if(toupper(answer[0]) == 'Y')
{
getFile(out, fileName);
display(out,"", matC, cRow, cCol);
cout<<"Matrix C written to "<<fileName<<endl;
}
}
}
return a.exec();
}
int abs(int x)
{
return (x < 0) ? -x : x;
}
void getFile(ofstream &out, string &name)
{
ifstream in;
char ans;
bool again = true;
while(again)
{
cout<<"What is the name of the file to write data to: ";
cin>>name;
in.open(name.c_str());
if(in.fail())
{
in.close();
in.clear();
out.open(name.c_str());
again = false;
}
else
{
in.close();
cout<<"WARNING!!!! That file exists!"<<endl
<<"Do you want to (O)verwrite, (A)ppend, (R)e-enter, or (Q)uit? ";
cin>>ans;
switch(toupper(ans))
{
case 'O' : out.open(name.c_str());
again = false;
break;
case 'A' : out.open(name.c_str(), ios::app);
again = false;
break;
case 'R' :
break;
case 'Q' : cout<<"Program terminating!"<<endl;
exit(0);
default : cout<<"Unknown option. You will need to re-enter a file name to try again"<<endl;
}
}
}
}
int findColWidth(int mat[10][10], int row, int col)
{
int max = abs(mat[0][0]);
bool isNeg = mat[0][0] < 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
if(max < abs(mat[i][j]))
{
max = abs(mat[i][j]);
isNeg = mat[i][j] < 0;
}
return (int)log10(1.*max) + isNeg ? 3 : 2;
}
void display(ostream &out, string title,int mat[10][10], int row, int col)
{
int width = findColWidth(mat, row, col);
if(out == cout)
cout<<"Matrix "<<title<<":"<<endl;
else
out<<"Rows : "<<row<<endl
<<"Columns : "<<col<<endl;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
cout<<setw(width)<<mat[i][j];
out<<endl;
}
if(out == cout)
out<<endl<<endl;
}
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol)
{
int sum = 0;
if(aCol != bRow)
{
cout<<"ERROR!! Dimension mismatch!!!"<<endl;
return false;
}
cRow = aCol;
cCol = bRow;
for(int i = 0; i < aRow; i++)
{
sum = 0;
for(int j = 0; j < bCol; j++)
{
for(int k = 0; k < bRow; k++)
sum += matA[i][k] + matB[k][i];
matC[i][j] = sum;
}
}
return true;
}
void zeroMatrix(int mat[10][10], int rows, int cols)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
mat[i][j] = 0;
}
void getMatrixInfo(ifstream &in, int &val)
{
stringstream ss;
string line;
int pos;
getline(in, line);
pos = line.find(':');
ss<<line.substr(pos+1);
ss>>val;
}
bool getMatrixFromFile(string title, int mat[10][10], int &rows, int &cols)
{
string line, answer;
ifstream in;
bool repeat = true;
while(repeat)
{
cout<<"Matrix "<<title<<" file name? ";
cin>>line;
if(line == "")
return false;
in.open(line.c_str());
if(in.fail())
{
in.close();
in.clear();
cout<<"That file does not exist. Do you want (Q)uit or (R)e-enter? ";
cin>>answer;
if(toupper(answer[0]) == 'Q')
exit(0);
}
else
repeat = false;
}
getMatrixInfo(in, rows);
getMatrixInfo(in,cols);
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
in>>mat[i][j];
in.close();
}
Explanation / Answer
the modified code
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
bool getMatrixFromFile(string title, int mat[10][10], int &r, int &c);
void getMatrixInfo(ifstream &in, int &val);
void zeroMatrix(int mat[10][10], int rows, int col);
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol);
void display(ostream &out, string title, int mat[10][10], int row, int col);
int findColWidth(int mat[10][10], int row, int col);
int abs(int x);
void getFile(ofstream &out, string &name);
int main(int argc, char *argv[])
{
int matA[10][10], matB[10][10], matC[10][10],
aRow, aCol, bRow, bCol, cRow, cCol;
ifstream in;
ofstream out;
string answer, fileName;
while(getMatrixFromFile("A", matA, aRow, aCol))
{
while(!getMatrixFromFile("B", matB, bRow, bCol));
zeroMatrix(matC, 10, 10);
if(multMatrix(matA, aRow, aCol, matB, bRow, bCol, matC, cRow, cCol))
{
display(cout,"A", matA, aRow, aCol);
cout<<"times"<<endl;
display(cout,"B",matB, bRow, bCol);
cout<<"equals"<<endl;
display(cout,"C", matC, cRow, cCol);
cout<<"Do you want to write Matrix C to a file? ";
cin>>answer;
if(toupper(answer[0]) == 'Y')
{
getFile(out, fileName);
display(out,"", matC, cRow, cCol);
cout<<"Matrix C written to "<<fileName<<endl;
}
}
}
}
int abs(int x)
{
return (x < 0) ? -x : x;
}
void getFile(ofstream &out, string &name)
{
ifstream in;
char ans;
bool again = true;
while(again)
{
cout<<"What is the name of the file to write data to: ";
cin>>name;
in.open(name.c_str());
if(in.fail())
{
in.close();
in.clear();
out.open(name.c_str());
again = false;
}
else
{
in.close();
cout<<"WARNING!!!! That file exists!"<<endl
<<"Do you want to (O)verwrite, (A)ppend, (R)e-enter, or (Q)uit? ";
cin>>ans;
switch(toupper(ans))
{
case 'O' : out.open(name.c_str());
again = false;
break;
case 'A' : out.open(name.c_str(), ios::app);
again = false;
break;
case 'R' :
break;
case 'Q' : cout<<"Program terminating!"<<endl;
exit(0);
default : cout<<"Unknown option. You will need to re-enter a file name to try again"<<endl;
}
}
}
}
int findColWidth(int mat[10][10], int row, int col)
{
int max = abs(mat[0][0]);
bool isNeg = mat[0][0] < 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
if(max < abs(mat[i][j]))
{
max = abs(mat[i][j]);
isNeg = mat[i][j] < 0;
}
return (int)log10(1.*max) + isNeg ? 3 : 2;
}
void display(ostream &out, string title,int mat[10][10], int row, int col)
{
int width = findColWidth(mat, row, col);
if(out == cout)
cout<<"Matrix "<<title<<":"<<endl;
else
out<<"Rows : "<<row<<endl
<<"Columns : "<<col<<endl;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
cout<<setw(width)<<mat[i][j];
out<<endl;
}
if(out == cout)
out<<endl<<endl;
}
bool multMatrix(int matA[10][10], int aRow, int aCol, int matB[10][10], int bRow, int bCol, int matC[10][10], int &cRow, int &cCol)
{
int sum = 0;
if(aCol != bRow)
{
cout<<"ERROR!! Dimension mismatch!!!"<<endl;
return false;
}
cRow = aCol;
cCol = bRow;
for(int i = 0; i < aRow; i++)
{
sum = 0;
for(int j = 0; j < bCol; j++)
{
for(int k = 0; k < bRow; k++)
sum += matA[i][k] + matB[k][i];
matC[i][j] = sum;
}
}
return true;
}
void zeroMatrix(int mat[10][10], int rows, int cols)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
mat[i][j] = 0;
}
void getMatrixInfo(ifstream &in, int &val)
{
stringstream ss;
string line;
int pos;
getline(in, line);
pos = line.find(':');
ss<<line.substr(pos+1);
ss>>val;
}
bool getMatrixFromFile(string title, int mat[10][10], int &rows, int &cols)
{
string line, answer;
ifstream in;
bool repeat = true;
while(repeat)
{
cout<<"Matrix "<<title<<" file name? ";
cin>>line;
if(line == "")
return false;
in.open(line.c_str());
if(in.fail())
{
in.close();
in.clear();
cout<<"That file does not exist. Do you want (Q)uit or (R)e-enter? ";
cin>>answer;
if(toupper(answer[0]) == 'Q')
exit(0);
}
else
repeat = false;
}
getMatrixInfo(in, rows);
getMatrixInfo(in,cols);
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
in>>mat[i][j];
in.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.