Vous êtes sur la page 1sur 7

7-Oct-16 W 4 Lab Exercises - Selection Structure

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!";

c) int m = 0; d) int num=7;


if (m = 0) switch (num/2)
cout<< "if!"; {
else case 5:
cout<< "else!"; num = num - 3;
case 3:
num--;
case 1:
num = num + 5;
break;
case 4:
num++;
default:
num/=2;
}
cout<<num;

2. Answer the questions below concerning the following fragment of code.

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";
}

4. Rewrite the following if-else chain by using a switch statement:

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;

5. Determine the value of the following expressions, assuming a = 4, b = 6,c = 2, and d = 3:


a) a == 8
b) b * d == c * c
c) d % b * c > 5 && c % b * d <8
7-Oct-16 W 4 Lab Exercises - Selection Structure
P1
Write a program to prompt user to insert an alphabet, determine the letter that user entered and output
message according to the input. (Hint, you may refer to the ASCII character codes table to form the
condition expression, or use standard library to simplify your codes, eg. check online the function to check
if a character is capital/lower letter)
Expand your program by adding another selection structure, when user key in any other character, output
not an alphabet
Example sample run:
7-Oct-16 W 4 Lab Exercises - Selection Structure
P2
Using if-else chain structure, write a program to display exam grade based on user input marks, according
to the criteria as below:
Grade Mark
A Same as or more than 80
B Between 70 to 79
C Between 60 to 69
D Between 50 to 59
E Between 40 to 49
F Less than 40
Depends on how you design the algorithm, when user entered a number over 100, the output grade might
be A, and when user entered -10, the output grade might be F, these should be avoided. Edit your program
to fix the problem so that it will only receive marks within range of 0-100.

Example sample run:


7-Oct-16 W 4 Lab Exercises - Selection Structure
P3
Using SWITCH statement structure
i. Based on the Marks and Grades table as in program P2 above, write a program to accept an
alphabet, and output the marks range for the grade inserted.
ii. Enhance the program so that the user only can enter either small letter or capital letter for a,b,c,d,e
or f, other than that it will display Invalid alphabet entered!

Example sample run:


Small letter b

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:

First test, set of sample a=1, b=-5, c=6


Enter coefficient for the quadratic equation
a: 1
b: -5
c: 6
The first solution is 3
The second solution is 2

Second test, set of sample a=2, b=2, c=2


Enter coefficient for the quadratic equation
a: 2
b: 2
c: 2
The equation has no real solution

Third test, set of sample a=2, b=4, c=2


Enter coefficient for the quadratic equation
a: 2
b: 4
c: 2
Equation has a single root of -1
7-Oct-16 W 4 Lab Exercises - Selection Structure
P6

Material's A B C D
Grade

Year of Proc

before 2010 200/kg 100/kg 50/kg 25/kg

2010-2012 300/kg 150/kg 75/kg 37.5/kg

2013-2016 500/kg 250/kg 125/kg 62.5/kg

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:

First test, grade = A, year =2015


Enter the material's grade (A,B,C or D): A
Enter the material's production year: 2015
The material grade A produced on 2015 cost $500 per kg.
Press any key to continue . . .

Second test, grade = D, year =2000


Enter the material's grade (A,B,C or D): D
Enter the material's production year: 2000
The material grade D produced on 2000 cost $25 per kg.
Press any key to continue . . .

Third test, grade = X, year =2013


Enter the material's grade (A,B,C or D): X
Enter the material's production year: 2013
Grade X does not exist in database!
Invalid input, please try again
Press any key to continue . . .

Vous aimerez peut-être aussi