-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 4.2 (Completed)
431 lines (405 loc) · 15.8 KB
/
Exercise 4.2 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
1. (Practice) Write suitable if statements for the following conditions:
a. If an angle is equal to 90 degrees, print the message “The angle is a right angle.”; else, print
the message “The angle is not a right angle.”
if (angle == 90)
cout << “The angle is a right angle”;
else
cout << “The angle is not a right angle”;
b. If the temperature is above 100 degrees, display the message “above the boiling point of
water”; else, display the message “below the boiling point of water.”
if (temp > 100)
cout << “above the boiling point of water”;
else
cout << “below the boiling point of water”;
c. If the number is positive, add the number to the variable positivesum; else, add the number
to the variable negativesum.
if (number > 0)
number += positivesum;
else
number += negativesum;
d. If the slope is less than 0.5, set the variable flag to 0; else, set flag to 1.
if (slope < 0.5 )
flag = 0;
else
flag = 1;
e. If the difference between slope1 and slope2 is less than 0.001, set the variable approx to
0; else, calculate approx as the quantity (slope1 - slope2) / 2.0.
if (slope1 - slope2 < .001 )
approx = 0;
else
approx = (slope1 - slope2) / 2.0;
f. If the frequency is above 60, display the message “The frequency is too high.”
if (frequency > 60) {
cout << "The frequency is too high." << endl;
}
g. If the difference between temp1 and temp2 exceeds 2.3, calculate the variable error as
(temp1 - temp2) * factor.
if(temp1 - temp2 > 2.3){
error = (temp1 - temp2) * factor;}
h. If x is greater than y and z is less than 20, request that the user input a value for the
variable p.
if (x > y && x < 20){
cout << "request input for varable p:";
cin >> p;
}
i. If distance is greater than 20 and less than 35, request that the user input a value for the
variable time.
if (distance > 20 && distance < 35){
cout << "request input for value of variable time:" << endl;
cin >> time;
}
2. (Practice) Write if statements corresponding to the conditions shown in the following
flowcharts:
*See textbook for diagram*
a. if (ace < 25)
sum = sum + a;
else
count = count + 1;
b. if(c == 15)
{
credit = 10;
limit = 1200;
}
else
{
credit = 8;
limit = 800;
}
c. if (id > 22)
factor = 0.7;
d. if (count == 10)
{
average = sum / count;
cout << average;
}
3. (Practice) Write a C++ program that asks the user to input two numbers. If the first number
entered is greater than the second number, the program should print the message “The first
number is greater.”; else, it should print the message “The first number is smaller.” Test your
program by entering the numbers 5 and 8 and then using the numbers 11 and 2. What do you
think your program will display if the two numbers entered are equal? Test this case.
#include <iostream>
using namespace std;
int main(){
int number1, number2;
cout << "The first number is" << endl;
cin >> number1;
cout << "The second number is" << endl;
cin >> number2;
if (number1 > number2){
cout << "The first number is greater" << endl;
}
else{ cout << "The first number is smaller" << endl; }
return 0;
}
If the two numbers are equal, the else statement is executed, which produces an incorrect
display.
4. (Program) a. In a pass/fail course, a student passes if the grade is greater than or equal to 70
and fails if the grade is lower than 70. Write a C++ program that accepts a grade and prints the
message “A passing grade” or “A failing grade,” as appropriate.
#include <iostream>
using namespace std;
int main(){
double grade;
cout << What is your grade<<endl;
cin >> grade;
if (grade < 70) {
cout << "A failing grade" << endl;
}
else{ cout << "A passing grade" << endl;}
return 0;
}
b. How many runs should you make for the program written in Exercise 4a to verify that it’s
operating correctly? What data should you input in each program run?
One pass is all that is required. The data should be an postive integer or floating point number.
5. (Program) a. If money is left in a particular bank for more than 5 years, the bank pays interest
at a rate of 4.5%; otherwise, the interest rate is 3.0%. Write a C++ program that uses the
cin object to accept the number of years in the variable numYears and display the correct
interest rate, depending on the value input into numYears.
#include <iostream>
using namespace std;
int main(){
int numYears;
cout << "Enter total number of years:"<<endl;
cin >> numYears;
if (numYears > 5){
cout << "Your current interest rate is 4.5%" << endl;
}
else { cout << "Your current interest rate is 3.0% <<endl;}
return 0;
}
b. How many runs should you make for the program written in Exercise 5a to verify that it’s
operating correctly? What data should you input in each program run?
One run is all that is required. The data type should be an integer.
6. (Practice) a. Write a C++ program to display the message “PROCEED WITH TAKEOFF”
or “ABORT TAKEOFF” depending on the input. If the character g is entered in the variable
code, the first message should be displayed; otherwise, the second message should be
ndisplayed.
#include <iostream>
using namespace std;
int main(){
char your_character;
cout << "Enter Character" << endl;
cin >> your_character;
if( your_character = 'g') {
cout << "PROCEED WITH TAKEOFF" << endl;}
else{
cout << "ABORT TAKEOFF" << endl;
}
return 0;
}
b. How many runs should you make for the program written in Exercise 6a to verify that it’s
operating correctly? What data should you input in each program run?
Two runs are required for each case test. The dat input should be a character type.
7. (Program) Write, compile, and run a C++ program that accepts a user-input integer number
and determines whether it’s even or odd. The output should display the message “The
entered number is even.” or “The entered number is odd.” corresponding to the number the
user entered. (Hint: An even number has a 0 remainder when divided by 2.)
#include <iostream>
using namespace std;
int main(){
int number;
cout << "Enter number"<<endl;
cin >> number;
if( number % 2 == 0){
cout << "The entered number is even" << endl;}
else{ cout << The entered number is odd;}
return 0;
}
8. (Program) Write, compile, and run a C++ program that accepts a user-entered number and
calculates the square root and the reciprocal. Before calculating the square root, validate that
the number isn’t negative, and before calculating the reciprocal, check that the number isn’t
zero. If either condition occurs, display a message stating that the operation can’t be calculated.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double number,solution,solution2;
cout << "Enter your number" << endl;
cin >> number;
if (number >= 0 && number != 0){
solution = sqrt(number);
cout << solution << endl;
solution2 = (1 / number);
cout << solution2 << endl;
}
else if (number == 0){
solution = sqrt(number);
cout << solution << endl;
cout << "The operation can't be calculated" << endl; }
return 0;
9. (Program) Years that are evenly divisible by 400 or are evenly divisible by 4 but not by 100
are leap years. For example, because 1600 is evenly divisible by 400, 1600 was a leap year.
Similarly, because 1988 is evenly divisible by 4 but not by 100, it was also a leap year. Using
this information, write a C++ program that accepts the year as user input, determines whether
the year is a leap year, and displays a message telling the user whether the entered year is or
is not a leap year.
#include <iostream>
using namespace std;
int main() {
int year;
cout << "What is the year" << endl;
cin >> year;
bool leap = (year % 4 == 0);
if (leap == true){
cout << "This year is a leap year" << endl;
}
else{
cout << "This year is not a leap year" << endl; }
return 0;
}
10. (Program) a. Write, compile, and run a C++ program to compute and display a person’s
weekly salary as determined by the following conditions: If the hours worked are less than or
equal to 40, the person receives $12.00 per hour; otherwise, the person receives $480.00 plus
$18.00 for each hour worked over 40 hours. The program should request the hours worked as
input and display the salary as output.
#include <iostream>
using namespace std;
int main() {
double hours,total;
cout << "Enter hours worked" << endl;
cin >> hours;
if (hours > 40){
total=(12 * 40) + 480 * (18 *( hours - 40));
cout <<" Your earned this much << total <<endl;
}
else if(hours <= 40) {
total = (12 * hours);
cout << "You earned this much" << total << endl;
}
return 0;
}
b. How many runs should you make for the program written in Exercise 10a to verify that it’s
operating correctly? What data should you input in each program run?
Two runs to test each case is required at least. The data should be an integer or floationg point type number.
11. (Program) a. A senior salesperson is paid $800 a week, and a junior salesperson, $500 a
week. Write a C++ program that accepts as input a salesperson’s status in the character variable
status. If status equals s, the senior salesperson’s salary should be displayed; otherwise,
the junior salesperson’s salary should be displayed.
#include <iostream>
using namespace std;
int main(){
char status;
status = 's';
if(status == true){
cout << "Senior salesperson weekly salary status is "<< 800 <<end;
else{ cout << "Must be a junior salesperson " << 500 <<" is their salary " endl;}
return 0;
}
b. How many runs should you make for the program written in Exercise 11a to verify that it’s
operating correctly? What data should you input in each program run?
two runs is required, character data type should be used.
12. (Program) a. Write a C++ program that displays the message “I feel great today!” or “I feel
down today #$*!” depending on the input. If the character u is entered in the variable ch, the
first message should be displayed; otherwise, the second message should be displayed.
#include <iostream>
using namespace std;
int main(){
char feelings;
cout << What's your feeling character << endl;
cin >> feelings;
if ( feelings == 'u'){
cout << "I feel great today!" << endl;
}
else {cout << "I feel dow today #$*!" << endl;}
return 0;
}
b. How many runs should you make for the program written in Exercise 12a to verify that it’s
operating correctly? What data should you input in each program run?
Two runs is recommended, the data type should be a character.
13. (Program) a. Write a program to display the following two prompts:
Enter a month (use a 1 for Jan, etc.):
Enter a day of the month:
Have your program accept and store a number in the variable month in response to the first
prompt and accept and store a number in the variable day in response to the second prompt.
If the month entered isn’t between 1 and 12, display a message informing the user that an
invalid month has been entered. If the day entered isn’t between 1 and 31, display a message
informing the user that an invalid day has been entered.
#include <iostream>
using namespace std;
int main(){
int month,day;
cout << "Enter a month (use a 1 for Jan, etc.):" << endl;
cin >> month;
cout << "Enter a day of the month:" << endl;
cin >> day;
if( month > 12 || month < 1){
cout << "invalid month has been entered" << endl;
}
else if( day < 1 || day > 31){
cout << "invalid day has been entered" << endl;}
return 0;
}
b. What will your program do if the user enters a number with a decimal point for the month?
How can you make sure your if statements check for an integer number?
the decimal point will be ignored. By simplying using an integer data-type.
14. (Program) a. Write, compile, and run a C++ program that accepts a character as input data and
determines whether the character is a lowercase letter. A lowercase letter is any character that’s
greater than or equal to “a” and less than or equal to “z.” If the entered character is a lowercase
letter, display the message “The character just entered is a lowercase letter.” If the entered
letter isn’t lowercase, display the message “The character just entered is not a lowercase letter.”
#include <iostream>
using namespace std;
int main(){
char my_ch;
cout << "Enter your character" << endl;
cin >> my_ch;
if (my_ch >= 'a' && my_ch <= 'z'){
cout << "The character just entered is a lowercase letter." << endl;
}
else {cout << "The character just entered is not a lowercase letter." << endl; }
return 0;
}
b. Modify the program written for Exercise 14a to also determine whether the entered character
is an uppercase letter. An uppercase letter is any character greater than or equal to “A”
and less than or equal to “Z.”
#include <iostream>
using namespace std;
int main(){
char my_ch;
cout << "Enter your character" << endl;
cin >> my_ch;
if (my_ch >= 'A' && my_ch <= 'Z'){
cout << "The character just entered is a uppercase letter." << endl;
}
else {cout << "The character just entered is not a uppercase letter." << endl; }
return 0;
}
15. (Program) Write, compile, and run a C++ program that first determines whether an entered
character is a lowercase or an uppercase letter (see Exercise 14). If the letter is lowercase,
determine and print its position in the alphabet. For example, if the entered letter is c, the
program should print 3 because c is the third letter in the alphabet. (Hint: If the entered character
is lowercase, its position can be determined by subtracting 'a' from the letter and adding
1.) Similarly, if the letter is uppercase, determine and print its position in the alphabet. For
example, if the entered letter is G, the program should print 7 because G is the seventh letter
in the alphabet. (Hint: If the entered character is uppercase, its position can be determined by
subtracting 'A' from the letter and adding 1.)
#include <iostream>
using namespace std;
int main(){
int location;
char my_ch;
cout << "Enter your character" << endl;
cin >> my_ch;
if (my_ch >= 'a' && my_ch <= 'z'){
cout << "The character just entered is a lowercase letter." << endl;
location = (my_ch - 'a') + 1;
cout << "the location is " << location << endl;
}
else if (my_ch >= 'A' && my_ch <= 'Z'){
cout << "The character just entered is a uppercase letter." << endl;
location = (my_ch - 'A') + 1;
cout << "The location is " << location << endl;
}
else { cout << "The character just entered is not a lowercase or uppercase letter." << endl; }
return 0;
}
16. (Program) Write, compile, and run a C++ program that asks the user to input two numbers.
After your program accepts these numbers by using one or more cin object calls, have it
check the numbers. If the first number entered is greater than the second number, the program
should print the message “The first number is greater.”; otherwise, it should print the
message “The first number is not greater than the second.” Test your program by entering
the numbers 5 and 8 and then using the numbers 11 and 2. What will your program display if
the two numbers entered are equal?
#include <iostream>
using namespace std;
int main(){
double number1,number2;
cout << "Enter your first number" << endl;
cin >> number1;
cout << "Enter your second number" << endl;
cin >> number2;
if (number1 > number2){
cout << "The first number is greater" << endl;
}
else { cout << "The first number is not greater than the second" << endl; }
return 0;
}
17. (Debug) The following program displays the message Hello there! regardless of the letter
input. Determine where the error is and why the program always causes the message to be
displayed.
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << “Enter a letter: “;
cin >> letter;
if (letter = 'm')
cout << “Hello there!” << endl;
return 0;
}
--------------Corrected Code-------------------
#include <iostream>
using namespace std;
int main()
{
char letter;
cout << "Enter a letter : ";
cin >> letter;
if (letter == 'm') // The letter wasn't being tested for equivalence.
{
cout << "Hello there!" << endl;
}
else { cout << "Sorry wrong letter" << endl; }
return 0;
}