-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 6.2 (Completed)
695 lines (537 loc) · 18.9 KB
/
Exercise 6.2 (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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
1. (Modify) Rewrite Program 6.4 so that the findMax() function accepts two double-precision
arguments and returns a double-precision value to main(). Make sure to modify main() to
pass two double-precision values to findMax() and to accept and store the double-precision
value returned by findMax().
#include <iostream>
using namespace std;
double findMax(double, double); // the function prototype
int main()
{
double firstnum, secnum, max;
cout << "\nEnter a number : ";
cin >> firstnum;
cout << "Great!Please enter a second number : ";
cin >> secnum;
max = findMax(firstnum, secnum); // the function is called here
cout << "\nThe maximum of the two numbers is " << max << endl;
system("PAUSE");
return 0;
}
double findMax(double x, double y)
{ // start of function body
double maxnum; // variable declaration
if (x >= y) // find the maximum number
maxnum = x;
else
maxnum = y;
return maxnum; // return statement
}
2. (Practice) Write function headers for the following functions:
a. A function named check(), which has three parameters. The first parameter should accept
an integer number, the second parameter a floating-point number, and the third parameter
a double-precision number. The function returns no value.
void check(int num1, float num2, double num3)
b. A function named findAbs() that accepts a double-precision number passed to it and
returns that number’s absolute value
double findAbs(double x)
c. A function named mult() that accepts two floating-point numbers as parameters, multiplies
these two numbers, and returns the result.
float mult(float first, float second)
d. A function named square() that computes and returns the square of the integer value
passed to it.
int square(int number)
e. A function named powfun() that raises an integer number passed to it to a positive integer
power (also passed as an argument) and returns the result as an integer.
int powfun(int num, int exponent)
f. A function named table() that produces a table of numbers from 1 to 10, their squares, and
their cubes. No arguments are to be passed to the function, and the function returns no value.
void table(void) or void table()
3. (Program) a. Write a function named rightTriangle() that accepts the lengths of two sides
of a right triangle as the arguments a and b. The subroutine should determine and return the
hypotenuse, c, of the triangle. (Hint: Use Pythagoras’ theorem, c2 = a2 + b2.)
double rightTriangle(double a, double b)
{
return sqrt((a * a) + (b * b));
}
b. Include the function written for Exercise 3a in a working program. The main() function
should call rightTriangle() correctly and display the value the function returns. Test the
function by passing various data to it and verifying the returned value.
#include <math.h>
#include <cmath>
#include <iostream>
using namespace std;
double rightTriangle(double, double); // the function prototype
int main()
{
double num,num2;
cout << "Enter your number" << endl;
cin >> num;
cout << "Enter your other number" << endl;
cin >> num2;
cout << "The sqrt is " << rightTriangle(num,num2) << endl;
system("PAUSE");
return 0;
}
double rightTriangle(double a, double b)
{
return sqrt((a*a) + (b*b));
}
4. (Program) a. Write a C++ function named findAbs() that accepts a double-precision number
passed to it, computes its absolute value, and returns the absolute value to the calling
function. 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.
double findAbs(double number)
{
if(number < 0)
number = -number;
return number;
}
b. Include the function written in Exercise 4a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
double findAbs(double); // the function prototype
int main()
{
double number;
cout << "Enter your number" << endl;
cin >> number;
cout << "The number is now positve " << findAbs(number) << endl;
system("PAUSE");
return 0;
}
double findAbs(double number)
{
if (number < 0)
number = -number;
return number;
}
5. (Program) a. The volume, V, of a cylinder is given by the formula
V = π r^2 L
where r is the cylinder’s radius and L is its length. Using this formula, write a C++ function
named cylvol() that accepts a cylinder’s radius and length and returns its volume.
double cylvol(double r, double L)
{
double volume = 3.1416 * pow(r, 2)*L;
return volume;
}
b. Include the function written in Exercise 5a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <cmath>
#include <iostream>
using namespace std;
double cylvol(double, double); // the function prototype
int main()
{
double num,num2;
cout << "Enter your number" << endl;
cin >> num;
cout << "Enter your other number" << endl;
cin >> num2;
cout << "The volume of a cylinder is " << cylvol(num,num2) << endl;
system("PAUSE");
return 0;
}
double cylvol(double r, double L)
{
double volume = 3.1416 * pow(r, 2)*L;
return volume;
}
6. (Program) a. The surface area, S, of a cylinder is given by the formula
S = 2 π r l
where r is the cylinder’s radius, and l is the length of the cylinder. Using this formula, write a
C++ function named surfarea() that accepts a cylinder’s radius and length and returns its
surface area.
double surfarea(double r, double l)
{
double surf = 2 * 3.14 * r * l;
return surf;
}
b. Include the function written in Exercise 6a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <iostream>
using namespace std;
double surfarea(double, double); // the function prototype
int main()
{
double num,num2;
cout << "Enter your number" << endl;
cin >> num;
cout << "Enter your other number" << endl;
cin >> num2;
cout << "The volume of a cylinder is " << surfarea(num,num2) << endl;
system("PAUSE");
return 0;
}
double surfarea(double r, double l)
{
double surf = 2 * 3.14 * r * l;
return surf;
}
7. (Program) a. Write a function named totamt() that uses four parameters named quarters,
dimes, nickels, and pennies, which represent the number of each of these coins in a piggybank.
The function should determine the dollar value of the number of quarters, dimes,
nickels, and pennies passed to it and return the calculated value.
double totamt(double quarters, double dimes, double nickels, double pennies)
{
double value = (quarters * .25) + (dimes * .10) + (nickels * .05) + (pennies * .01);
return value;
}
b. Include the function written in Exercise 7a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <iostream>
using namespace std;
double totamt(double, double,double,double); // the function prototype
int main()
{
double num,num2,num3,num4;
cout << "Enter your number" << endl;
cin >> num;
cout << "Enter your other number" << endl;
cin >> num2;
cout << "Enter your third number" << endl;
cin >> num3;
cout << "Enter your forth number" << endl;
cin >> num4;
cout << "The volume of a cylinder is " << totamt(num,num2,num3,num4) << endl;
system("PAUSE");
return 0;
}
double totamt(double quarters, double dimes, double nickels, double pennies)
{
double value = (quarters * .25) + (dimes * .10) + (nickels * .05) + (pennies * .01);
return value;
}
8. (Program) a. Write a function named daycount() that accepts a month, day, and year as its
input arguments; calculates an integer representing the total number of days from the turn of
the century to the date that’s passed; and returns the calculated integer to the calling function.
For this problem, assume each year has 365 days and each month has 30 days. Test your function
by verifying that the date 1/1/00 returns a day count of 1.
double daycount(int month, double day, double year)
{
if (month % 30 == 0){
month = 30;
}
else if (month == 1){
month = 0;
}
else{ month = month * 30;}
double value = (day)+(month)+(year*365);
return value;
}
b. Include the daycount() function written for Exercise 8a in a working program. The
main() function should correctly call daycount() and display the integer returned by the
function. Test the function by passing various data to it and verifying the returned value.
#include <iostream>
using namespace std;
double daycount(int, double,double); // the function prototype
int main()
{
double num,num2,num3;
cout << "Enter your month" << endl;
cin >> num;
cout << "Enter your day " << endl;
cin >> num2;
cout << "Enter the year" << endl;
cin >> num3;
cout << "The total number of day passed sence the first of the year" << daycount(num,num2,num3) << endl;
system("PAUSE");
return 0;
}
double daycount(int month, double day, double year)
{
if (month % 30 == 0){
month = 30;
}
else if (month == 1){
month = 0;
}
else{ month = (month-1) * 30;} // only one I needed *facepam* lol
double value = (day)+(month)+(year*365);
return value;
}
9. (Program) a. A clever and simple method of preparing to sort dates into ascending (increasing)
or descending (decreasing) order is to convert a date in the form month/day/year into an
integer number with the formula date = year × 10000 + month × 100 + day. For example, using
this formula, the date 12/6/1999 converts to the integer 19991206, and the date 2/28/2011 converts
to the integer 20110228. Sorting the resulting integer numbers puts dates into the correct
order automatically. Using this formula, write a function named convertdays() that accepts
a month, day, and year; converts the passed data into a single date integer; and returns the
integer to the calling function.
int convertdays(int month, int day, int year)
{
int date = year * 10000 + month * 100 + day;
return date;
}
b. Include the convertdays() function written for Exercise 9a in a working program. The
main() function should call convertdays() correctly and display the integer the function
returns. Test the function by passing various data to it and verifying the returned value.
#include <iostream>
using namespace std;
int convertdays(int, int, int); // the function prototype
int main()
{
int num,num2,num3;
cout << "Enter your month" << endl;
cin >> num;
cout << "Enter your day " << endl;
cin >> num2;
cout << "Enter the year" << endl;
cin >> num3;
cout << "The total number of day passed sence the first of the year" << convertdays(num,num2,num3) << endl;
system("PAUSE");
return 0;
}
int convertdays(int month, int day, int year)
{
int date = year * 10000 + month * 100 + day;
return date;
}
10. (Program) a. Write a function named ReadOneChar() that reads a key pressed on the keyboard
and displays the integer code of the entered character.
int ReadOneChar(char reads)
{
return reads;
}
b. Include the ReadOneChar() function written for Exercise 10a in a working program. The
main() function should correctly call ReadOneChar() and display the integer the function
returns. Test the function by passing various data to it and verifying the returned value.
#include <iostream>
using namespace std;
int ReadOneChar(char); // the function prototype
int main()
{
char one;
cout << "Enter your character" << endl;
cin >> one;
cout << "The key pressed was " << ReadOneChar(one) << endl;
system("PAUSE");
return 0;
}
int ReadOneChar(char reads)
{
return reads;
}
11. (Program) Heron’s formula for the area, A, of a triangle with sides of length a, b, and c is
A =[s(s)(s-a)(s-b)(s-c)]
where
s = (a + b + c)/2
Write, test, and execute a function that accepts the values of a, b, and c as parameters from a
calling function, and then calculates the values of s and [s(s - a)(s - b)(s - c)]. If this quantity is
positive, the function calculates A. If the quantity is negative, a, b, and c do not form a triangle,
and the function should set A = -1. The value of A should be returned by the function. Test
the function by passing various data to it and verifying the returned value.
#include <iostream>
using namespace std;
int HeronForm(int,int,int); // the function prototype
int main()
{
int s,a,b,c;
cout << "Enter a" << endl;
cin >> a;
cout << "Enter b" << endl;
cin >> b;
cout << "Enter c" << endl;
cin >> c;
cout << "The Heron's Area is " << HeronForm( a, b, c) << endl;
system("PAUSE");
return 0;
}
int HeronForm( int a, int b, int c)
{
int Area;
int s = ((a + b + c) / 2);
Area = (s*(s)*(s - a)*(s - b)*(s - c));
if ((Area == -(Area))){
Area = -1;
}
return Area;
}
12. (Program) a. Write a function named whole() that returns the integer part of any number
passed to the function. (Hint: Assign the passed argument to an integer variable.)
int whole( int a)
{
return a;
}
b. Include the function written in Exercise 12a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <iostream>
using namespace std;
int whole(int); // the function prototype
int main()
{
int s,a,b,c;
cout << "Enter a" << endl;
cin >> a;
cout << "The integer part of any number is " << whole(a) << endl;
system("PAUSE");
return 0;
}
int whole( int a)
{
return a;
}
13. (Program) a. Write a C++ function named fracpart() that returns the fractional part of any
number passed to it. For example, if the number 256.879 is passed to fracpart(), the number
0.879 should be returned. Have fracpart() call the whole() function you wrote in
Exercise 12. The number returned can then be determined as the number passed to
fracpart() less the returned value when the same argument is passed to whole(). The
completed program should consist of main() followed by fracpart() followed by whole().
double fracpart( double a)
{
double b;
double frac=modf(a, &b);
return frac;
}
b. Include the function written in Exercise 13a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <iostream>
using namespace std;
double fracpart(double); // the function prototype
int main()
{
double a;
cout << "Enter a" << endl;
cin >> a;
cout << "The integer part of any number is " << fracpart(a) << endl;
system("PAUSE");
return 0;
}
double fracpart( double a)
{
double b;
double frac=modf(a, &b);
return frac;
}
14. (Program) a. Years that are evenly divisible by 400 or are evenly divisible by 4 but not by 100
are leap years. For example, because 1600 is evenly divisible by 400, 1600 was a leap year.
Similarly, because 1988 is evenly divisible by 4 but not by 100, it was also a leap year. Using
this information, write a C++ function that accepts the year as user input and returns a 1 if the
passed year is a leap year or a 0 if it isn’t.
int leapyear( int num)
{
if (num % 4 == 0 && num % 100 != 0){
num = 1;
}
else{ num = 0; }
return num;
}
b. Include the function written in Exercise 14a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <iostream>
using namespace std;
int leapyear(int); // the function prototype
int main()
{
int a;
cout << "Enter a" << endl;
cin >> a;
cout << "The integer part of any number is " << leapyear(a) << endl;
system("PAUSE");
return 0;
}
int leapyear( int num)
{
if (num % 4 == 0 && num % 100 != 0){
num = 1;
}
else{ num = 0; }
return num;
}
15. (Program) a. A second-degree polynomial in x is given by the expression ax2 + bx + c, where a,
b, and c are known numbers and a is not equal to 0. Write a C++ function named polyTwo
(a,b,c,x) that computes and returns the value of a second-degree polynomial for any passed
values of a, b, c, and x.
int polyTwo( int a, int b, int c, int x)
{
int poly = a*pow(x, 2) + b*x + c;
return poly;
}
b. Include the function written in Exercise 15a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <cmath>
#include <iostream>
using namespace std;
int polyTwo(int,int,int,int); // the function prototype
int main()
{
int a,b,c,x;
cout << "Enter a" << endl;
cin >> a;
cout << "Enter b" << endl;
cin >> b;
cout << "Enter c" << endl;
cin >> c;
cout << "Enter x" << endl;
cin >> x;
cout << "The second polynomical is " << polyTwo(a,b,c,x) << endl;
system("PAUSE");
return 0;
}
int polyTwo( int a, int b, int c, int x)
{
int poly = a*pow(x, 2) + b*x + c;
return poly;
}
16. (Program) a. The following is a useful programming algorithm for rounding a real number to
n decimal places:
Step 1: Multiply the number by 10^n.
Step 2: Add 0.5.
Step 3: Delete the fractional part of the result.
Step 4: Divide by 10^n.
For example, using this algorithm to round the number 78.374625 to three decimal places
yields:
Step 1: 78.374625 × 10^3 = 78374.625
Step 2: 78374.625 + 0.5 = 78375.125
Step 3: Retaining the integer part = 78375
Step 4: 78375 divided by 10^3 = 78.375
Using this algorithm, write a C++ function that accepts a user-entered value and returns the
result rounded to two decimal places.
double realTwoNum(double num, int& num2)
{
num=(num * pow(10, 2));
num = num + .5;
num2 = (int) num;
double done = (num2 / pow(10, 2));
return done;
}
b. Include the function written in Exercise 16a in a working program. Make sure your function
is called from main() and returns a value to main() correctly. Have main() use a cout
statement to display the returned value. Test the function by passing various data to it and
verifying the returned value.
#include <cmath>
#include <iostream>
using namespace std;
double realTwoNum(double,int&); // the function prototype
int main()
{
double num;
int num2;
cout << "Enter a" << endl;
cin >> num;
cout << "The number with two decimal places " << realTwoNum(num,num2) << endl;
system("PAUSE");
return 0;
}
double realTwoNum(double num, int& num2)
{
num=(num * pow(10, 2));
num = num + .5;
num2 = (int) num;
double done = (num2 / pow(10, 2));
return done;
}