-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 4.4 (Completed)
207 lines (201 loc) · 4.81 KB
/
Exercise 4.4 (Completed)
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
1. (Modify) Rewrite the following if-else chain by using a switch statement:
if (letterGrade == 'A')
cout << “The numerical grade is between 90 and 100\n”;
else if (letterGrade == 'B')
cout << “The numerical grade is between 80 and 89.9\n”;
else if (letterGrade == 'C')
cout << “The numerical grade is between 70 and 79.9\n”;
else if (letterGrade == 'D')
cout << “How are you going to explain this one?\n”;
else
{
cout << “Of course I had nothing to do with my grade.\n”;
cout << “It must have been the professor's fault.\n”;
}
---------------------Changed----------------------------------
switch (letterGrade)
{
case 'A':
cout << “The numerical grade is between 90 and 100”;
break;
case 'B':
cout << “The numerical grade is between 80 and 89.9”;
break;
case 'C':
cout << “The numerical grade is between 70 and 79.9”;
break;
case 'D':
cout << “How are you going to explain this one?”;
break;
default:
cout << “Of course I had nothing to do with the grade.”;
cout << “\nIt must have been the professor's fault.”;
}
2. (Modify) Rewrite the following if-else chain by using a switch statement:
if (bondType == 1)
{
inData();
check();
}
else if (bondType == 2)
{
dates();
leapYr();
}
else if (bondType == 3)
{
yield();
maturity();
}
else if (bondType == 4)
{
price();
roi();
}
else if (bondType == 5)
{
files();
save();
}
else if (bondType == 6)
{
retrieve();
screen();
}
----------------------- Switch Version----------------------------
switch (bondType)
{
case 1:
inData();
check();
break;
case 2:
dates();
leapYr();
break;
case 3:
yield();
maturity();
break;
case 4:
price();
roi();
break;
case 5:
files();
save();
default:
retrieve();
screen();
}
3. (Program) Each storage drive in a shipment is stamped with a code from 1 to 4, indicating
the following storage capacities:
Code Capacity
1 2 GB
2 4 GB
3 16 GB
4 32 GB
Write, compile, and run a C++ program that accepts the code number as an input value and,
based on the value entered, displays the correct storage drive capacity.
#include <iostream>
using namespace std;
int main(){
switch (code){
case 1:
cout << 2 <<"GB"<<endl;
case 2:
cout << 4 << "GB" << endl;
case 3:
cout << 16 << "GB" << endl;
case 4:
cout << 32 << "GB" << endl;
default:
cout << "Nothing" << endl;
}
return 0;
}
4. (Modify) Rewrite Program 4.4 by using a switch statement.
#include <iostream>
using namespace std;
int main(){
switch (marcode){
case 'M':
cout << "Individual is married."<<endl;
case 'S':
cout << "Individual is single" << endl;
default:
cout << "Nothing" << endl;
}
return 0;
}
5. (Modify) Repeat Exercise 9 in Section 4.3, using a switch statement instead of an
if-else chain.
#include <iostream>
using namespace std;
int main(){
double number, Celsius, Fahrenheit;
char letter;
cout << Enter a number and a letter<< endl;
cin >> number >> letter;
Celsius = ((5/9)*(number - 32));
Fahrenheit = ((9.0/5.0) * number + 32.0);
if ( letter == 'f'){
cout << Celsius << endl;}
else if (letter == 'c'){
cout << Fahrenheit << endl;}
return 0;
}
-------------------Switch Statements-----------------------------------------------
#include <iostream>
using namespace std;
int main(){
double number, Celsius, Fahrenheit;
char letter;
cout << Enter a number and a letter<< endl;
cin >> number >> letter;
Celsius = ((5/9)*(number - 32));
Fahrenheit = ((9.0/5.0) * number + 32.0);
switch (letter){
case 'f':
cout << Fahrenheit << endl;
case 'c':
cout << Celsius << endl;
}
return 0;
}
6. (Modify) Rewrite Program 4.6 by using a character variable for the select code.
#include <iostream>
using namespace std;
int main()
{
int opselect;
double fnum, snum;
cout << “Please type in two numbers: “;
cin >> fnum >> snum;
cout << “Enter a select code: “;
cout << “\n 1 for addition”;
cout << “\n 2 for multiplication”;
cout << “\n 3 for division : “;
cin >> opselect;
switch (opselect)
{
case 'a':
cout << “The sum of the numbers entered is “
<< fnum+snum << endl;
break;
case 'b':
cout << “The product of the numbers entered is “
<< fnum*snum << endl;
break;
case 'c':
cout << “The first number divided by the second is “
<< fnum/snum << endl;
break;
} // end of switch
return 0;
}
7. (For thought) Determine why the if-else chain in Program 4.5 can’t be replaced with a
switch statement.
The expression in the switch statement must evaluate to an integer quantity and be tested
for equality. The if-else chain in Program 4.5 compares double-precision values, violating
the switch statement’s requirements for integer relational expressions.