-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 3.3 (Completed)
328 lines (291 loc) · 11.1 KB
/
Exercise 3.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
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
1. (Practice) Write function calls to determine the following:
a. The square root of 6.37
sqrt(6.37)
b. The square root of x - y
sqrt(x-y)
c. The sine of 30 degrees
sin(30 * (3.1416/180))
d. The sine of 60 degrees
sin(60 * (3.1416/180))
e. The absolute value of a2 - b2
abs(a *a - b * b) or abs(pow(a,2) - pow(b,2))
f. The value of e raised to the third power
exp(3)
2. (Practice) For a = 10.6, b = 13.9, and c = -3.42, determine the following values:
a. int (a)
10
b. int (b)
13
c. int (c)
-3
d. int (a + b)
24
e. int (a) + b + c
20.48
f. int (a + b) + c
20.58
g. int (a + b + c)
21
h. double (int (a)) + b
23.9
i. double (int (a + b))
24
j. abs(a) + abs(b)
24.5
k. sqrt(abs(a - b))
1.81659
3. (Practice) Write C++ statements for the following:
*See textbook for statements*
a. area = (c * b * sin(a)) / 2;
b. c = sqrt(pow(a,2)+pow(b,2));
c. p = sqrt(abs(m-n));
d. sum = (a*(pow(r,n)-1))/(r-1);
e. b = pow(sin(x),2)+pow(cos(x),2);
4. (Program) Write, compile, and run a C++ program that calculates and returns the fourth root
of the number 81.0, which is 3. After verifying that your program works correctly, use it to
determine the fourth root of 1,728.896400. Your program should make use of the sqrt()
function or use the fact that the fourth root of a value can be obtained by raising the value to
the 1/4 power.
#include <iostream>
using namespace std;
int main(){
double fourth_root, root;
root = pow(81,(.25));
fourth_root = root;
cout << fourth_root << endl;
return 0;
}
*The forth root of 1,728.896400 is 6.44826
5. (Program) The volume of oil stored in an underground 200-foot deep cylindrical tank is
determined by measuring the distance from the top of the tank to the surface of the oil.
Knowing this distance and the radius of the tank, the volume of oil in the tank can be determined
by using this formula:
volume = π radius^2 (200 - distance)
Using this information, write, compile, and run a C++ program that determines the volume of
oil in a 200-foot tank that has a radius of 10 feet and measures 12 feet from the top of the tank
to the top of the oil. Your program should display the radius, distance from the top of the tank
to the oil, and the calculated volume.
#include <math.h>
#include <iostream>
using namespace std;
int main(){
double volume, radius = 10, distance = 12;
volume = 3.14*pow(radius,2)*( 200 - distance);
cout << volume <<endl;
return 0;
* The volume is 59,032 *
6. (Program) The circumference of an ellipse (review Figure 3.4) is given by this formula:
Circumference = 3.14(sqrt(a+b)^2))
Using this formula, write a C++ program to calculate the circumference of an ellipse with a
minor radius, a, of 2.5 inches and a major radius, b, of 6.4 inches.
#include <math.h>
#include <iostream>
using namespace std;
int main(){
double a = 2.5, b = 6.4,pi =3.14;
double circumference = pi*(pow(sqrt(a + b), 2));
cout << circumference << endl;
return O;
}
"The circumference is 27.946"
7. (Program) Write, compile, and run a C++ program to calculate the distance between two
points with the coordinates (7, 12) and (3, 9). Use the fact that the distance between two points
with the coordinates (x1, y1) and (x2, y2) is given by this formula:
distance = sqrt((x2-x1)^2 + (y2 - y1)^2)
After verifying that your program works correctly by calculating the distance between the
two points manually, use your program to determine the distance between the points
(-12, -15) and (22, 5).
#include <iostream>
#include <math.h> /* pow & sqrt */
using namespace std;
int main()
{
double x1 =-12, x2 =-15, y1 = 22, y2 = 5, distance;
distance = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
cout << distance << endl;
return 0;
}
The distance is 17.2627
8. (Program) If a 20-foot ladder is placed on the side of a building at a 85-degree angle, as
shown in Figure 3.9, the height at which the ladder touches the building can be calculated as
height = 20 × sin 85°. Calculate this height by hand, and then write, compile, and run a C++
program that determines and displays the value of the height. After verifying that your program
works correctly, use it to determine the height of a 25-foot ladder placed at an angle of
85 degrees.
*See textbook for Figure 3.9*
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double height;
height = 25 * (sin((85)*(3.14/180)));
cout << height << endl; // * 24.9032 *
return 0;
}
9. (Program) The maximum height reached by a ball thrown with an initial velocity, v, in
meters/sec, at an angle of u is given by this formula:
height = (.5 × v^2 × sin^2 A) / 9.8
Using this formula, write, compile, and run a C++ program that determines and displays the
maximum height reached when the ball is thrown at 5 mph at an angle of 60 degrees. (Hint:
Make sure to convert the initial velocity into the correct units. There are 1609 meters in a
mile.) Calculate the maximum height manually, and verify the result your program produces.
After verifying the result, use your program to determine the height reached by a ball thrown
at 7 mph at an angle of 45 degrees.
#include <iostream>
#include <math.h>
int main(){
float height, A, v;
A = 45*(3.14/180);
v = (7*1609);
height = (.5 * pow(v,2) * pow(sin(A),2))/9.8;
cout << height<< endl;
return 0;
}
*The max height is 3.2335*
10. (Program) A model of worldwide population growth, in billions of people, since 2000 is given
by this formula:
Population = 7.5 e^0.02[Year - 2010]
Using this formula, write, compile, and run a C++ program to estimate the worldwide population
in the year 2012. Verify the result your program produces by calculating the answer manually,
and then use your program to estimate the world’s population in the year 2020.
#include <math.h>
#include <iostream>
using namespace std;
int main(){
double Population, Year;
Year = 20
Population = 7.5 * pow(e, .02 *(Year- 2010));
cout << "The poulation is" << Population <<"Billion people"<< endl;
return 0;
}
*The Popluation 9.53 Billion People*
11. (Program) The roads of Kansas are laid out in a rectangular grid at exactly one-mile intervals,
as shown in Figure 3.10. Farmer Pete drives his 1939 Ford pickup x miles east and y miles
north to get to farmer Joe’s farm. Both x and y are integer numbers. Using this information,
write, test, and run a C++ program that prompts the user for the values of x and y, and then
uses this formula to find the shortest driving distance across the fields to Joe’s farm:
distance = sqrt(x^2 + y^2);
Round the answer to the nearest integer value before it’s displayed.
#include <math.h>
#include <iostream>
using namespace std;
int main(){
double distance, x, y;
cout << "Enter x" << endl;
cin >> x;
cout << "Enter y" << endl;
cin >> y;
distance = sqrt(pow(x,2) + pow(y,2));
cout << "The distance is" << round(distance) << endl;
return 0;
}
12. (Program) A model to estimate the number of grams of a radioactive isotope left after t years
is given by this formula:
remaining material = (original material) e^-0.00012t
Using this formula, write, compile, and run a C++ program to determine the amount of
radioactive material remaining after 1000 years, assuming an initial amount of 100 grams.
Verify the display your program produces by using a hand calculation. After verifying that
your program is working correctly, use it to determine the amount of radioactive material
remaining after 275 years, assuming an initial amount of 250 grams.
#include <iostream>
#include <math.h>
int main(){
double t, remaining,original,e;
e = 2.71;
t = 275;
original = 250;
remaining = original * pow(e,-.00012*t); // or simply use the exp() function sorry completed these pretty quick
cout << remaining;
return 0;}
"After 250 years there is 241 grams remaining"
13. (Program) The number of years it takes for an isotope of uranium to decay to one-half an
original amount is given by this formula, where λ, the decay constant (which is equal to the
inverse of the mean lifetime), equals 0.00012:
half-life - ln(2) / λ
Using this formula, write, compile, and run a C++ program that calculates and displays the
half-life of this uranium isotope. Verify the result your program produces by using a hand calculation.
After verifying that your program is working correctly, use it to determine the half-life
of a uranium isotope with λ = 0.00026.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double half_life, λ;
λ = .00026;
half_life = log(2) / λ;
return half_life;
return 0;
}
* The half-life is 2665.95 years *
a14. (Program) a. Appendix B lists the integer values corresponding to each letter stored with
the ASCII code. Note that uppercase letters consist of contiguous codes, starting with an
integer value of 65 for the letter A and ending with 90 for the letter Z. Similarly, lowercase
letters begin with the integer value of 97 for the letter a and end with 122 for the letter z.
With this information as background, determine the character value of the expressions
char ('A' + 32) and char ('Z' + 32).
99,104,97,114,32,40,96,65,96,32,43,32,51,50,41,32,97,110,
100,32,99,104,97,114,32,40,96,90,96,32,43,32,51,50,41,46
b. Using Appendix B, determine the integer value of the expression 'a' - 'A'.
96,97,96,32,95,32,96,65,96,46
c. Using the results of Exercises 14a and 14b, determine the character value of the following
expression, where uppercase letter can be any uppercase letter from A to Z:
char (uppercase letter + 'a' - 'A').
99,104,97,114,32,40,117,112,112,101,114,99,97,115,101,32,43,32,96,97,96,32,45,32,96,65,96,41,46
15. (Desk check and program) a. For display purposes, the setprecision() manipulator
allows rounding all outputs to the specified number of decimal places. Doing so can, however,
yield seemingly incorrect results when used in financial programs that require displaying all
monetary values to the nearest penny. For example, examine this program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a, b, c;
a = 1.674;
b = 1.322;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << a << endl;
cout << b << endl;
cout << “----\n”;
c = a + b;
cout << c << endl;
return 0;
}
It produces the following display:
1.67
1.32
----
3.00
Clearly, the sum of the displayed numbers should be 2.99, not 3.00. The problem is that
although the values in a and b have been displayed with two decimal digits, they were added
internally in the program as three-digit numbers. The solution is to round the values in a and
b before they’re added with the statement c = a + b;. Using the int cast, devise a method
to round the values in the variables a and b to the nearest hundredth (penny value) before
they’re added.
---------------------------------------------------------------------
stactic_cast<int>(a+b);
---------------------------------------------------------------------
b. Include the method you devised for Exercise 15a in a working program that produces the
following display:
1.67
1.32
----
2.99
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double a, b, c;
a = 1.67;
b = 1.32;
cout << setiosflags(ios::fixed) << setprecision(2);
cout << a << endl;
cout << b << endl;
cout << "----\n";
static_cast<int>(a + b);
c = a + b;
cout << c << endl;
system("PAUSE");
return 0;
}