-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 3.1 (Completed)
226 lines (201 loc) · 6.64 KB
/
Exercise 3.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
1. (Practice) Write an assignment statement to calculate the circumference of a circle having a
radius of 3.3 inches. The formula for determining the circumference, c, of a circle is c = 2πr,
where r is the radius and π equals 3.1416.
c = 2 * 3.1416 * 3.3;
2. (Practice) Write an assignment statement to calculate the area of a circle. The formula for
determining the area, a, of a circle is a = πr2, where r is the radius and π = 3.1416.
a = 3.1416 * r * r;
3. (Practice) Write an assignment statement to convert temperature in degrees Fahrenheit to
degrees Celsius. The formula for this conversion is Celsius = 5.0 / 9.0 (Fahrenheit - 32).
celsius = (5/9) * (fahrenheit - 32);
4. (Practice) Write an assignment statement to calculate the round-trip distance, d, in feet, of a
trip that’s s miles long one way.
d = s * 5280
5. (Practice) Write an assignment statement to calculate the elapsed time, in minutes, it takes
to make a trip. The formula for computing elapsed time is elapsed time = total distance / average
speed. Assume the distance is in miles and the average speed is in miles per hour (mph).
elapsed time = total distance / (average speed * 60)
6. (Practice) Write an assignment statement to calculate the value, v, of the nth term in an arithmetic
sequence. The formula for calculating this value is as follows:
v = a + (n - 1)d
a is the first number in the sequence.
d is the difference between any two numbers in the sequence.
v = a + (n-1) *d
7. (Practice) Write an assignment statement to determine the maximum bending moment, M,
of a beam, given this formula:
M = X W (L - X) / L
X is the distance from the end of the beam that a weight, W, is placed.
L is the length of the beam.
M = X * W *(L - X) / L
8. (Debug) Determine and correct the errors in the following programs.
a. #include <iostream>
using namespace std;
int main()
{
width = 15
area = length * width;
cout << “The area is “ << area
}
---------------Corrected below-------------------------------
#include <iostream>
using namespace std;
int main()
{ int length = 20
int area;
int width = 15;
area = length * width;
cout << “The area is “ << area << endl;
}
b. #include <iostream>
using namespace std;
int main()
{
int length, width, area;
area = length * width;
length = 20;
width = 15;
cout << “The area is “ << area;
return 0;
------------Corrected below----------------
#include <iostream>
using namespace std;
int main()
{
int length, width, area;
length = 20;
width = 15;
area = length * width;
cout << “The area is “ << area;
return 0;
c. #include <iostream>
int main()
{
int length = 20; width = 15, area;
length * width = area;
cout << “The area is “ , area;
return 0;
}
---------Corrected Code-----------------
#include <iostream>
int main()
{
int length = 20, width = 15, area;
length * width = area;
cout << "The area is" << area;
return 0;
}
9. (Debug) By mistake, a student reordered the statements in Program 3.3 as follows:
#include <iostream>
using namespace std;
int main()
{
int sum;
sum = 0;
sum = sum + 96;
sum = sum + 70;
sum = sum + 85;
sum = sum + 60;
cout << “The value of sum is initially set to “
<< sum << endl;
cout << “ sum is now “ << sum << endl;
cout << “ sum is now “ << sum << endl;
cout << “ sum is now “ << sum << endl;
cout << “ The final sum is “ << sum << endl;
return 0;
}
Determine the output this program produces.
The value of sum is initally set to 311
sum is now 311
sum is now 311
sum is now 311
The final sum is 311
10. (Practice) Using Program 3.1, complete the following chart by determining the area of a rectangle
having these lengths and widths:
Length (in.) Width (in.) Area
1.62 6.23 10.0926
2.86 7.52 21.5072
4.26 8.95 38.127
8.52 10.86 92.527212.29 15.35 188.651
11. (Program) The area of an ellipse (see Figure 3.4) is given by this formula:
Area = π a b
Using this formula, write a C++ program to calculate the area of an ellipse having a minor axis,
a, of 2.5 inches and a major axis, b, of 6.4 inches.
Use Figure 3.4 as a reference
#include <iostream>
using namespace std;
int main(){
double Area,a,b;
a= 2.5;
b = 6.4;
Area = 3.14 * a * b;
cout << "The area of an ellipse with a minor axis is" << Area <<endl;
return 0;
}
12. (Program) a. Write a C++ program to calculate the dollar amount contained in a piggybank.
The bank currently contains 12 half-dollars, 20 quarters, 32 dimes, 45 nickels, and 27 pennies.
#include <iostream>
using namespace std;
int main(){
double half, quart, dim, nick, pen,total;
half =.50;
quart = .25;
dim = .10;
nick = .05;
pen = .01;
total = (12*half) + (20*quart) +(32*dim) + (45*nick) + (27*pen);
return 0;
}
b. Run the program written for Exercise 12a on a computer.
Done!!
13. (Program) a. Write a C++ program to calculate the distance, in feet, of a trip that’s 2.36 miles
long. One mile is equal to 5280 feet.
#include <iostream>
using namespace std;
int main(){
double distance,miles, feet;
miles = 2.36;
feet = 5280;
distance = miles * feet;
cout << distance <<endl;
return 0;
}
b. Run the program written for Exercise 13a on a computer.
Done !!
14. (Program) a. Write a C++ program to calculate the elapsed time it takes to make a 183.67-mile
trip. The equation for computing elapsed time is elapsed time = total distance / average speed.
Assume that the average speed during the trip is 58 miles per hour.
#include <iostream>
using namespace std;
int main(){
double elapsed_time, distance = 183.67 , average_speed = 58;
elasped_time = distance / average_speed;
cout << elasped_time<<endl;
return 0;
}
b. Run the program written for Exercise 14a on a computer.
Done
15. (Program) a. Write a C++ program to calculate the sum of the numbers from 1 to 100. The
formula for calculating this sum is sum = (n / 2) × (2 × a + (n - 1) × d), where n = number of
terms to be added, a = the first number, and d = the difference between each number and the
next number (d = 1).
#include <iostream>
using namespace std;
int main()
{
int sum, d, a;
a = 1;
d = 1;
for (int n = 1; n <= 100; n++)
{
sum = (n / 2) * (2 * a + (n - 1) * d);
}
cout << sum << endl;
system("PAUSE");
return 0;
}
b. Run the program written for Exercise 15a on a computer.
Done
16. (For thought) Determine why the expression a-b=25 is invalid but the expression
a - (b=25) is valid.
a-b = 25 is invalid because the variables are not properly defined;
and the expression a - (b=25) is valid because variable a is being subtracted from 25 and thus it is properly defined.