Académique Documents
Professionnel Documents
Culture Documents
1. What is the output produced from the following segments of C++ code?
a) int m = 0; b) int m= -5;
if (age = 0) if (m)
cout<< "if"; cout<< "impossible!";
else else
cout<< "else"; cout<< "possible!";
int n;
cout<< "Enter an integer: ";
cin>> n;
if (n < 10)
cout << "less than 10" <<endl;
else if (n > 5)
cout<< "greater than 5" <<endl;
else
cout << "not interesting" <<endl;
(i) What will be the output of the fragment above if the interactive user enters the integer value 0?
(ii) What will be the output of the fragment above if the interactive user enters the integer value 15?
(iii) What will be the output of the fragment above if the interactive user enters the integer value 7?
(iv) What values for n will cause the output of the fragment above to be "not interesting"?
7-Oct-16 W 4 Lab Exercises - Selection Structure
3. Rewrite the following switch statement by using an if-else chain:
switch(letterGrade)
{
case 'A':
cout<< "The numerical grade is between 90 and 100\n";
break;
case 'B':
cout<< "The numerical grade is between 90 and 100\n";
break;
case 'C':
cout<< "The numerical grade is between 70 and 79.9\n";
break;
case 'D':
cout<< "How are you going to explain this one?\n";
break;
default:
cout<< "Of course I had nothing to do with my grade.\n";
cout<< "It must have been the professor's fault.\n";
}
if (factor == 1)
pressure = 25.0;
else if (factor == 2)
pressure = 36.0;
else if (factor == 3)
pressure = 45.0;
else if (factor == 4) || (factor == 5)
pressure = 49.0;
else pressure = 51.0;
Capital letter b
Special Character
Number
P4
Write a C++ program that accepts a number followed by one space and then a letter. If the letter following
the number is f, the program is to treat the number entered as a temperature in degrees Fahrenheit,
convert the number to the equivalent degrees Celsius, and display a suitable message. If the letter
following the number is c, the program is to treat the number entered as a temperature in degrees Celsius,
convert the number to the equivalent degrees Fahrenheit, and display a suitable message. If the letter is
neither f nor c, the program is to display a message that the data entered is incorrect and terminate. Verify
your program is working by hand-calculating with some random data (eg. 1 c = 33.8 f). Use an if-else chain
in your program and make use of these conversion formulas:
5.0
= ( ) ( 32.0)
9.0
9.0
= ( ) + 32.0
5.0
7-Oct-16 W 4 Lab Exercises - Selection Structure
P5
In algebra, you learned that the standard quadratic equation ax2 + bx + c = 0 has two solutions given by the formula
b b 2 4ac
x
2a
The first solution is obtained by using + in place of ; the second is obtained by using in place of . Most of this
expression contains simple operators covered in our lectures.
Write a program that accepts values for a, b, and c, as integer and then calculates the two solutions, as
doubles. If the quantity under the square root sign is negative, the equation has no real solutions, and your
program should display a message to that effect. You may assume that the value for a is nonzero. Verify your
program is working by hand-calculating with some random data. Examples of sample run:
Material's A B C D
Grade
Year of Proc
Table above represents the cost of a material, the materials grade and the year of production. The cost
per kg of the material if differs in terms of grade and year of production. For example, the grade A material
produced on year before 2010 cost $200 / kg. Write a simple program to assist the user to check the cost
of the material. First, the program should prompt the user to enter the grade (eg. A) and follow by the
year (eg. 2015). Next, the program should display the cost per kg of the material (eg. $500). . Examples of
sample run: