-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 8.3 (Completed)
205 lines (151 loc) · 4.76 KB
/
Exercise 8.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
1. (Modify) Replace the while statement in Program 8.10 with a for statement.
#include <iostream>
#include <new>
using namespace std;
int main()
{
const int VALUES = 5;
int nums[VALUES] = { 16, 54, 7, 43, -5 };
int total = 0, *nPt;
// store address of nums[0] in nPt
for (nPt = nums; nPt < nums + VALUES; nPt++){
total += *nPt;
cout << "The total of the array elements is " << total << endl;
}
system("PAUSE");
return 0;
}
2. (Program) a. Write a program that stores the following numbers in an array named rates:
6.25, 6.50, 6.8, 7.2, 7.35, 7.5, 7.65, 7.8, 8.2, 8.4, 8.6, 8.8, and 9.0. Display the values in the array
by changing the address in a pointer called dispPt. Use a for statement in your program.
#include <iostream>
#include <new>
using namespace std;
int main()
{
const int VALUES = 13;
double rates[VALUES] = { 6.25, 6.50, 6.8, 7.2, 7.35, 7.5, 7.65, 7.8, 8.2, 8.4, 8.6, 8.8, 9.0 };
// store address of nums[0] in nPt
for (int i = 0; i < 13; i++){
cout << *(rates + i) << endl;
}
system("PAUSE");
return 0;
}
b. Modify the program written in Exercise 2a to use a while statement.
#include <iostream>
#include <new>
using namespace std;
int main()
{
const int VALUES = 13;
double rates[VALUES] = { 6.25, 6.50, 6.8, 7.2, 7.35, 7.5, 7.65, 7.8, 8.2, 8.4, 8.6, 8.8, 9.0 };
// store address of nums[0] in nPt
int i = 0;
while( i < 13){
cout << *(rates + i) << endl;
i++;
}
system("PAUSE");
return 0;
}
3. (Program) a. Write a program that stores the string Hooray for All of Us in an array
named strng. Use the declaration strng[] =“Hooray for All of Us”;, which ensures
that the end-of-string escape sequence \0 is included in the array. Display the characters in
the array by changing the address in a pointer called messPt. Use a for statement in your
program.
#include <iostream>
using namespace std;
int main()
{
char strng[] = { "Hooray for All of Us\0" };
char *messPt = strng;
for (int i = 0; i < 22; i++){
cout << *(messPt + i) << endl;
}
system("PAUSE");
return 0;
}
b. Modify the program written in Exercise 3a to use the while statement while
(*messPt++ != '\0').
#include <iostream>
using namespace std;
int main()
{
char strng[] = { " Hooray for All of\0 Us" };
char *messPt = strng;
while(*messPt++ != '\0'){
cout << *messPt << endl;
}
system("PAUSE");
return 0;
}
c. Modify the program written in Exercise 3a to start the display with the word All.
#include <iostream>
using namespace std;
int main()
{
char strng[] = { " Hooray for All of\0 Us" };
char *messPt = strng;
for (int i = 11; i < 22; i++){
cout << *(messPt + i) << endl;
}
system("PAUSE");
return 0;
}
4. (Program) Write a program that stores the following numbers in the array named miles: 15,
22, 16, 18, 27, 23, and 20. Have your program copy the data stored in miles to another array
named dist, and then display the values in the dist array. Your program should use pointer
notation when copying and displaying array elements.
#include <iostream>
using namespace std;
int main()
{
int miles[] = { 15,22, 16, 18, 27, 23, 20 };
int *dist = miles;
for (int i = 0; i < 7; i++){
cout << *dist++ << endl;
}
system("PAUSE");
return 0;
}
5. (Program) Write a C++ program that stores the following letters in the array named message:
This is a test. Have your program copy the data stored in message to another array
named mess2 and then display the letters in the mess2 array.
#include <iostream>
using namespace std;
int main()
{
char message[] = { "This is a test" };
char *mess2 = message;
for (int i = 0; i < 15; i++){
cout << *mess2++ << endl;
}
system("PAUSE");
return 0;
}
6. (Program) Write a program that declares three one-dimensional arrays named miles,
gallons, and mpg. Each array should be capable of holding 10 elements. In the miles array,
store the numbers 240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4, and 192.3. In the
gallons array, store the numbers 10.3, 15.6, 8.7, 14, 16.3, 15.7, 14.9, 10.7, 8.3, and 8.4. Each
element of the mpg array should be calculated as the corresponding element of the miles array
divided by the equivalent element of the gallons array: for example, mpg[0] =miles[0]
/ gallons[0]. Use pointers when calculating and displaying the elements of the mpg array.
#include <iostream>
using namespace std;
int main()
{
double miles[] = { 240.5, 300.0, 189.6, 310.6, 280.7, 216.9, 199.4, 160.3, 177.4,192.3 };
double gallons[] = { 10.3, 15.6, 8.7, 14, 16.3, 15.7, 14.9, 10.7, 8.3, 8.4 };
double mpg[10];
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
*(mpg +i) = *(miles + i)/ *(gallons + i);
}
}
for (int k = 0; k < 10; k++){
cout << *(mpg+ k) << endl;
}
system("PAUSE");
return 0;
}