-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 6.3 (Completed)
306 lines (230 loc) · 8.56 KB
/
Exercise 6.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
1. (Practice) Write parameter declarations for the following:
a. A parameter named amount that will be a reference to a double-precision value
double& amount
b. A parameter named price that will be a reference to a double-precision number
double& price
c. A parameter named minutes that will be a reference to an integer number
int& minutes
d. A parameter named key that will be a reference to a character
char& key
e. A parameter named yield that will be a reference to a double-precision number
double yield
2. (Practice) Three integer arguments are to be used in a call to a function named time(). Write
a suitable function header for time(), assuming that time() accepts these variables as the
reference parameters sec, min, and hours and returns no value to its calling function.
void time(int& sec,int& min,int& hours)
3. (Modify) a. Rewrite the findMax() function in Program 6.4 so that the variable max,
declared in main(), is used to store the maximum value of the two passed numbers. The
value of max should be set from within findMax(). (Hint : A reference to max has to be
accepted by findMax().)
void findMax(int x, int y, int& max)
{ // start of function body
int maxnum; // variable declaration
if (x >= y) // find the maximum number
maxnum = x;
else
maxnum = y;
max = maxnum;
return; // return statement
}
b. Include the function written in Exercise 3a 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;
void findMax(int,int,int&); // the function prototype
int main()
{
int num;
int num2;
int num3;
cout << "Enter first number" << endl;
cin >> num;
cout << "Enter second number" << endl;
cin >> num2;
findMax(num, num2, num3);
cout << "The Highest number is" << num3 << endl;
system("PAUSE");
return 0;
}
void findMax(int x, int y, int& max)
{ // start of function body
int maxnum; // variable declaration
if (x >= y) // find the maximum number
maxnum = x;
else
maxnum = y;
max = maxnum;
return; // return statement
}
4. (Program) a. Write a function named change() that has an integer parameter and six integer
reference parameters named hundreds, fifties, twenties, tens, fives, and ones. The
function is to consider the passed integer value as a dollar amount and convert the value into
the fewest number of equivalent bills. Using the reference parameters, the function should
alter the arguments in the calling function.
void change(int parameter, int& hundreds, int& fifties, int& twenties, int& tens, int& fives, int& ones)
{ // start of function body
hundreds = parameter / 100;
parameter %= 100;
fifties = parameter / 50;
parameter %= 50;
twenties = parameter / 20;
parameter %= 20;
tens = parameter / 10;
parameter %= 10;
fives = parameter / 5;
parameter %= 5;
ones = parameter;
return; // return statement
}
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.
#include <iostream>
using namespace std;
void change(int,int&, int&, int&, int&, int&,int&); // the function prototype
int main()
{
int num,num2,num3,num4,num5,num6,num7;
cout << "Enter your number" << endl;
cin >> num;
change(num,num2,num3,num4,num5,num6,num7);
cout << "You get back " << num2<< "hundreds"<< endl;
cout << "You get back " << num3 << "fifties" << endl;
cout << "You get back " << num4 << "twenties" << endl;
cout << "You get back " << num5 << "tens" << endl;
cout << "You get back " << num6 << "fives" << endl;
cout << "You get back " << num7 << "ones" << endl;
system("PAUSE");
return 0;
}
void change(int parameter, int& hundreds, int& fifties, int& twenties, int& tens, int& fives, int& ones)
{ // start of function body
hundreds = parameter / 100;
parameter %= 100;
fifties = parameter / 50;
parameter %= 50;
twenties = parameter / 20;
parameter %= 20;
tens = parameter / 10;
parameter %= 10;
fives = parameter / 5;
parameter %= 5;
ones = parameter;
return; // return statement
}
5. (Program) Write a function named time() that has an integer parameter named seconds
and three integer reference parameters named hours, mins, and secs. The function is to
convert the passed number of seconds into an equivalent number of hours, minutes, and seconds.
Using the reference parameters, the function should alter the arguments in the calling
function.
#include <iostream>
using namespace std;
void time(int,int&, int&, int&); // the function prototype
int main()
{
int num,num2,num3,num4;
cout << "Enter your number" << endl;
cin >> num;
time(num,num2,num3,num4);
cout << "You get back " << num2<< " hours"<< endl;
cout << "You get back " << num3 << " minutes" << endl;
cout << "You get back " << num4 << " seconds" << endl;
system("PAUSE");
return 0;
}
void time(int seconds,int& hours, int& mins, int& secs)
{ // start of function body
hours = seconds / 120;
seconds %= 120;
mins = seconds / 60;
mins %= 60;
secs = seconds;
return; // return statement
}
6. (Program) Write a function named yearCalc() that has an integer parameter representing
the total number of days from the date 1/1/2000 and reference parameters named year, month,
and day. The function is to calculate the current year, month, and day given the number of days
passed to it. Using the reference parameters, the function should alter the arguments in the
calling function. For this problem, assume each year has 365 days, and each month has 30 days.
#include <iostream>
using namespace std;
void yearCalc(int,int&, int&, int&); // the function prototype
int main()
{
int num,num2,num3,num4;
cout << "Enter your number" << endl;
cin >> num;
yearCalc(num,num2,num3,num4);
cout << "You get back " << num2<< " years"<< endl;
cout << "You get back " << num3 << " months" << endl;
cout << "You get back " << num4 << " days" << endl;
system("PAUSE");
return 0;
}
void yearCalc(int total,int& year, int& month, int& days)
{ // start of function body
year = total / 365;
total %= 365;
month= total / 30;
total %= 30;
days = total;
return; // return statement
}
7. (Program) Write a function named liquid() that has an integer number parameter and
reference parameters named gallons, quarts, pints, and cups. The passed integer represents
the total number of cups, and the function is to determine the numbers of gallons, quarts,
pints, and cups in the passed value. Using the reference parameters, the function should alter
the arguments in the calling function. Use these relationships: 2 cups = 1 pint, 4 cups = 1 quart,
and 16 cups = 1 gallon.
#include <iostream>
using namespace std;
void liquid(int,int&, int&, int&,int&); // the function prototype
int main()
{
int num,num2,num3,num4, num5;
cout << "Enter your number" << endl;
cin >> num;
liquid(num,num2,num3,num4,num5);
cout << "You get back " << num2<< " gallons"<< endl;
cout << "You get back " << num3 << " quarts" << endl;
cout << "You get back " << num4 << " pints" << endl;
cout << "You get back " << num5 << " cups" << endl;
system("PAUSE");
return 0;
}
void liquid(int total,int& gallons, int& quarts, int& pints, int& cups)
{ // start of function body
gallons = total / 16;
total %= 16;
quarts= total / 4;
total %= 4;
pints = total / 2;
total %= 2;
cups = total;
return; // return statement
}
8. (Desk check) The following program uses the same argument and parameter names in both
the calling and called functions. Determine whether doing so causes any problem for the
compiler.
#include <iostream>
using namespace std;
void time(int&, int&); // function prototype
int main()
{
int min, hour;
cout << “Enter two numbers :”;
cin >> min >> hour;
time(min, hour);
return 0;
}
void time(int& min, int& hour) // accept two references
{
int sec;
sec = (hour * 60 + min) * 60;
cout << “The total number of seconds is “ << sec << endl;
return;
NO!!, Doing this does not cause any problems the main function and
modular function act independently when it comes to the declaration of variable names.