forked from PatrickHentz/A-First-Book-of-C-Exercises-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise 7.2 (Completed)
318 lines (231 loc) · 8.38 KB
/
Exercise 7.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
1. (Practice) Write array declarations, including initializers, for the following:
a. A list of 10 integer grades: 89, 75, 82, 93, 78, 95, 81, 88, 77, and 82
const int SIZE = 10;
int grades[SIZE] = {89, 75, 82, 93, 78, 95, 81, 88, 77, 82};
b. A list of five double-precision amounts: 10.62, 13.98, 18.45, 12.68, and 14.76
const int SIZE = 5;
double amounts[SIZE] = {10.62, 13.98, 18.45, 12.68, 14.76};
c. A list of 100 double-precision interest rates, with the first six rates being 6.29, 6.95, 7.25,
7.35, 7.40, and 7.42
const int SIZE = 100;
double rates[SIZE] = {6.29, 6.95, 7.25, 7.35, 7.40, 7.42};
d. A list of 64 double-precision temperatures, with the first 10 temperatures being 78.2, 69.6,
68.5, 83.9, 55.4, 67.0, 49.8, 58.3, 62.5, and 71.6
const int SIZE = 64;
double temps[SIZE] = {78.2, 69.6, 68.5, 83.9, 55.4, 67.0, 49.8, 58.3, 62.5, 71.6};
e. A list of 15 character codes, with the first seven codes being f, j, m, q, t, w, and z
const int SIZE = 15;
char codes[SIZE] = { 'f', 'j', 'm', 'q', 't', 'w', 'z' };
2. (Practice) Write an array declaration statement that stores the following values in an array
named prices: 16.24, 18.98, 23.75, 16.29, 19.54, 14.22, 11.13, and 15.39. Include these statements
in a program that displays the values in the array.
#include <iostream>
using namespace std;
int main()
{
double price[] = { 16.24, 18.98, 23.75,16.29,19.54,14.22,11.13,15.39};
for (int i = 0; i < 8; i++){
cout << price[i] << endl;
}
system("PAUSE");
return 0;
}
3. (Program) Write, compile, and run a C++ program that uses an array declaration statement to
initialize the following numbers in an array named slopes: 17.24, 25.63, 5.94, 33.92, 3.71,
32.84, 35.93, 18.24, and 6.92. Your program should locate and display the maximum and minimum
values in the array.
#include <iostream>
using namespace std;
int main()
{
double slopes[] = { 17.24,25.63,5.94,33.92,3.71,32.84,35.93,18.24,6.92};
double max = slopes[0];
for (int i = 0; i < 8; i++){
if (slopes[i] > max){
max = slopes[i];
}
}
double min = slopes[0];
for (int i = 0; i < 8; i++){
if (slopes[i] < min){
min = slopes[i];
}
}
cout << max << endl;
cout << min << endl;
system("PAUSE");
return 0;
}
4. (Program) Write, compile, and run a C++ program that stores the following numbers in an
array named prices: 9.92, 6.32, 12.63, 5.95, and 10.29. Your program should also create two
arrays named units and amounts, each capable of storing five double-precision numbers.
Using a for loop and a cin statement, have your program accept five user-input numbers in
the units array when the program is run. Your program should store the product of the corresponding
values in the prices and units array in the amounts array. For example, use
amounts[1] = prices[1] * units[1]. Your program should then display the following
output (fill in the chart):
Price Units Amount
9.92 1 9.92
6.32 2 12.64
12.63 3 37.89
5.95 5 29.75
10.29 6 61.74
Total: 151.94
#include <iostream>
using namespace std;
int main()
{
const int NUM = 5;
double prices[] = { 9.92, 6.32, 12.63, 5.95, 10.29 };
double units[5];
double amounts[5];
double total = 0;
for (int i = 0; i < NUM; i++){
cout << "Enter number of units" << endl;
cin >> units[i];
amounts[i] = prices[i] * units[i];
total = total + amounts[i];
}
cout << "Price Units Amount" << endl;
for (int i = 0; i < NUM; i++){
cout << prices[i] <<setw(15)<< units[i] <<setw(15) << amounts[i] << endl;
}
cout << "Total:" << total << endl;
system("PAUSE");
return 0;
}
5. (Program) Define an array with a maximum of 20 integer values and fill the array with
numbers of your own choosing as intializers. Then write, compile, and run a C++ program
that reads the numbers in the array and places all zero and positive numbers in an array
named positive and all negative numbers in an array named negative. Finally, have your
program display the values in both the positive and negative arrays.
#include <iostream>
using namespace std;
int main()
{
const int NUM = 20;
int nums[] = { 9, -3, 20, 30, 40, 50, 60, 70, 80, 90, 100, -100, 0, 3, 2, 1, -10, -5, -1, 4 };
int positive[NUM];
int negative[NUM];
for (int i = 0; i < 20; i++){
if (nums[i] >= 0){
positive[i] = nums[i];
cout << "Positive values: " << positive[i] << endl;
}
else if (nums[i] < 0){
negative[i] = nums[i];
cout << "Negative values: " << negative[i] << endl;
}
}
system("PAUSE");
return 0;
}
6. (Practice) The string of characters “Good Morning” is to be stored in a character array
named goodstr1. Write the declaration for this array in three different ways.
char goodstr1[] = “Good Morning”;
char goodstr1[] = {'G', 'o', 'o', 'd', ' ', 'M', 'o', 'r', 'n', 'i', 'n','g'};
char goodstr1[12] = {'G', 'o', 'o', 'd', ' ', 'M', 'o', 'r', 'n', 'i', 'n','g'};
7. (Practice) a. Write declaration statements to store the string of characters “Input
the Following Data” in a character array named message1, the string “------------” in
an array named message2, the string “Enter the Date:” in an array named message3, and
the string “Enter the Account Number:” in an array named message4.
char message1[] = “Input the Following Data”;
char message2[] = “------------------------”;
char message3[] = “Enter the Date: “;
char message4[] = “Enter the Account Number: “;
b. Include the array declarations written in Exercise 7a in a program that uses a cout statement
to display the messages. For example, the statement cout << message1; causes the
string stored in the message1 array to be displayed. Your program requires four of these
statements to display the four messages. Using a cout statement to display a string requires
placing the end-of-string marker \0 in the character array used to store the string.
#include <iostream>
using namespace std;
int main()
{
char message1[] = "Input the Following Data";
char message2[] = "------------------------";
char message3[] = "Enter the Date : ";
char message4[] = "Enter the Account Number : ";
cout << message1 << endl;
cout << message2 << endl;
cout << message3 << endl;
cout << message4 << endl;
system("PAUSE");
return 0;
}
8. (Program) a. Write a declaration to store the string “This is a test” in an array named
strtest. Include the declaration in a program to display the message using the following loop:
for (i = 0; i < NUMDISPLAY; i++)
cout << strtest[i];
NUMDISPLAY is a named constant for the number 14.
-------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
const int NUMDISPLAY = 14;
char strtest[] = "This is a test";
for (int i = 0; i < NUMDISPLAY; i++){
cout << strtest[i] << endl;
}
system("PAUSE");
return 0;
}
b. Modify the for statement in Exercise 8a to display only the array characters t, e, s, and t.
#include <iostream>
using namespace std;
int main()
{
const int NUMDISPLAY = 14;
char strtest[] = "This is a test";
for (int i = 0; i < NUMDISPLAY; i++){
switch (strtest[i]){
case 't':
cout << strtest[i] << endl;
break;
case 'e':
cout << strtest[i] << endl;
break;
case 's':
cout << strtest[i] << endl;
break;
case 'T':
cout << strtest[i] << endl;
break;
}
}
system("PAUSE");
return 0;
}
c. Include the array declaration written in Exercise 8a in a program that uses a cout statement
to display characters in the array. For example, the statement cout << strtest; causes
the string stored in the strtest array to be displayed. Using this statement requires having
the end-of-string marker, \0, as the last character in the array.
#include <iostream>
using namespace std;
int main()
{
const int NUMDISPLAY = 14;
char strtest[] = "This is a test\0";
for (int i = 0; i < NUMDISPLAY; i++){
cout << strtest[i] << endl;
}
system("PAUSE");
return 0;
}
d. Repeat Exercise 8a, using a while loop. (Hint: Stop the loop when the \0 escape sequence
is detected. The expression while (strtest[i] != '\0') can be used.)
#include <iostream>
using namespace std;
int main()
{
const int NUMDISPLAY = 14;
char strtest[] = "This is a\0 test";
int i = 0;
while (strtest[i] != '\0' && i < NUMDISPLAY){
cout << strtest[i] << endl;
i++;
}
system("PAUSE");
return 0;
}