cs140_labsheet_6.pdf
TRANSCRIPT
-
CS 140 / Programming language 1
Lab Sheet #6
Imam Mohammad bin Saud Islamic University
Girls Education Center Collage of Computing and Information Science
Title: Selection Statements
Lab lecture: (Sun 2/11/2014 to Thu 6/11/2014)
Due Date: 9 am Sunday 2/11/2014
What to submit: your solutions to exercises 1,2,3,4,6 and 8( 5,7 are optional).
You have to submit a screenshot of your source code and program run.
Work type: Individual work
-
CS 140 / Programming language 1
Lab Sheet #6
Imam Mohammad bin Saud Islamic University
Girls Education Center Collage of Computing and Information Science
Exercise 1: Find the errors in the following code and identify their types:
a) 1. #include
2. using namespace std;
3.
4. int main()
5. {
6. double x;
7. cout > x;
9. if( x != 5 );
10. cout 0 )
11. cout
-
CS 140 / Programming language 1
Lab Sheet #6
Imam Mohammad bin Saud Islamic University
Girls Education Center Collage of Computing and Information Science
Exercise 2: What is the output of the following programs:
a)
#include using namespace std; int main() { int x=3; bool a = !x; if( !x || a ) cout
-
CS 140 / Programming language 1
Lab Sheet #6
Imam Mohammad bin Saud Islamic University
Girls Education Center Collage of Computing and Information Science
Exercise 3: Write the following if-else statement using switch statement
int age; cout > age; if( age == 1 || age == 2 ) cout 2 && age
-
CS 140 / Programming language 1
Lab Sheet #6
Imam Mohammad bin Saud Islamic University
Girls Education Center Collage of Computing and Information Science
Exercise 5: Write a complete C++ program that ask the user to input two integers, then print if they are divisible by each other or not ( the division has no carry). If they
are divisible print their division results on each other. Make sure avoid the divide by
zero situation before testing their divisibility.
Here is a sample output:
Exercise 6: Write a complete C++ program that calculates the cost of train ticket as the following,
If the passenger is an adult (i.e. +17 years old) his ticket costs 150 SR,
If the passenger is a kid (i.e. 16~3 years old) his ticket costs 80 SR,
If the passenger is a baby (i.e. less than 2 years old) his ticket costs 40 SR,
Also, if the passenger wants a meal on the train it cost for the adult is 25 SR, 15 SR
for the kids meal and the baby meal is for free.
If the passenger has a discount coupon he gets 15% off the total cost.
Here is a sample output:
-
CS 140 / Programming language 1
Lab Sheet #6
Imam Mohammad bin Saud Islamic University
Girls Education Center Collage of Computing and Information Science
Exercise 7: Write a Complete C++ program that reads from the user 3 numbers and displays them in descending order.
Here is a sample output:
Exercise 8: Write a Complete C++ program to determine the cost of an automobile insurance premium, based on driver's age and the number of accidents that the driver
has had. The basic insurance charge is $500. There is a surcharge of $100 if the
driver is under 25 and an additional surcharge for the number of accidents:
# of accidents Accident Surcharge
1 50
2 125
3 225
4 375
5 575
6 or more No insurance
Here is a sample output: