-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 6.1 (Completed)
450 lines (328 loc) · 12.9 KB
/
Exercise 6.1 (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
445
446
447
448
449
450
1. (Practice) For the following function headers, determine the number, type, and order
(sequence) of the values that must be passed to the function:
a. void factorial(int n)
requires one int value
b. void price(int type, double yield, double maturity)
requires three values in this order: an int and two doubles
c. void yield(int type, double price, double maturity)
requires three values in this order: an int and two doubles
d. void interest(char flag, double price, double time)
requires three values in this order: a char and two doubles
e. void total(double amount, double rate)
requries two doubles
f. void roi(int a, int b, char c, char d, double e, double f)
requires six values in this order: two ints, two chars and two doubles
g. void getVal(int item, int iter, char decflag, char delim)
requires four values in this order: two ints and two chars
2. (Practice) a. Write a function named check() that has three parameters. The first parameter
should accept an integer number, and the second and third parameters should accept a doubleprecision
number. The function body should just display the values of data passed to the function
when it’s called. (Note: When tracing errors in functions, having the function display values
it has been passed is helpful. Quite often, the error isn’t in what the function body does with
data, but in the data received and stored.)
void check(int num1, double num2, double num3){
cout << "In check()\n";
cout << "The value of num1 is " << num1 << endl;
cout << "The value of num2 is " << num2 << endl;
cout << "The value of num3 is " << num3 << endl;
return;
}
b. Include the function written in Exercise 2a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void check(int num1, double num2, double num3){
cout << "In check()\n";
cout << "The value of num1 is " << num1 << endl;
cout << "The value of num2 is " << num2 << endl;
cout << "The value of num3 is " << num3 << endl;
}
int main(){
check( 10 ,20 ,30);
system("PAUSE");
return 0;
}
3. (Practice) a. Write a function named findAbs() that accepts a double-precision number
passed to it, computes its absolute value, and displays the absolute value. A number’s absolute
value is the number itself if the number is positive and the negative of the number if the
number is negative.
#include <iostream>
using namespace std;
void findAbs(double num){
num = abs(num);
cout << num << endl;
}
b. Include the function written in Exercise 3a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void findAbs(double num){
num = abs(num);
cout << num << endl;
}
int main(){
findAbs( -10);
system("PAUSE");
return 0;
}
4. (Practice) a. Write a function called mult() that accepts two double-precision numbers as
parameters, multiplies these two numbers, and displays the result.
void mult(double num, double num2){
int result = num2 *num;
cout << result<< endl;
}
b. Include the function written in Exercise 4a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void mult(double num, double num2){
int result = num2 *num;
cout << result<< endl;
}
int main(){
mult( 50,20);
system("PAUSE");
return 0;
}
5. (Practice) a. Write a function named sqrIt() that computes the square of the value passed
to it and displays the result. The function should be capable of squaring numbers with decimal
points.
void sqrtIt(double num){
double result = sqrt(num);
cout << result << endl;
}
b. Include the function written in Exercise 5a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void sqrtIt(double num){
double result = sqrt(num);
cout << result << endl;
}
int main(){
sqrtIt( 50);
system("PAUSE");
return 0;
}
6. (Practice) a. Write a function named powfun() that raises an integer number passed to it to
a positive integer power and displays the result. The positive integer should be the second
value passed to the function. Declare the variable used to store the result as a long-integer data
type to ensure enough storage for the result.
void powfun(double num, double num2){
double result = pow(num,num2);
cout << result << endl;
}
b. Include the function written in Exercise 6a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void powfun(double num, double num2){
double result = pow(num,num2);
cout << result << endl;
}
int main(){
powfun( 50, 2);
system("PAUSE");
return 0;
}
7. (Practice) a. Write a function that produces a table of the numbers from 1 to 10, their squares,
and their cubes. The function should produce the same display as Program 5.10.
void sqrtfun(){
cout << "SQUARE CUBE" << endl;
cout << "-------- - ------------" << endl;
for (double num = 1; num <= 10; num++){
cout << num * num << " "
<< num * num * num << endl;
}
}
b. Include the function written in Exercise 7a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void sqrtfun(){
cout << "SQUARE CUBE" << endl;
cout << "-------- - ------------" << endl;
for (double num = 1; num <= 10; num++){
cout << num * num << " "
<< num * num * num << endl;
}
}
int main(){
sqrtfun( );
system("PAUSE");
return 0;
}
8. (Modify) a. Modify the function written for Exercise 7a to accept the starting value of the
table, the number of values to be displayed, and the increment between values. If the increment
isn’t set explicitly, the function should use a default value of 1. Name your function
selTab(). A call to selTab(6,5,2); should produce a table of five lines, the first line starting
with the number 6 and each succeeding number increasing by 2.
void sqrtfun(int start, int maxnum, int increment){
cout << "SQUARE CUBE" << endl;
cout << "-------- - ------------" << endl;
for (double num = start; num <=(start + maxnum *increment)-1; num = num + increment){
cout << num * num << " "
<< num * num * num << endl;
}
}
b. Include the function written in Exercise 8a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
void sqrtfun(int start, int maxnum, int increment){
cout << "SQUARE CUBE" << endl;
cout << "-------- - ------------" << endl;
for (double num = start; num <=(start + maxnum *increment)-1; num = num + increment){
cout << num * num << " "
<< num * num * num << endl;
}
}
int main(){
sqrtfun( 6,5,2);
system("PAUSE");
return 0;
}
9. (Program) a. The time in hours, minutes, and seconds is to be passed to a function named
totsec(). Write totsec() to accept these values, determine the total number of seconds in
the passed data, and display the calculated value.
void totsec(int hours, int minutes, int seconds){
int total = (hours * 60 * 60) + (minutes * 60) + seconds;
cout << "Total Seconds equals " << total << endl;
}
b. Include the totsec() function written for Exercise 9a in a working program. The main()
function should correctly call totsec() and display the value the function returns. Use
the following test data to verify your program’s operation: hours = 10, minutes = 36, and
seconds = 54. Make sure to do a hand calculation to verify the result your program displays.
#include <iostream>
using namespace std;
void totsec(int hours, int minutes, int seconds){
int total = (hours * 60 * 60) + (minutes * 60) + seconds;
cout << "Total Seconds equals " << total << endl;
}
int main(){
totsec(10,36,54);
system("PAUSE");
return 0;
}
10. (Program) a. The volume, V, of a sphere is given by this formula, where r is the sphere’s
radius:
Volume = (4π r^3)/3
Using this formula, write, compile, and run a C++ function named spherevol() that accepts
a sphere’s radius and then calculates and displays its volume.
void spherevol(double radius){
double pi = 3.1416;
double total = (((4 * pi) * pow(radius, 3)) / 3);
cout << total << endl;
}
b. Include the function written in Exercise 10a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void spherevol(double radius){
double pi = 3.1416;
double total = (((4 * pi) * pow(radius, 3)) / 3);
cout << total << endl;
}
int main(){
spherevol(10);
system("PAUSE");
return 0;
}
11. (Program) a. Write and test a C++ function named makeMilesKmTable() to display a table
of miles converted to kilometers. The arguments to the function should be the starting and
stopping values of miles and the increment. The output should be a table of miles and their
equivalent kilometer values. Use the relationship that 1 mile = 1.61 kilometers.
void makeMilesKmTable(int start, int max, int increment){
cout << "Miles" << " = " << "Kilometers" << " " << "Miles" <<
"=" << "Kilometers" << endl;
for (int miles = start; miles <= max; miles = miles + increment){
double kilo = miles * 1.61;
cout << miles <<setw(20)<< kilo << endl ;
}
}
b. Modify the function written for Exercise 12a so that two columns are displayed. For example,
if the starting value is 1 mile, the ending value is 20 miles, and the increment is 1, the
display should look like the following:
Miles = Kilometers Miles = Kilometers
1 1.61 11 17.70
2 3.22 12 19.31
. . . .
. . . .
10 16.09 20 32.18
(Hint : Find split = (start + stop) / 2. Let a loop execute from miles = start to split, and calculate
and print across one line the values of miles and kilometers for both miles and (miles - start +
split + 1).)
#include <iostream>
using namespace std;
void makeMilesKmTable(int start, int max, int increment){
cout << "Miles" << " = " << "Kilometers" << endl;
double split = ((start + max) / 2);
for (int miles = start; miles <= split; miles = miles + increment){
double kilo = miles * 1.61;
cout << miles << setw(20) << kilo << endl;
}
cout << "Miles" << " = " << "Kilometers" << endl;
for (int miles2 = split + 1; miles2 <= max; miles2 = miles2 + increment){
double kilo = miles2 * 1.61;
cout << miles2 << setw(20) << kilo << endl;
}
}
int main(){
makeMilesKmTable(1,20,1);
system("PAUSE");
return 0;
}
12. (Program) a. Write a C++ function that accepts an integer argument, determines whether the
passed integer is even or odd, and displays the result of this determination. (Hint: Use the %
operator.)
void evenOrOdd(int num){
if (num % 2 == 0){
cout << "even" << endl;
}
else{ cout << "odd" << endl; }
}
b. Include the function written in Exercise 12a in a working program. Make sure your function
is called from main(). Test the function by passing various data to it.
#include <iostream>
using namespace std;
void evenOrOdd(int num){
if (num % 2 == 0){
cout << "even" << endl;
}
else{ cout << "odd" << endl; }
}
int main(){
evenOrOdd(1);
system("PAUSE");
return 0;
}
13. (Program) A useful function using no parameters can be constructed to return a value for π
that’s accurate to the maximum number of decimal places your computer allows. This value is
obtained by taking the arcsine of 1.0, which is π / 2, and multiplying the result by 2. In C++,
the required expression is 2.0 * asin(1.0); the asin() function is included in the standard
C++ mathematics library. (Remember to include cmath in your preprocessor directives.) Using
this expression, write a C++ function named pi() that calculates and displays the value of π.
(In the next section, you see how to return this value to the calling function.)
#include <cmath>
#include <iostream>
using namespace std;
void pi(){
double result =2.0 *asin(1.0);
cout << result << endl;
}
int main(){
pi();
system("PAUSE");
return 0;
}
14. (Program) a. Write a function template named display() that displays the value of the
single argument passed to it when the function is called.
void display(int result){
cout << result << endl;
}
b. Include the function template created in Exercise 14a in a complete C++ program that calls
the function three times: once with a character argument, once with an integer argument,
and once with a double-precision argument.
void display(char result3, int result, double result2){
cout << result3 << endl;
cout << result << endl;
cout << result2 << endl;
}