Upload
naeem-nasser
View
1
Download
0
Embed Size (px)
DESCRIPTION
c++ lab
Citation preview
1
Qatar University
College of Engineering
Dept. of Computer Science & Eng’g
Computer Programming -
GENG106 - Lab Handbook
Spring 2013 Semester
Lab4: More on Selection
Objectives
At the end of this lab, you should be able to
Use the switch statement.
Use the ternary operator (the conditional expression).
Work more with nested if statements.
Convert nested if statements to switch statements.
Quick Review
switch-statement syntax:
Note:
The selector is an integral constant (i.e. char or one of the integer types).
The default part of the switch is optional.
The break statement is very important. If omitted, then the current case and the following
cases will all execute, which is not usually the required logic. In some occasions, however,
we may want to execute a statement if more than one case of the selector is satisfied. In such
occasions we omit the statement and the break from all the cases for which this statement
must be executed, and leave it for the last one (see exercise 2).
The ternary operator:
Note:
The ternary operator can be used to replace an if/else statement.
The value assigned to variable depends on the condition.
The value if true / value if false could be an expression or an input
command. The data type, however, must be compatible with that of variable.
variable = condition ? value if true : value if false ;
switch (selector)
{
case label1: statement1;
break;
case label2: statement2;
break;
...
case labeln: statementn;
break;
default: statementd; // optional
break;
}
2
1) The following program is a demonstration for using the switch statement.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int yearCode;
string yearName;
cout << "Please enter the number of a class year (1-5) : ";
cin >> yearCode;
switch (yearCode)
{
case 1:
yearName ="Freshman";
break;
case 2:
yearName = "Sophomore";
break;
case 3:
yearName = "Junior";
break;
case 4:
yearName = "Senior";
break;
case 5:
yearName = "Graduate";
break;
default:
cout << "YearName: code error: " << yearCode << endl;
yearName = "";
}
cout << yearName << endl;
return 0;
}
Your Answer:
Run the program and write down the output when 1 is entered; when 3 is entered; and when 9 is
entered.
___________________________________________________________________
___________________________________________________________________
Modify the above program by commenting out the break statements in case 1, case 2, … case 5
(i.e. by typing // before the keyword break). What is output by the program when 2 is entered and
when 4 is entered?
___________________________________________________________________
___________________________________________________________________
3
1) Write an interactive program that asks the user to enter the first letter of a geometric shape, and then
let your program calculate and output the and of the chosen shape. For simplicity,
let your program deal only with rectangles and circles.
Hints:
For a rectangle: , ))
For a circle: where is the radius.
Your program must display an error message to indicate that the user entered a character
other than ’R’, ’r’, ’C’ or ’c’.
Use the switch statement to decide which formulas to be used.
2) In Question #3 of Lab-3 you were asked to use the if/else statement to code the following function.
Complete the following ternary operator to calculate
= ? ____________________ : _______________________;
Now, make a new copy of your solution to Q3 of Lab-3 and then replace the if/else statement (in the
copy) with the above ternary operation.
Which one of the two programs has fewer lines? __________________
3) Complete the following statement that determines the number of days in February based on the
Boolean variable leap of Question #2 (Lab-3):
= ? 29 : 28;
Modify your program to use this statement instead of the if/else statement.
4) Write a C++ program that asks the user to input the wattage (or watts) of a standard light bulb and
uses a switch statement to assign to the variable lumens the expected brightness. Use this table:
Watts Brightness (lumens)
15 125
25 215
40 500
60 880
75 1000
100 1675
Assign the value -1 to lumens if the value of watts is not in the table.
4
5) Convert the following nested if statement to a switch statement and then use it in a program that asks
the user for the integer value choice and print the correct message.
if (choice == 1) cout << “You have chosen one”;
else if (choice == 2)
cout << “You have chosen two”;
else if (choice == 3)
cout << “You have chosen three”;
else if (choice == 4)
cout << “You have chosen four”;
else
cout << “Invalid choice\n”;
6) The table below shows the normal boiling points of several substances. Write a program that
prompts the user for the observed boiling point of a substance in °C and identifies the substance if
the observed boiling point is within 5% of the expected boiling point. If the data input is more than
5% higher or lower than any of the boiling points in the table, the program should output the
message “Substance unknown”.
Substance Normal boiling point(°C)
Water 100
Mercury 357
Copper 1187
Silver 2193
Gold 2660
7) Modify the program of Question#5 (Lab-3) to comply with the conditions given in the following
table:
pH Range Solution is
pH<3 very acidic
3<pH<7 Acidic
pH=7 Neutral
7<pH<12 Alkaline
pH>12 very alkaline
5
8) Isaac Newton discovered that a glass prism can split white light apart into its constituent colors.
Wavelengths of visible light fall approximately into the range 400 – 700 nm (nm = nano-meter =
10-9
meter).
The table below shows the relationship between wavelength and color.
Color Wavelength range (nm)
Violet 400 - 424
Blue
424 - 491
Green
491 - 575
Yellow
575 - 585
Orange
585 - 647
Red
647 - 700
Write a C++ program that inputs a wavelength and then displays the associated light color. If the
wavelength is shorter than 400 nm or longer than 700 nm, display the message “Wavelength
outside visual range”. Classify boundary wavelengths as the lower-wavelength color. For
example, label a wavelength of 424 nm as violet.