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
|
CST116
Module 3: Lab 3
5a
6.4 Exercises
p 126
5 pts #1
Submit: output
OUTPUT:
cout << a++ << endl; 0
cout << ++a << endl; 2
cout << a + 1 << endl; 3
cout << a << endl; 2
cout << --a << endl; 1
7.1 Exercises
p 148
5 pts #1�-6
Submit: corrected conditions
int_exp1 = 0, int_exp2 = 1
char_exp = 'B'
1. int_exp1 => int_exp2 int_exp1 <= int_exp2 (Flip sign)
2. int_exp1 = int_exp2 int_exp1 == int_exp2 (Two =)
3. int_exp1 =! int_exp2 int_exp1 != int_exp2 (= comes second)
4. char_exp == "A" char_exp == 'A' (Single quotes)
5. int_exp1 > char_exp Correct
6. int_exp1 < 2 && > -10 int_exp1 < 2 && int_exp 1 > -10
7.2 Learn by Doing Exercises
p 155
10 pts #1
Submit: code & run
Total: 15 pts
5b
7.4 Learn by Doing Exercises
p 161
10 pts #1
Submit: code & run
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int select;
cout << "\n\t\tStudent Grade Program ";
cout << "\n\t\t - Main Menu - ";
cout << "\n\t\t1. Enter name ";
cout << "\n\t\t2. Enter test scores ";
cout << "\n\t\t3. Display test scores ";
cout << "\n\t\t9. Exit ";
cout << "\n\n\tPlease Enter your choice from the list above: ";
cin >> select;
switch (select)
{
case 1:
cout << "\n\tYou chose to enter name " << endl;
break;
case 2:
cout << "\n\tYou chose to enter test score " << endl;
break;
case 3:
cout << "\n\tYou chose to display test scores " << endl;
break;
case 9:
cout << "\n\tYou chose to exit " << endl;
break;
default:
cout << "\n\tInvalid option entered." << endl;
}
return 0;
}
RUN:
Student Grade Program
- Main Menu -
1. Enter name
2. Enter test scores
3. Display test scores
9. Exit
Please Enter your choice from the list above: 5
Invalid option entered.
5c
7.10 Programming Exercises
p 168
10 pts #2
Submit: code & run
PSEUDOCODE:
Display "Enter loan interest rate "
Read rate
Display "Enter loan amount "
Read loan
If rate amount is between 1 and 18% && loan amount is between 100 - 1000
If loan is between 100 - 200
loan_fee = 20
If loan > 500
loan_fee = 25
If else
Display error message and end program
Calculate the amount of interest paid on loan
Display requested amount of loan
Display interest rate
Display sum of interest and fees
CODE:
#include <iostream>
using namespace std;
int main()
{
float rate = 0;
int amount = 0;
int loan_fee = 0;
float fees = 0;
float interest_rate = 0;
cout << "Enter interest rate percentage: ";
cin >> rate;
cout << "Enter loan amount: ";
cin >> amount;
if (rate >= 1 && rate <= 18 && amount >= 100 && amount <= 1000)
{
if (amount >= 100 && amount <= 500)
loan_fee = 20;
else if (amount > 500)
loan_fee = 25;
interest_rate = rate / 100;
fees = amount * interest_rate + loan_fee;
cout << "Requested amount of loan: " << amount << "\nInterest rate: " << rate << "\nSum of interest and fees: " << fees << endl;
}
else
{
cout << "Invalid amount entered" << endl;
}
return 0;
}
RUN:
Enter interest rate percentage 10
Enter loan amount 500
Requested amount of loan: 500
Interest rate: 10
Sum of interest and fees: 70
6a
6.5 Exercises
pp 126-127
3 pts #1-5
Submit: converted statements
int a(10), b(20), c(30);
1. a = 25 + a; a += 25;
2. b = b * a * 2; b *= a * 2;
3. b = 1 + b; b += 1; OR b++;
4. c = c % 5; c %= 5;
5. b = b / a; b /+ a;
8.2 Learn by Doing Exercises
pp 177
10 pts #1
Submit: code & run
CODE:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int value = 0;
cout << "Enter an integer between 1 and 50: ";
cin >> value;
cout << "Even numbers from " << value << " to 0: \n";
for (int i = value; i >= 0; i--)
{
if (i % 2 == 0)
cout << i << " ";
}
return 0;
}
RUN:
Enter an integer between 1 and 50: 25
Even numbers from 25 to 0:
24 22 20 18 16 14 12 10 8 6 4 2 0
8.3 Learn by Doing Exercises
pp 179
10 pts #1
Submit: code & run
CODE:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int value = 0;
do
{
cout << "\nPlease enter an integer between 1 and 50: ";
cin >> value;
} while (value > 50 || value < 1);
cout << "Even numbers from " << value << " to 0: \n";
for (int i = value; i >= 0; i--)
{
if (i % 2 == 0)
cout << i << " ";
}
return 0;
}
RUN:
Please enter an integer between 1 and 50: 85
Please enter an integer between 1 and 50: 25
Even numbers from 25 to 0:
24 22 20 18 16 14 12 10 8 6 4 2 0
6b
8.4 Learn by Doing Exercises
p 184
10 pts #2
Submit: code & run
CODE:
#include <iostream>
using namespace std;
int main()
{
int length = 0;
cout << "Enter triangle lenth: ";
cin >> length;
for (int row = length; row >= 1; row--)
{
for (int column = row; column > 0; column--)
{
cout << "* ";
}
cout << endl;
}
}
RUN:
Enter triangle lenth: 6
* * * * * *
* * * * *
* * * *
* * *
* *
*
6c
8.10 Programming Exercises
p 192
10 pts #3
Submit: code & run
#include <iostream>
using namespace std;
int main()
{
int term1 = 0;
int term2 = 1;
int nextTerm = 0;
int end;
cout << "Enter an integer: ";
cin >> end;
cout << "Fibonacci sequence: " << term1 << ", " << term2 << ", ";
nextTerm = term1 + term2;
while(nextTerm <= end) {
cout << nextTerm << ", ";
term1 = term2;
term2 = nextTerm;
nextTerm = term1 + term2;
}
return 0;
}
RUN:
Enter an integer: 21
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21,
|