-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 8.2 (Completed)
286 lines (205 loc) · 5.72 KB
/
Exercise 8.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
1. (Practice) Replace each of the following references to a subscripted variable with a pointer reference:
a. prices[5]
*(prices + 5)
b. grades[2]
*(grades + 2)
c. yield[10]
*(yield + 10)
d. dist[9]
*(dist + 9)
e. mile[0]
*mile
f. temp[20]
*(temp + 20)
g. celsius[16]
*(celsius + 16)
h. num[50]
*(num + 50)
i. time[12]
*(time + 12)
2. (Practice) Replace each of the following pointer references with a subscript reference:
a. *(message+6)
message[6]
b. *amount
amount[0]
c. *(yrs+10)
yrs[10]
d. *(stocks+2)
stocks[2]
e. *(rates+15)
rates[15]
f. *(codes+19)
codes[19]
3. (Practice) a. List three things the declaration statement double prices[5]; causes the
compiler to do.
Creates a pointer to an array location.
Declares that array a double-percision variable.
Every location before the array is also created.
b. If each double-precision number uses 8 bytes of storage, how much storage is set aside for
the prices array?
8 * 6 = 48
48 bytes are set aside for the prices array.
c. Draw a diagram similar to Figure 8.16 for the prices array.
prices
|&prices[0]|
|
|
V
|Address || Address||Address ||Address|| Address || Address|
prices[0] prices[1] prices[2] prices[3] prices[4] prices[5]
or or or or or or
*prices*(prices +1)*(prices +2)*(prices +3)*(prices +4)*(prices +5)
d. Determine the byte offset in relation to the start of the prices array, corresponding to the
offset in the expression *(prices+3).
24 bytes(3 * 8) + 8 = 32 bytes
The offset is 32 bytes
4. (Practice) a. Write a declaration to store the string “This is a sample” in an array named
samtest. Include the declaration in a program that displays the values in samtest by using a
for loop that uses a pointer access to each element in the array.
#include <iostream>
using namespace std;
int main(){
char samtest[17] = { "This is a sample" };
for (int i = 0; i < 17; i++){
std::cout << *(samtest+i) << endl;
}
system("PAUSE");
return 0;
}
b. Modify the program written in Exercise 4a to display only array elements 10 through 15 (the
letters s, a, m, p, l, and e).
#include <iostream>
using namespace std;
int main(){
char samtest[17] = { "This is a sample" };
for (int i = 0; i < 17; i++){
if (i >= 10 && i <= 15){
std::cout << *(samtest + i) << endl;
}
}
system("PAUSE");
return 0;
}
5. (Practice) Write a declaration to store the following values in an array named rates: 12.9,
18.6, 11.4, 13.7, 9.5, 15.2, and 17.6. Include the declaration in a program that displays the values
in the array by using pointer notation.
#include <cmath>
#include <iostream>
using namespace std;
int main(){
double rates[7] = { 12.9,18.6,11.4,13.7,9.5,15.2,17.6 };
for (int i = 0; i < 7; i++){
std::cout << *(rates + i) << endl;
}
system("PAUSE");
return 0;
}
6. (Modify) a. Repeat Exercise 6a in Section 7.1, but use pointer references to access all array
elements.
#include <iostream>
using namespace std;
int main()
{
const int NUMELS = 9;
int i, prices[NUMELS];
for (i = 0; i < NUMELS; i++) // Enter the prices
{
cout << "Enter a price: ";
cin >> *(prices + i);
}
cout << endl;
for (i = 0; i < NUMELS; i++) // Print the prices
cout << "price [" << i << "] is " << *(prices + i) << endl;
system("PAUSE");
return 0;
}
b. Repeat Exercise 6b in Section 7.1, but use pointer references to access all array elements.
#include <iostream>
using namespace std;
int main()
{
const int NUMELS = 9;
int i, prices[NUMELS];
for (i = 0; i < NUMELS; i++) // Enter the grades
{
cout << "Enter a price: ";
cin >> *(prices +i);
}
cout << endl;
for (i = 0; i < 3; i++) { // Print the grades
cout << *(prices + i) << " ";
}
cout << endl;
for (i = 3; i < 6; i++) { // Print the grades
cout << *(prices + i) << " ";
}
cout << endl;
for (i = 6; i < NUMELS; i++) { // Print the grades
cout << *(prices + i) << " ";
}
cout << endl;
system("PAUSE");
return 0;
}
7. (Modify) Repeat Exercise 7 in Section 7.1, but use pointer references to access all array
elements.
#include <iostream>
using namespace std;
int main()
{
const int NUMELS = 8;
int i, grade[NUMELS], total = 0, average;
for (i = 1; i <= NUMELS; i++) // Enter the grades
{
cout << "Enter a grade: ";
cin >> *(grade +i);
}
cout << endl;
for (i = 1; i <= 8; i++) { // Print the grades
cout << *(grade + i) << " " << endl;
total = total + *(grade + i);
average = total / i;
}
cout << "The average is " << endl;
cout << average << endl;
system("PAUSE");
return 0;
}
8. (Modify) As described in Table 8.2, the new operator returns the address of the first new storage
area allocated or returns NULL if there’s insufficient storage. Modify Program 8.8 to check
that a valid address has been returned before attempting to place values in the grades array.
Display an appropriate message if not enough storage is available.
#include <iostream>
#include <new>
using namespace std;
int main()
{
int numgrades, i;
cout << "Enter the number of grades to be processed : ";
cin >> numgrades;
int *grades = new int[numgrades]; // create the array
for (i = 0; i < numgrades; i++)
{
if ( numgrades >= 20){
cout << " Not enough storage or invalid address entered" << endl;
goto end;
}
if (numgrades > 0 && numgrades < 20){
cout << " Enter a grade : ";
cin >> grades[i];
}
}
if (numgrades > 0 && numgrades < 20){
cout << "\nAn array was created for " << numgrades << " integers\n";
cout << "The values stored in the array are: ";
for (i = 0; i < numgrades; i++)
cout << "\n " << grades[i];
cout << endl;
delete[] grades; // return the storage to the heap
// the [] is required for array deletions
}
else{ cout << "Nothing to show" << endl; }
end:
system("PAUSE");
return 0;
}