-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 3.4 (Completed)
444 lines (404 loc) · 17.6 KB
/
Exercise 3.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
1. (Practice) For the following declaration statements, write one or more statements using the
cin object that causes the computer to pause while the user enters the appropriate data:
a. int firstnum;
cin >> firstnum;
b. double grade;
cin >> grade;
c. double secnum;
cin >> secnum;
d. char keyval;
cin >> keyval;
e. int month, years;
double average;
cin >> month >>years >> average;
f. char ch;
int num1,num2;
double grade1,grade2;
cin >> ch >> num1 >> num2 >> grade1 >> grade2;
g. double interest, principal, capital;
double price, yield;
cin >> interest >> principal >> capital >> price >> yield;
h. char ch,letter1,letter2;
int num1,num2,num3;
cin >> ch >> letter1 >> letter2 >> num1 >> num2 >> num3;
i. double temp1,temp2,temp3;
double volts1,volts2;
cin >> temp1 >> temp2 >> temp3 >> volts1 >> volts2;
2. (Program) a. Write, compile, and run a C++ program that displays the following prompts:
Enter the length of the room:
Enter the width of the room:
After each prompt is displayed, your program should use a cin object call to accept data from
the keyboard for the displayed prompt. After the width of the room is entered, your program
should calculate and display the area of the room. The area displayed should be calculated by
using the formula area = length × width and should be included in an appropriate message.
#include <iostream>
using namespace std;
int main(){
int area, length, width;
cout << "Enter the length of the room:" << endl;
cin >>length;
cout << "Enter the width of the room:" <<endl;
cin >> width;
area = lenght * width;
cout << area <<endl;
return 0;
}
b. Check the area displayed by the program written for Exercise 2a by calculating the result
manually.
Done!!
3. (Program) a. Write, compile, and run a C++ program that displays the following prompts:
Enter the length of the swimming pool:
Enter the width of the swimming pool:
Enter the average depth of the swimming pool:
After each prompt is displayed, your program should use a cin statement to accept data from
the keyboard for the displayed prompt. After the depth of the swimming pool is entered, your
program should calculate and display the volume of the pool. The volume should be calculated
with the formula volume = length × width × average depth and be displayed in an output
message.
#include <iostream>
using namespace std;
int main(){
int length, width, average,volume;
cout << "Enter the length of the swimming pool:" <<endl;
cin >> length;
cout << "Enter the width of the swimming pool:" <<endl;
cin >> width;
cout << "Enter the average depth of the swimming pool: << endl;
cin >> average >> endl;
cout << volume << endl;
return 0;
}
b. Check the volume displayed by the program written for Exercise 3a by calculating the
result manually.
Done!!
4. (Program) Write, compile, and run a C++ program that displays the following prompt:
Enter the radius of a circle:
After accepting a value for the radius, your program should calculate and display the area of
the circle. (Hint : Area = 3.1416 × radius^2.) For testing purposes, verify your program by using
an input radius of 3 inches. After manually determining that your program’s result is correct,
use your program to complete the following chart:
Radius (in) Area (sq. in)
1.0 3.1416
1.5 7.0686
2.0 12.5664
2.5 19.635
3.0 28.2744
3.5 38.4846
#include <iostream>
using namespace std;
int main(){
double Area,radius;
cout<< " Enter the radius of a circle: " << endl;
cin >> radius;
Area = 3.1416 * pow(radius, 2);
cout << Area << endl;
return 0;
5. (Program) a. Write a C++ program that first displays the following prompt:
Enter the temperature in degrees Celsius:
Have your program accept a value entered from the keyboard and convert the temperature
entered to degrees Fahrenheit, using this formula:
Fahrenheit = (9.0 / 5.0) × Celsius + 32.0
Your program should then display the temperature in degrees Fahrenheit with an output
message.
#include <iosteam>
using namespace std;
int main(){
double Fahrenheit, Celsius;
cout << "Enter the temperature in degress Celsius:" << endl;
cin >> Celsius;
Fahrenheit = (9/5) * Celsius + 32.0;
cout << Fahrenheit << endl;
return 0;
}
b. Compile and run the program written for Exercise 5a. To verify your program, use the following
test data and calculate the Fahrenheit equivalents by hand, and then use your program
to see whether you get the same results:
Test data set 1: 0 degrees Celsius 32
Test data set 2: 50 degrees Celsius 82
Test data set 3: 100 degrees Celsius 132
When you’re sure your program is working correctly, use it to complete the following chart:
Celsius Fahrenheit
45 77
50 82
55 87
60 92
65 97
70 102
6. (Program) a. Write, compile, and run a C++ program that displays the following prompts:
Enter the miles driven:
Enter the gallons of gas used:
After each prompt is displayed, your program should use a cin statement to accept data from
the keyboard for the displayed prompt. After the number for gallons of gas used has been
entered, your program should calculate and display the miles per gallon (mpg). This value
should be calculated with the formula miles per gallon = miles / gallons used and displayed in an
output message. Verify your program by using the following test data:
Test data set 1: miles = 276, gas = 10 gallons // 27.6 gallon
Test data set 2: miles = 200, gas = 15.5 gallons // 12.9032 gallon
#include <iostream>
using namespace std;
int main(){
double miles, gallons, gallon;
cout << "Enter the miles driven:" << endl;
cin >> miles;
cout << "Enter the gallons of gas used:" << endl;
cin >> gallons;
gallon = miles/gallons;
cout << gallon << endl;
return 0;
}
After finishing your verification, use your program to complete the following chart. (Make sure to
convert the miles driven to kilometers driven, convert gallons used to liters used, and then compute
the kilometers per liter. There are 1.61 kilometers per mile and 4.54609 liters per gallon.)
Miles Driven Gallons Used Mpg Km Driven Liters Used Km/L
250 16.00 15.625 402.5 72.73744 5.5336
275 18.00 15.2778 442.75 81.82962 5.41063
312 19.54 15.9672 502.32 88.8305986 5.65481
296 17.39 17.0213 476.56 79.0565051 6.02809
b. For the program written for Exercise 6a, determine how many verification runs are required
to make sure the program is working correctly, and give a reason to support your answer.
Only one is required if you use a miles and gallon pattern that result in an answer
that can't be easily created by mistake. Making the change of the function results being value very high.
7. (Program) a. Write, compile, and run a C++ program that displays the following prompts:
Enter a number:
Enter a second number:
Enter a third number:
Enter a fourth number:
After each prompt is displayed, your program should use a cin statement to accept a number
from the keyboard for the displayed prompt. After the fourth number has been entered, your
program should calculate and display the average of the numbers. The average should be
displayed in an output message. Check the average your program calculates by using the following
test data:
Test data set 1: 100, 100, 100, 100 // 100
Test data set 2: 100, 0, 100, 0 // 50
#include <iostream>
using namespace std;
int main(){
double number, second_number, third_number, fourth_number,average;
cout << "Enter a number:" << endl;
cin >> number;
cout << "Enter a second number:" << endl;
cin >> second_number;
cout << "Enter a third number:" << endl;
cin >> third_number;
cout << "Enter a fourth number:" << endl;
cin >> fourth_number;
average = (number + second_number + third_number + fourth_number)/ 4;
cout <<"\n" << average <<endl;
return 0;
}
After finishing your verification, use your program to complete the following chart:
Numbers Average
92, 98, 79, 85 88.5
86, 84, 75, 86 82.75
63, 85, 74, 82 76
b. Repeat Exercise 7a, making sure you use the same variable name, number, for each number
input. Also, use the variable sum for the sum of the numbers. (Hint: To do this, you can use
the statement sum = sum + number; after each number is accepted. Review the material
on accumulating in Section 3.1.)
#include <iostream>
using namespace std;
int main()
{
double number, average, sum = 0;
cout << "Enter a number:" << endl;
cin >> number;
sum = sum + number;
cout << "Enter a second number:" << endl;
cin >> number;
sum = sum + number;
cout << "Enter a third number:" << endl;
cin >> number;
sum = sum + number;
cout << "Enter a fourth number:" << endl;
cin >> number;
sum = sum + number ;
average = sum / 4;
cout << "\n" << average << endl;
system("PAUSE");
return 0;
}
8. (Program) The perimeter, approximate surface area, and approximate volume of an in-ground
pool are given by the following formulas:
perimeter = 2 × (length + width)
volume = length × width × average depth
underground surface area = 2 × (length + width) × average depth + length × width
Using these formulas as a basis, write a C++ program that accepts the length, width, and average
depth measurements, and then calculates the pool’s perimeter, volume, and underground surface
area. In writing your program, make these two calculations immediately after entering the
input data: length × width and length + width. The results of these two calculations should be used,
as needed, in the assignment statements for determining the perimeter, volume, and underground
surface area without recalculating them for each equation.
#include <iostream>
using namespace std;
int main(){
double perimeter, volume, underground_surface_area, length, width, average_depth;
cout << "Enter length: " <<endl;
cin >> length;
cout << "Enter width: " << endl;
cin >> width;
cout << "Enter average depth: " << endl;
cin >> average_depth;
perimeter = 2 * (length + width);
cout << "Perimeter: " << perimeter <<endl;
volume = length * width * average_depth;
cout << "Volume: " << volume << endl;
underground_surface_area = 2 * (length + width) * average_depth + length * width;
cout <<"Underground Surface area: " << underground_surface_area << endl;
return 0;
}
Verify your program’s results by doing a hand calculation, using the following test data: length = 25 feet, width = 15 feet, and
average depth = 5.5 feet.
Perimeter: 80
Volume: 2062.5
Underground Surface Area: 815
After verifying that your program is working, use it to complete the following
chart:
Length Width Average Perimeter Volume Underground
Depth Surface Area
25 10 5.0 70 1250 600
25 10 5.5 70 1375 635
25 10 6.0 70 1500 670
25 10 6.5 70 1625 705
30 12 5.0 84 1800 780
30 12 5.5 84 1980 822
30 12 6.0 84 2160 864
30 12 6.5 84 2340 906
9. (Program) a. Write, compile, and run a C++ program to compute and display the value of the
second-order polynomial ax2 + bx + c for any user-entered values of the coefficients a, b, and c
and the variable x. Have your program display a message first to inform users what the program
does, and then display suitable prompts to alert users to enter data. (Hint: Use a prompt such
as Enter the coefficient of the x-squared term:.)
#include <math.h>
#include <iostream>
using namespace std;
int main(){
double a,b,c,x,value;
cout << "Enter the coefficient of the x-squared term:" <<endl;
cin >> x;
cout << "Enter the coefficient of the a-squared term:" <<endl;
cout << "Enter the coefficient of the b-squared term:" <<endl;
cout << "Enter the coefficient of the c-squared term:" <<endl;
value = (a*pow(x,2)) + (b*x) + c;
cout << "The value of the second-order polynimical is:" << endl;
return 0;
b. Check the result of your program written for Exercise 9a by using the following test data:
Test data set 1: a = 0, b = 0, c = 22, x = 56 // 22
Test data set 2: a = 0, b = 22, c = 0, x = 2 // 44
Test data set 3: a = 22, b = 0, c = 0, x = 2 // 88
Test data set 4: a = 2, b = 4, c = 5, x = 2 // 21
After finishing your verification, use your program to complete the following chart:
a b c x Polynomial Value (ax2 + bx + c)
2.0 17.0 -12.0 1.3 13.48
3.2 2.0 15.0 2.5 40
3.2 2.0 15.0 -2.5 30
-2.0 10.0 0.0 2.0 12
-2.0 10.0 0.0 4.0 8
-2.0 10.0 0.0 5.0 0
-2.0 10.0 0.0 6.0 -12
5.0 22.0 18.0 8.3 545.05
4.2 -16 -20 -5.2 175.768
10. (Program) Write, compile, and run a program that calculates and displays the square root
value of a user-entered real number.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
float number,square;
cout << Enter number to be squared<<endl;
cin << number;
square = sqrt(number);
cout << square <<endl;
return 0;
}
Verify your program by calculating the square roots of this
test data: 25, 16, 0, and 2.
5,4,0,1.41421 repectively
After finishing your verification,use your program to determine
the square roots of 32.25, 42, 48, 55, 63, and 79.
5.67891, 6.48074, 6.9282, 7.4162, 7.93725,8.88819
11. (Program) Write, compile, and run a program to calculate and display the fourth root of a
user-entered number. Recall from elementary algebra that you find the fourth root of a number
by raising the number to the 1⁄4 power. (Hint : Don’t use integer division—can you see
why?)
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double number, solution;
cout << "Enter number to be forth-squared:"<< endl;
cin >> number;
solution = pow(number,(.25));
cout << "The solution is " << solution <<endl;
return 0;
}
Verify your program by calculating the fourth root of this test data: 81, 16, 1, and 0.
3,2,1,0 respectively
When you’re finished, use your program to determine the fourth root of 42, 121, 256, 587, 1240, and
16,256.
2.54573, 3.31662,4, 4.9222, 5.93411 respectively
12. (Program) Program 3.12 prompts users to input two numbers; the first value entered is stored
in num1, and the second value is stored in num2. Using this program as a starting point,
write a program that swaps the values stored in the two variables.
#include <iostream>
using namespace std;
int main()
{
double num1, num2, product;
cout << “Please type in number 1: “;
cin >> num1;
cout << “Please type in number 2: “;
cin >> num2;
swap(num1,num2);
cout << "New num1 " << num1 << endl;
cout << "New num2 " << num2 << endl;
return 0;
}
13. (Program) Write a C++ program that prompts users to enter a number. Have your program
accept the number as an integer and display the integer immediately by using a cout statement.
Run your program three times. The first time, enter a valid integer number; the second
time, enter a double-precision number; and the third time, enter a character. Using the output
display, see what number your program actually accepted from the data you entered.
#include <iostream>
using namespace std;
int main(){
int number;
cout << "Enter your number" << endl;
cin >> number;
cout << number << endl;
return 0;
}
First time: Input: 5 Output: 5
Second time: Input: 3.45 Output: 3
Third time: Input: Cat Output: -858993460
14. (Program) Repeat Exercise 13, but have your program declare the variable used to store the
number as a double-precision variable. Run the program three times. The first time, enter an
integer; the second time, enter a double-precision number; and the third time, enter a character.
Using the output display, keep track of what number your program actually accepted from
the data you entered. What happened, if anything, and why?
The double-precision variable was able to send the correct numbers back for the first two runs.
The third run sent out useless data because the complied did not know what to do with it.
#include <iostream>
using namespace std;
int main(){
double number;
cout << "Enter your number" << endl;
cin >> number;
cout << number << endl;
return 0;
}
First time: Input: 5 Output: 5
Second time: Input: 3.45 Output: 3.45
Third time: Input: Cat Output: -9.25596e+061
15. (For thought) a. Why do you think successful programs contain extensive data-input validity
checks? (Hint: Review Exercises 13 and 14.)
The program contain extensive data-input validity checks because
they must be able to display information in a particual way in order to pass all test cases.
b. What do you think is the difference between a data-type check and a data-reasonableness
check?
A data type check checks the data type and how much control over the varable it has
while the dat-resonablenees check checks if the data is even understandable.
c. Assume that a program requests users to enter a month, day, and year. What are some checks
that could be made on the data entered?
check that the month is a character type
check that the day is an integer type
check that the year is an integer type