You are on page 1of 4

Qatar University College of Engineering Dept.

of Computer Science & Engineering

Computer Programming GENG106 Lab Handbook Fall 2012 Semester

Lab3: Selection
Objectives At the end of this lab, you should be able to Use the if/else-statement and nested if-statement. Use compound logical expressions in if-statements (conditions that are connected with &&, ||).

Quick Review if - statement syntax if (condition) Statement1 if/else - statement syntax if (condition) Statement1 else Statement2 If the condition is true Statement1 is executed, otherwise Statement2 is executed.

If the condition is true Statement1 is executed, otherwise Statement1 is skipped. Notes

Statement1 or statment2 could be a simple statement or a compound statement (i.e. a block of several simple statements). The condition is represented by a relational expression and/or a logical expression see the tables below.
Relational Operators and Relational Expressions

Operator > < >= <= == !=

Example x>y x<y x>=y x<=y x==y x!=y

Meaning x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y x is equal to y x is not equal to y

Logical Operators and Logical Expressions

Operator && || !

Example Meaning (x>y)&&(x<z) x is greater than y and x is less than z (x>y)||(x<z) x is greater than y or x is less than z !(x>y) x is not greater than y (which means x is less than or equal to y)

1) Run the following program to check if a number entered by user is odd or even #include <iostream> using namespace std; int main() { int n; cout<<"Enter a number"; cin>>n; if(n%2==0) cout<<n<<" is an even number"; else cout<<n<<" is an odd number"; return 0; } Add another if /else statement to check if the entered number is positive or negative.

2) A leap year in the Gregorian calendar is a year in which February has 29 days. Can you tell which of the following years are leap: 1996, 2000, 2010, 3000? It is known that a year is leap if it is divisible by 4, unless year is a century year, e.g. 1800, 1900, 2000, etc, in which case it must be divisible by 400. In logical terms a year is leap if either of the following conditions holds: year is divisible by 4 but not 100. year is divisible by 400. The following C++ program asks the user to enter the year and then decides whether the year is leap or not. However, one important expression is missing. Complete and run the program using the test data given above.
#include <iostream> #include <cmath> using namespace std; int main(){ int year; bool divisibleBy4, divisibleBy100, divisibleBy400, leap; cout<<"enter year please "; cin>>year; divisibleBy4 = (year%4==0); divisibleBy100 = (year%100==0); divisibleBy400 = (year%400==0); leap = ___________________________________________________ if(leap) cout<<year<<" is leap"<<endl; else cout<<year<<" is not leap"<<endl; return 0; }

Modify your program to output the number of days in February instead of the leap/not leap message. 1

3) The function

is defined as:

Write a C++ program that asks the user to input and then calculate and output the value of corresponding to the entered value. Test your program by entering these values: -1, 0 and 1. 4) Last week, you wrote the C++ program to calculate the roots of the quadratic equation 2+ + =0 using the formula:

But, if 4ac > b2 then the value under the square root is negative and the roots are said to be complex. This is so because the curve does not cut the x-axis. Modify your program to calculate the roots only when . Otherwise, print a message saying that the roots are complex. Run your program using the coefficients of the equation represented by the curve on the graph given above. 5) The pH of a solution is a measure of its acidity. The following program asks the user to enter a pH value and then output a text message based on the input value. Carefully read this program and guess its output based on the pH values given below:
#include <iostream> using namespace std; int main() { double pH; cout<<"Enter pH please "; cin>>pH; cout<<The solution is : ; if(pH<3) cout<<very acidic. <<endl; else if(pH>=3 && pH <7) cout<<acidic.<<endl; else cout<<alkaline.<<endl; return 0; }

Write down the output based on the following pH values: pH=2.4, output: ___________________________ pH=3, pH=7, output: __________________________ output: __________________________

Run the above program in a step-by-step way to see how it executes based on the conditions. To run your program step-by-step either press F10 or from Debug menu choose Step over. 2

If we replace the nested if statement in the program with the following:


if(pH<3) cout<< very acidic.<<endl; if(pH>=3 && pH <7) cout<< acidic.<<endl; if(pH>=7) cout<< alkaline.<<endl;

will that make any difference regarding the output?!! Run the program again step-by-step after replacing the nested if statement. Which version makes your program executes faster? nested if statement separated if statements

6) Game designers often require a random die roll. Engineering applications sometimes need a set of random values within a specified range. To create a random die roll on a 6-sided die (i.e. to generate numbers between 1 and 6) use rand()%6+1. It is generally believed that more than 70% of games are coded using C++. Explore the rand() function in www.cplusplus.com ; simply type in the word rand in the search field. Write down an expression that generates a random number between 1 and 10. _______________________________________________________________ Now, write a C++ program which generates a random number between 1 and 10. It then asks the user to enter a guess, if the input matches the generated value, the user wins otherwise s/he loses. Note: Each time you run this program, youll get the same generated value. For this reason the rand() function is a pseudo-random function. Update your program such that you allow the user to enter the seed, so that you use a different seed on each new run. Tip: use the srand()function; explore it in www.cplusplus.com

7) Write a C++ program to generate three multiplications with numbers generated randomly (numbers between 2 and 12 inclusive). If the child user answers correctly, s/hell get an encouragement; otherwise the correct answer is displayed. Here is a sample run of the program; with users input is in red:
Do you want to multiply? Y 4 x 3 = 14 No Dear: 4 x 3 = 12 Sample output 4 x 5 = 20 Very Good . . . 3 x 9 = 36 No Dear: 3 x 9 = 27 Bye . . .
If the user replies by n, the program is terminated immediately

You might also like