-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 5.1 (Completed)
294 lines (266 loc) · 8.55 KB
/
Exercise 5.1 (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
1. (Modify) Rewrite Program 5.1 to print the numbers 2 to 10 in increments of 2. The output
of your program should be the following:
2 4 6 8 10
#include <iostream>
using namespace std;
int main()
{
int count;
count = 2;
while (count <= 10)
{
if ((count % 2) == 0){
cout << count << " ";
}
count++;
}
return 0;
}
2. (Modify) Rewrite Program 5.4 to produce a table starting at a Celsius value of -10 and ending
with a Celsius value of 60, in increments of 10 degrees.
#include <iostream>
#include <iomanip>
#include <math.h> /* pow & sqrt */
using namespace std;
int main()
{
const int MAXCELSIUS = 60;
const int STARTVAL = -10;
const int STEPSIZE = 10;
int celsius;
double fahren;
cout << "DEGREES DEGREES\n"
<< "CELSIUS FAHRENHEIT\n"
<< "------ - ----------\n";
celsius = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
while (celsius <= MAXCELSIUS)
{
fahren = (9.0 / 5.0) * celsius + 32.0;
cout << setw(4) << celsius
<< setw(13) << fahren << endl;
celsius = celsius + STEPSIZE;
}
system("PAUSE");
return 0;
}
3. (Desk check) a. For the following program, determine the total number of items displayed
as well as the first and last numbers printed:
#include <iostream>
using namespace std;
int main()
{
int num = 0;
while (num <= 20)
{
num++;
cout << num << “ “;
}
return 0;
}
21 numbers are displayed, with 1 being the first and 21 being the last.
b. Enter and run the program from Exercise 3a on a computer to verify your answers to the
exercise.
Done!!
c. How would the output be affected if the two statements in the compound statement were
reversed (that is, if the cout statement were placed before the num++ statement)?
21 numbers would still be printed, but the first number would be 0 and the last would be 20.
4. (Program) Write, compile, and run a C++ program that converts gallons to liters. The program
should display gallons from 10 to 20 in 1-gallon increments and the corresponding liter equivalents.
Use the relationship that 1 gallon = 3.785 liters.
#include <iostream>
using namespace std;
int main()
{
const int MAXCELSIUS = 20;
const int STARTVAL = 10;
const int STEPSIZE = 1;
int gallons;
double liters;
cout << "GALLONS LITERS\n"
<< "------ - ----------\n";
gallons = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
while (gallons<= MAXCELSIUS)
{
liters = (gallons * 3.78541178);
cout << setw(4) << gallons
<< setw(13) << liters << endl;
gallons = gallons + STEPSIZE;
}
system("PAUSE");
return 0;
5. (Program) Write, compile, and run a C++ program that converts feet to meters. The program
should display feet from 3 to 30 in 3-foot increments and the corresponding meter equivalents.
Use the relationship that 3.28 feet = 1 meter.
#include <iostream>
using namespace std;
int main()
{
const int MAXCELSIUS = 30;
const int STARTVAL = 3;
const int STEPSIZE = 3;
int FEET;
double METERS;
cout << "FEET METERS\n"
<< "------ - ----------\n";
FEET = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
while (FEET<= MAXCELSIUS)
{
METERS = (FEET * .3048);
cout << setw(4) << FEET
<< setw(13) << METERS << endl;
FEET = FEET + STEPSIZE;
}
system("PAUSE");
return 0;
}
6. (Program) A machine purchased for $28,000 is depreciated at a rate of $4000 a year for 7 years.
Write, compile, and run a C++ program that computes and displays a depreciation table for
7 years. The table should have this form:
END-OF-YEAR ACCUMULATED
YEAR DEPRECIATION VALUE DEPRECIATION
---- ------------ ----------- ------------
1 4000 24000 4000
2 4000 20000 8000
3 4000 16000 12000
4 4000 12000 16000
5 4000 8000 20000
6 4000 4000 24000
7 4000 0 28000
#include <iostream>
using namespace std;
int main()
{
const int MAX = 7;
const int STARTVAL = 1;
const int STEPSIZE = 1;
int year, End_year_value;
double depreciation, Accumulated_Depreciation;
cout << "YEAR DEPRESCIATION END-OF-YEAR VALUE ACCUMULATED DEPRECIATION \n"
<< "------ - ------------- - ------------- - -------------- \n";
year = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
while (year<= MAX )
{
int End_year_value = 28000;
depreciation = 4000;
Accumulated_Depreciation = year * depreciation;
End_year_value = End_year_value - Accumulated_Depreciation;
cout << setw(4) << year
<< setw(15) << depreciation
<< setw(15) << End_year_value
<< setw(15) << Accumulated_Depreciation << endl;
year = year + STEPSIZE;
}
system("PAUSE");
return 0;
}
7. (Program) An automobile travels at an average speed of 55 mph for 4 hours. Write, compile,
and run a C++ program that displays the distance, in miles, the car has traveled after 0.5, 1.0,
1.5, and so on hours until the end of the trip.
#include <iostream>
using namespace std;
int main()
{
double MAX = 4;
double STARTVAL = .5;
double STEPSIZE = .5;
double hours,distance;
double speed = 55;
cout << "HOURS DISTANCE TRAVELED \n"
<< "------ - -------------\n";
hours = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
while (hours <= MAX )
{
distance = speed * hours;
cout << setw(4) << hours
<< setw(15) << distance
<< endl;
hours = hours + STEPSIZE;
}
system("PAUSE");
return 0;
}
8. (Program) a. The following is an approximate conversion formula for converting Fahrenheit
to Celsius temperatures:
Celsius = (Fahrenheit - 30) / 2
Using this formula, and starting with a Fahrenheit temperature of 0 degrees, write a C++ program
that determines when the approximate equivalent Celsius temperature differs from the
exact equivalent value by more than 4 degrees. (Hint: Use a while loop that terminates when
the difference between approximate and exact Celsius equivalents exceeds 4 degrees.)
#include <iostream>
using namespace std;
int main()
{
const int MAX = 100;
const int STARTVAL = 0;
const int STEPSIZE = 1;
double Fahrenheit = 0;
double Celsius, realCelsius;
cout << "REALCELSIUS CELSIUS \n"
<< " ----------- - ------------- \n";
Fahrenheit = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
while (Fahrenheit <= MAX && (Celsius - realCelsius) < 4 )
{
Celsius = (Fahrenheit - 30) / 2;
realCelsius = ((Fahrenheit - 32) * (.55555555555));
cout << setw(4) << realCelsius
<< setw(15) << Celsius
<< endl;
Fahrenheit = Fahrenheit + STEPSIZE;
}
system("PAUSE");
return 0;
}
b. Using the approximate Celsius conversion formula given in Exercise 8a, write a C++ program
that produces a table of Fahrenheit temperatures, exact Celsius equivalent temperatures,
approximate Celsius equivalent temperatures, and the difference between the exact
and approximate equivalent Celsius values. The table should begin at 0 degrees Fahrenheit,
use 2-degree Fahrenheit increments, and terminate when the difference between exact and
approximate values is more than 4 degrees.
#include <iostream>
using namespace std;
int main()
{
const int MAX = 100;
const int STARTVAL = 0;
const int STEPSIZE = 2;
double Fahrenheit = 0;
double Celsius, realCelsius, difference;
cout << "REALCELSIUS CELSIUS FAHRENHEIT DIFFERENCE \n"
<< " ----------- - ------------- - ---------------- - ---------------\n";
Fahrenheit = STARTVAL;
// set output formats for floating-point numbers only
cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed)
<< setprecision(2);
difference = Celsius - realCelsius;
while (Fahrenheit <= MAX && difference < 4)
{
Celsius = (Fahrenheit - 30) / 2;
realCelsius = ((Fahrenheit - 32) * (.55555555555));
cout << setw(4) << realCelsius
<< setw(15) << Celsius
<< setw(15) << Fahrenheit
<< setw(15) << difference
<< endl;
Fahrenheit = Fahrenheit + STEPSIZE;
}
system("PAUSE");
return 0;
}