#include<iostream> using namespace std; int main() { int value; cout << "enter an integer: "; cin >> value; if(cin.eof()) cout << "EOF .. "; if(cin.fail()) cout << "FAIL .. "; cout << "value= " << value << endl; system("pause"); return 0; }Run the same program several times and explain the output corresponding to the following different input
Bronson's textbook - references | ||
Section | Page | Questions |
---|---|---|
§4.1 | 138//142 | 2(a), 2(d), 4(c) |
§4.2 | 150//156 | 4, 8 |
§4.3 | 157//165 | 4 (Program 4.5) (or 3)
Extend Q4: draw the flowchart of the C++ program in the question. |
§4.4 | 164//171 | 1, 5 |
§4.5 | 166//173 | 1, 3 |
Bronson's textbook cf. §4.1-4.5 | |
No. | Question Detail |
---|---|
B.1 |
Using parentheses, rewrite the following expressions to indicate the correct
order of evaluation. Then evaluate each expression, assuming a=5,
b=2, and c=4.
|
B.2 |
Determine the value of the following expressions, assuming
a=5, b=2, c=4, and d=5.
|
B.3 |
|
B.4 | Write a C++ program that accepts a character using the cin object and determines whether the character is a lowercase letter. A lowercase letter is any character that is 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 is not lowercase, display the message The character just entered is not a lowercase letter. |
B.5 |
Write a C++ program that accepts a number followed by one space and
then a letter. If the letter following the number is f, then
program is to treat the number entered as a temperature in degrees
Fahrenheit, convert the number to the equivalent temperature in
degrees Celsius, and print a suitable display message. If the letter
following the number is c, the program is to consider the
number entered as a Celsius temperature, convert the number to the
equivalent Fahrenheit temperature, and print a suitable message. If
the letter is neither f nor c, the program is to print a
message that the data entered is incorrect and then terminate. Use an
if-else chain in your program and make use of the conversion
formulas
Celsius = (5.0 / 9.0) * (Fahrenheit - 32.0) Fahrenheit = (9.0 / 5.0) * Celsius + 32.0
NOTE: It may be helpful for a tutor to completely exhibit how to solve
this question before other programming questions are being attempted by
the students in class.
|
B.6 |
Using the commission schedule, the following
program calculates monthly income.
#include <iostream> #include <iomanip> using namespace std; int main() { double monthlySales, income; cout << "Enter the value of monthly sales: "; cin >> monthlySales; if (monthlySales >= 50000.00) income = 375.00 + .16 * monthlySales; if (monthlySales >= 40000.00 && monthlySales < 50000.00) income = 350.00 + .14 * monthlySales; if (monthlySales >= 30000.00 && monthlySales < 40000.00) income = 325.00 + .12 * monthlySales; if (monthlySales >= 20000.00 && monthlySales < 30000.00) income = 300.00 + .09 * monthlySales; if (monthlySales >= 10000.00 && monthlySales < 20000.00) income = 250.00 + .5 * monthlySales; else income = 200.00 + .3 * monthlySales; cout << setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(2) << "The income is $" << income << endl; return 0; }
|
B.7 |
Rewrite the following if-else chain using a switch statement:
if (letGrad == 'A') cout << "The numerical grade is between 90 and 100"; else if (letGrad == 'B') cout << "The numerical grade is between 80 and 89.9"; else if (letGrad == 'C') cout << "The numerical grade is between 70 and 79.9"; else if (letGrad == 'D') cout << "How are you going to explain this one?" << endl; else { cout << "Of course I had nothing to do with the grade." << endl; cout << "\nThe professor was really off the wall." << endl; } |
B.8 |
Rewrite §4.3: Q3 in the above, i.e.
#include <iostream> using namespace std; int main() { double fahr, celsius, temp; char letter; cout << "\nEnter a temperature followed by a space\n" << "and an 'f' for Fahrenheit or a 'c' for Celsius.\n"; cin >> temp >> letter; if (letter == 'f' || letter == 'F') { celsius = (5.0/9.0)* (temp - 32.0); cout << temp << " degrees Fahrenheit = " << celsius << " degrees Celsius\n"; } else if (letter == 'c' || letter == 'C') { fahr = (9.0/5.0)* (temp + 32.0); cout << temp << " degrees Celsius = " << fahr << " degrees Fahrenheit\n"; } else cout << "\nThe data entered is invalid.\n"; return 0; }using a switch statement instead of an if-else chain. |
B.9 |
Problem: Using the assignment operator, =, in place of the relational operator,
==. This can cause an enormous amount of frustration because any
expression can be tested by an if-else statement. For example,
the statement
if (opselect = 2) cout << "Happy Birthday"; else cout << "Good Day";always results in the message Happy Birthday being printed, regardless of the initial value in the variable opselect. This is because the assignment expression opselect = 2 has a value of 2, which is considered a true value in C++. The correct expression to determine the value in opselect is opselect == 2. |
B.10 |
Problem: Using nest if statements without including braces to indicate clearly
the desired structure. Without braces, the compiler defaults to the pairing
else with the closest unpaired ifs, which sometimes
destroys the original intent of the selection statement. To avoid this
problem and to create code that is readily adaptable to change, it is
useful to write all if-else statements as compound statements
in the form
if (expression) { one of more statements in here } else { one ore more statements in here }Using this form maintains the original integrity and intent of the if statement, no matter how many statements are added later. |
#include<iostream> using namespace std; int main( ) { int value=0; if ( 4 > 5 ) if ( 5 > 4 ) value=1; else value=2; cout << value << endl; system("pause"); return 0; }Run the above program to verify your conclusion.