-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 8.4 (Completed)
345 lines (248 loc) · 8.03 KB
/
Exercise 8.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
1. (Practice) The following declaration was used to create the prices array:
double prices[500];
Write three different headers for a function named sortArray() that accepts the prices
array as a parameter named inArray and returns no value.
void sortArray(double *inArray)
void sortArray(double inArray [500])
void sortArray(double inArray [])
2. (Practice) The following declaration was used to create the keys array:
char keys[256];
Write three different headers for a function named findKey() that accepts the keys array as
a parameter named select and returns no value.
void findKey(char *select)
void findKey(char select[256])
void findKey(char select[])
3. (Practice) The following declaration was used to create the rates array:
double rates[256];
Write three different headers for a function named maximum() that accepts the rates array as
a parameter named speed and returns a double-precision value.
void maximum(double *speed)
void maximum(double speed[])
void maximum(double speed[256])
4. (Modify) Modify the findMax() function to locate the minimum value of the passed array.
Write the function using only pointers.
int findMax(int *vals, int numels) // find the maximum value
{
int i, min = *vals++; // get the first element and increment it
for (i = 1; i < numels; i++, vals++)
{
if (min > *vals)
min = *vals;
}
return min;
}
5. (Debug) In the second version of findMax(), vals was incremented in the altering list of the
for statement. Instead, you do the incrementing in the condition expression of the if statement,
as follows:
int findMax(int *vals, int numels) // incorrect version
{
int i, max = *vals++; // get the first element and increment
for (i = 1; i < numels; i++)
if (max < *vals++)
max = *vals;
return (max);
}
Determine why this version produces an incorrect result.
This version produces an incorrect result because the vals being pointed to is incremented once the max value is reached.
Instead of being incremented for the entire loop.
6. (Program) a. Write a program that has a declaration in main() to store the following numbers
in an array named rates: 6.5, 7.2, 7.5, 8.3, 8.6, 9.4, 9.6, 9.8, and 10.0. Include a function call to
show() that accepts rates in a parameter named rates and then displays the numbers by
using the pointer notation *(rates + i).
#include <iostream>
using namespace std;
void show(double *rates, double numels) // incorrect version
{
for (int i = 0; i < numels; i++){
cout << *(rates + i) << endl;
}
return;
}
int main()
{
double rates[] = { 6.5, 7.2, 7.5, 8.3, 8.6, 9.4, 9.6, 9.8,10.0 };
show(rates, 9);
system("PAUSE");
return 0;
}
b. Modify the show() function written in Exercise 6a to alter the address in rates. Always
use the expression *rates rather than *(rates + i) to retrieve the correct element.
void show(double *rates, double numels) // incorrect version
{
for (int i = 0; i < numels; i++,rates++){
cout << *rates << endl;
}
return;
}
7. (Program) a. Write a program that has a declaration in main() to store the string
Vacation is near in an array named message. Include a function call to display() that
accepts message in an argument named strng and then displays the contents of message by
using the pointer notation *(strng + i).
#include <iostream>
using namespace std;
void display(char *strng, double numels) // incorrect version
{
for (int i = 0; i < numels; i++){
cout << *(strng+ i) << endl;
}
return;
}
int main()
{
char strng[] = { " Vacation is near " };
display(strng, 19);
system("PAUSE");
return 0;
}
b. Modify the display() function written in Exercise 7a to use the expression *strng
rather than *(strng + i) to retrieve the correct element.
#include <iostream>
using namespace std;
void display(char *strng, double numels) // incorrect version
{ for (int i = 0; i < numels; i++, strng++){
cout << *strng << endl;
}
return;
}
int main()
{
char strng[] = { " Vacation is near " };
display(strng, 19);
system("PAUSE");
return 0;
}
8. (Program) Write a program that declares three one-dimensional arrays named price,
quantity, and amount. Each array should be declared in main() and be capable of holding
10 double-precision numbers. The numbers to be stored in price are 10.62, 14.89, 13.21,
16.55, 18.62, 9.47, 6.58, 18.32, 12.15, and 3.98. The numbers to be stored in quantity are 4,
8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, and 4.8. Have your program pass these three arrays to a function
called extend(), which calculates the elements in the amount array as the product of the
equivalent elements in the price and quantity arrays: for example, amount[1] = price[1] * quantity[1].
After extend() has put values in the amount array, display the values in the array from within
main(). Write the extend() function by using pointers.
#include <iostream>
using namespace std;
void extend(double *price, double *quantity){
const int NUM = 10;
double amount[NUM];
for (int i = 0; i < 10; i++){
*(amount+i) = *(price + i) * *(quantity + i);
cout << *(amount +i) << endl;
}
return;
}
int main(){
const int NUM = 10;
double price[] = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
double quantity[] = { 4, 8.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.8 };
extend(price, quantity);
system("PAUSE");
return 0;
}
9. (Program) Write a function named trimfrnt() that deletes all leading blanks from a string.
Write the function using pointers with the return type void.
#include <string>
#include <iostream>
using namespace std;
void trimfrnt(char *str,int size)
{
char *ptrMsg = str;
// Find beginning of text
while (*ptrMsg == ' ')
ptrMsg++;
// Copy text to beginning of array
for (int i = 0; i < size; i++)
*(str+i) = *ptrMsg++;
// Reset pointer to beginning of array
ptrMsg = str;
// Print array
cout << "new msg1: ";
cout << "\"" << ptrMsg << "\"" << endl;
cout << endl;
return;
}
// Driver program to test above function
int main()
{
char msg[] = " GREAT ";
const int size = sizeof(msg) / sizeof(char);
cout << "Orginal msg:\"" << msg << "\"" << endl;
trimfrnt(msg, size);
system("PAUSE");
return 0;
}
10. (Program) Write a function named trimrear() that deletes all trailing blanks from a string.
Write the function using pointers with the return type void.
#include <string>
#include <iostream>
using namespace std;
void trimrear(string str)
{
for (int i = 0; i < sizeof(str); i++){
if (str.back() == ' '){
str.pop_back();
}
}
std::cout << str << '\n';
return ;
}
// Driver program to test above function
int main()
{
std::string str[] = { "hello world. " };
trimrear(*str);
system("PAUSE");
return 0;
}
11. (Program) Write a C++ program that asks for two lowercase characters. Pass the two entered
characters, using pointers, to a function named capit(). The capit() function should capitalize
the two letters and return the capitalized values to the calling function through its
pointer arguments. The calling function should then display all four letters.
#include <iostream>
using namespace std;
void capit(char str, char str2)
{
int i = 0;
char c,e;
c = str;
e = str2;
putchar(toupper(c));
cout << endl;
putchar(toupper(e));
cout << endl;
cout << str << endl;
cout << str2 << endl;
return ;
}
// Driver program to test above function
int main()
{
capit('a', 'b');
system("PAUSE");
return 0;
}
12. (Desk check) a. Determine the output of the following program:
#include <iostream>
using namespace std;
void arr(int [] [3]); // equivalent to void arr(int (*) [3]);
int main()
{
const int ROWS = 2;
const int COLS = 3;
int nums[ROWS][COLS] = { {33,16,29},
{54,67,99}};
arr(nums);
return 0;
}
void arr(int (*val) [3])
{
cout << endl << *(*val);
cout << endl << *(*val + 1);
cout << endl << *(*(val + 1) + 2);
cout << endl << *(*val) + 1;
return;
}
The output is 33,16,99,34.
b. Given the declaration for val in the arr() function, is the notation val[1][2] valid in the
function?
Yes, because arrays and pointers behave the same way and are interchangeable in function notation.