-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 4.3 (Completed)
413 lines (387 loc) · 14.7 KB
/
Exercise 4.3 (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
1. (Practice) An acute angle is less than 90 degrees, an obtuse angle is greater than 90 degrees,
and a right angle is equal to 90 degrees. Using this information, write a C++ program that
accepts an angle, in degrees, and displays the type of angle corresponding to the degrees
entered.
#include <iostream>
using namespace std;
int main() {
float angle;
cout << "Enter your Angle:"<< endl;
cin >> angle;
if(angle < 90){
cout << "Acute angle" <<endl;}
else if(angle == 90){
cout << "right angle" << endl;
}
else {cout << "obtuse angle <<endl;}
return 0;
}
2. (Program) The grade level of undergraduate college students is typically determined according
to the following schedule:
Number of Credits Completed Grade Level
Less than 32 Freshman
32 to 63 Sophomore
64 to 95 Junior
96 or more Senior
Using this information, write a C++ program that accepts the number of credits a student has
completed, determines the student’s grade level, and displays the grade level.
#include <iostream>
using namespace std;
int main(){
int numcredits;
cout << "Enter your Credits Completed Grade Level" << endl;
cin >> numcredits;
if( numcredits >= 96){
ncout << "Senior" <<endl;
}
else if(numcredits >= 64 && numcredits <= 95){
cout << "Junior" << endl;
}
else if( numcredits >= 32 && numcredits <= 63){
cout << "Sophmore" <<endl:
}
else{cout << "Freshman" << endl;
}
3. (Program) A student’s letter grade is calculated according to the following schedule:
Numerical Grade Letter Grade
Greater than or equal to 90 A
Less than 90 but greater than or equal to 80 B
Less than 80 but greater than or equal to 70 C
Less than 70 but greater than or equal to 60 D
Less than 60 F
Using this information, write, compile, and run a C++ program that accepts a student’s numerical
grade, converts the numerical grade to an equivalent letter grade, and displays the letter grade.
#include <iostream>
using namespace std;
int mian(){
double grade;
if(grade < 90 && grade >= 80){
cout << "B" <<endl;}
else if(grade >= 90){
cout << 'A' << endl;}
else if (grade < 80 && grade >= 70){
cout << 'C' << endl;}
else if (grade < 70 && grade >= 60){
cout << 'D' << endl;}
else if (grade < 60){
cout << 'F' << endl;}
return 0;
}
4. (Program) The interest rate paid on funds deposited in a bank is determined by the amount
of time the money is left on deposit. For a particular bank, the following schedule is used:
Time on Deposit Interest Rate
Greater than or equal to 5 years .040
Less than 5 years but greater than or equal to 4 years .035
Less than 4 years but greater than or equal to 3 years .030
Less than 3 years but greater than or equal to 2 years .025
Less than 2 years but greater than or equal to 1 year .020
Less than 1 year .015
Write, compile, and run a C++ program that accepts the time funds are left on deposit and
displays the interest rate corresponding with the time entered.
#include <iostream>
using namespace std;
int main(){
float time, interest;
cin >>time;
if(time > 5){
cout << .040 << endl;}
else if( time < 5 && time>= 4){
cout << .035 << endl;}
else if(time < 4 && time >=3){
cout << .030 << endl;}
else if(time <3 && time >=2){
cout << .025 << endl;}
else if(time <2 && time >= 1){
cout << .020 << endl;}
else if(time < 1){
cout << .015 << endl;}
return 0;
}
5. (Program) Fluid flowing through a pipe can flow in a smooth, constant manner, called laminar
flow; in a chaotic manner, called turbulent flow; or in an intermediate stage between smooth
and turbulent flow, which is called transitional flow. In practice, a value known as the Reynolds
number can be used to determine the type of flow. For a Reynolds number below 2000, the
flow is laminar, and for a Reynolds number above 3000, the flow is turbulent. For a Reynolds
number between 2000 and 3000, the flow is transitional.
Using this information, write, compile, and run a C++ program that accepts a Reynolds number
as user input and displays a message indicating whether the flow is laminar, turbulent,
or transitional.
#include <iostream>
using namespace std;
int main(){
double numReynolds;
cin>> numReynolds;
if( numReynolds < 2000){
cout << "The flow is laminar" << endl;}
else if(numReynolds >= 2000 && numReynolds <= 3000){
cout << "The flow is transitional" << endl;}
else if (numReynolds > 3000){
cout << "The flow is turbulent" << endl;}
return 0;}
6. (Program) The tolerance of critical components in a system is determined according to the
following schedule:
Specification Status Tolerance
Space exploration Less than 0.1%
Military grade Greater than or equal to 0.1% and less than 1%
Commercial grade Greater than or equal to 1% and less than 10%
Toy grade Greater than or equal to 10%
Using this information, write, compile, and run a C++ program that accepts a component’s
tolerance reading and determines the specification that should be assigned to it.
#include <iostream>
using namespace std;
int main(){
double tolerence;
cin >> tolerence;
if(tolerence < .01){
cout << "Space exploration "<<endl;}
else if(tolerence >= 0.1 && tolerence <= 1 ){
cout << "Military grade"<<endl;}
else if (tolerence >= 1 && tolerence <= 10){
cout << "Commercial grade" << endl;}
return 0;
}
7. (Program) a. Write, compile, and run a program that accepts two real numbers and a select
code from a user. If the entered select code is 1, have the program add the two previously
entered numbers and display the result; if the select code is 2, the numbers should be multiplied;
and if the select code is 3, the first number should be divided by the second number.
#include <iostream>
using namespace std;
int main(){
int num1,num2,result,select;
cout << "Enter first number"<< endl;
cin >> num1;
cout << "Enter second number"<< endl;
cin >> num2;
cout << "Enter select code"<< endl;
cin >> select;
result = num1 + num2;
if(select == 1){
cout << result << endl;}
else if(select == 2){
cout << num1 * num2 << endl;}
else if(select == 3){
cout << num1 /num2 << endl;}
return 0;
}
b. Determine what the program written in Exercise 7a does when the entered numbers are
3 and 0 and the select code is 3.
The following is displayed Unhandled exception at 0x001B4B3A in Exercise.exe: 0xC0000094: Integer division by zero.
c. Modify the program written in Exercise 7a so that division by 0 isn’t allowed, and a message
is displayed when this division is attempted.
#include <iostream>
using namespace std;
int main(){
int num1,num2,result,select;
cout << "Enter first number"<< endl;
cin >> num1;
cout << "Enter second number"<< endl;
cin >> num2;
cout << "Enter select code"<< endl;
cin >> select;
result = num1 + num2;
if(select == 1){
cout << result << endl;}
else if(select == 2){
cout << num1 * num2 << endl;}
else if (select == 3 && num2 != 0){
cout << num1 / num2 << endl;
}
if (select == 3 && num2 == 0){
cout << "This action is not allowed" << endl;
}
return 0;
}
8. (Program) The quadrant in which a line starting from the origin is located is determined by
the angle the line makes with the positive x axis, as follows:
Angle from the Positive x Axis Quadrant
Between 0 and 90 degrees I
Between 90 and 180 degrees II
Between 180 and 270 degrees III
Between 270 and 360 degrees IV
a. Using this information, write, compile, and run a C++ program that accepts the angle of the
line as user input and determines and displays the correct quadrant for the input data. (Note:
If the angle is exactly 0, 90, 180, or 270 degrees, the corresponding line doesn’t reside in any
quadrant but lies on an axis.)
#include <iostream>
using namespace std;
int main(){
int x;
if(x > 0 && x<90){
cout << "Quadrant I" << endl;}
else if( x > 90 && x < 180)
{cout << "Quadrant II" << endl;}
else if(x> 180 && x < 270){
cout << "Quadrant III" << endl;}
else if(x>270 && x< 360){
cout << "Quadrant IV" << endl;}
else if (x == 0 || x == 90 || x == 180 || x== 270){
cout << "Axis" << endl;
return 0;
}
b. Modify the program written for Exercise 8a to display a message that identifies an angle of
0 degrees as the positive x axis, an angle of 90 degrees as the positive y axis, an angle of
180 degrees as the negative x axis, and an angle of 270 degrees as the negative y axis.
#include <iostream>
using namespace std;
int main(){
int x;
if(x > 0 && x<90){
cout << "Quadrant I" << endl;}
else if( x > 90 && x < 180)
{cout << "Quadrant II" << endl;}
else if(x> 180 && x < 270){
cout << "Quadrant III" << endl;}
else if(x>270 && x< 360){
cout << "Quadrant IV" << endl;}
else if (x == 0 ){
cout << "The positive x axis";
}
else if ( x == 90){
cout << "The positive y axis";
}
else if ( x == 180){
cout << "The negative x axis";
}
else if ( x== 270){
cout << "The negative y axis";
}
return 0;
}
9. (Program) Write, compile, and run a C++ program that accepts a number followed by one
space and then a letter. If the letter following the number is f, the program is to treat the
number entered as a temperature in degrees Fahrenheit, convert the number to the equivalent
degrees Celsius, and display a suitable message. If the letter following the number is c,
the program is to treat the number entered as a temperature in degrees Celsius, convert the
number to the equivalent degrees Fahrenheit, and display a suitable message. If the letter is
neither f nor c, the program is to display a message that the data entered is incorrect and then
terminate. Use an if-else chain in your program and make use of these conversion formulas:
Celsius = (5.0 / 9.0) × (Fahrenheit - 32.0)
Fahrenheit = (9.0 / 5.0) × Celsius + 32.0
#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;
}
10. (Program) Many states base yearly car registration fees on an automobile’s model year and
weight, using a schedule similar to the following:
Model Year Weight Weight Class Registration Fee
1990 or earlier Less than 2700 lbs 1 26.50
2700 to 3800 lbs 2 35.50
More than 3800 lbs 3 56.50
1991 to 1999 Less than 2700 lbs 4 35.00
2700 to 3800 lbs 5 45.50
More than 3800 lbs 6 62.50
2000 or later Less than 3500 lbs 7 49.50
3500 or more lbs 8 62.50
Using this information, write, compile, and run a C++ program that accepts an automobile’s
year and weight and determines and displays its weight class and registration fee.
#include <iostream>
using namespace std;
int main() {
double year, weight;
cout << "Enter your year and weight" << endl;
cin >> year >> weight;
if( year < 1900){
if (weight < 2700){
cout << "Weight Class and Registration Fee are " << 1 << "and" << 26.50;
}
else if (weight >2700 && weight < 3800){
cout << "Weight Class and Registration Fee are " << 2 << "and" << 35.50;}
else{cout<< "Weight Class and Registration Fee are" << 3 << "and << 56.50; }
}
else if ( year > 1991 && year < 1999){
if (weight < 2700){
cout << "Weight Class and Registration Fee are " << 4 << "and" << 35.50;
}
else if (weight >2700 && weight < 3800){
cout << "Weight Class and Registration Fee are " << 5 << "and" << 45.50;}
else{cout<< "Weight Class and Registration Fee are" << 6 << "and << 62.50; }
}
else { if (weight < 2700){
cout << "Weight Class and Registration Fee are " << 7 << "and" << 49.50;
}
else if (weight >2700 && weight < 3800){
cout << "Weight Class and Registration Fee are " << 8 << "and" << 62.50;}
}
return 0;
}
11. (Debug) Using the commission schedule from Program 4.5, the following program calculates
monthly income:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double monthlySales, income;
cout << “Enter the value of monthly sales: “;
cin >> monthlySales;
if (monthlySales >= 50000.00)
income = 375.00 + .16 * monthlySales;
if (monthlySales >= 40000.00 && monthlySales < 50000.00)
income = 350.00 + .14 * monthlySales;
if (monthlySales >= 30000.00 && monthlySales < 40000.00)
income = 325.00 + .12 * monthlySales;
if (monthlySales >= 20000.00 && monthlySales < 30000.00)
income = 300.00 + .09 * monthlySales;
if (monthlySales >= 10000.00 && monthlySales < 20000.00)
income = 250.00 + .05 * monthlySales;
if (monthlySales < 10000.00)
income = 200.00 + .03 * monthlySales;
cout << setiosflags(ios::showpoint)
<< setiosflags(ios:: fixed)
<< setprecision(2)
<< “\n\nThe income is $” << income << endl;
return 0;
}
a. Will this program produce the same output as Program 4.5?
Both programs produce the same output.
b. Which program is better? Why?
Program 4.5 is better because it requires less processing. In Program 4.5, when the correct
monthly sales figure is evaluated, the income value is calculated and the if-else statement
is exited without further processing. In the program in Exercise 11a, the system must
evaluate every if statement, even if the first one is the statement that evaluates to true.
Consequently, Program 4.5 is better because it potentially requires less processing.
12. (Debug) The following program was written to produce the same result as Program 4.5:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double monthlySales, income;
cout << “Enter the value of monthly sales: “;
cin >> monthlySales;
if (monthlySales < 10000.00)
income = 200.00 + .03 * monthlySales;
else if (monthlySales >= 10000.00)
income = 250.00 + .05 * monthlySales;
else if (monthlySales >= 20000.00)
income = 300.00 + .09 * monthlySales;
else if (monthlySales >= 30000.00)
income = 325.00 + .12 * monthlySales;
else if (monthlySales >= 40000.00)
income = 350.00 + .14 * monthlySales;
else if (monthlySales >= 50000.00)
income = 375.00 + .16 * monthlySales;
cout << setiosflags(ios::showpoint)
<< setiosflags(ios:: fixed)
<< setprecision(2)
<< “The income is $” << income << endl;
return 0;
}
a. What does this program do?
The program runs but calculates incorrect results, except for the case in which monthly
sales are less $20,000.
b. For what values of monthly sales does this program calculate the correct income?
If monthly sales are less than 20000, the program produces correct results because either the
first if or first else-if is executed. For any and all amounts >= 20000, the income for the
first else-if statement is obtained, and the other else-if statements aren’t evaluated.