0% found this document useful (0 votes)
66 views6 pages

C++ Selection Statements Lab Guide

The document contains instructions and exercises for a programming lab assignment. It includes 6 exercises involving conditional statements, loops, and functions. Students are asked to submit solutions to exercises 1, 2, 3, 4, 6 and 8 by the due date of Sunday 2/11/2014.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views6 pages

C++ Selection Statements Lab Guide

The document contains instructions and exercises for a programming lab assignment. It includes 6 exercises involving conditional statements, loops, and functions. Students are asked to submit solutions to exercises 1, 2, 3, 4, 6 and 8 by the due date of Sunday 2/11/2014.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. double x;
7. cout << "\nEnter a number: ";
8. cin >> x;
9. if( x != 5 );
10. cout << "Your number is 5";
11. else
12. cout << "Your number is not 5";
13. return 0;
14. }

b)
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int x;
7. cout << "\nEnter a number: ";
8. cin >> x;
9. if( x%2 == 0 )
10. if( x > 0 )
11. cout << x << " is even & positive";
12. else
13. cout << x << " is odd";
14. return 0;
15. }










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 <iostream>
using namespace std;
int main()
{
int x=3;
bool a = !x;

if( !x || a )
cout << a << endl;
else
if( x )
cout << x++ << endl;

switch (x)
{
case 3:
cout << "X \n";
break;
case 4:
cout << "A \n";
default:
cout << "D \n";
}
return 0;
}
b)
#include <iostream>
using namespace std;
int main()
{
int x=1, y=0;

if( x++ == ++y )
cout << x << endl;
else
cout << y << endl;

bool a = x;
bool b = ( a && y < x);

switch (b)
{
case true:
cout << "X \n";
case false:
cout << "Y \n";
break;
default:
cout << "D \n";
}
return 0;
}









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 << "Enter the age: ";
cin >> age;

if( age == 1 || age == 2 )
cout << "The baby clothes are in the west section";
else
if ( age > 2 && age <=6 )
cout << "The toddler clothes are in the east section";
else
cout << "Sorry we don't have the sizes fo this age!";



Exercise 4: Write the following switch statement using if-else statement:

int s;
cout << "Enter number of stars from 1 to 3: ";
cin >> s;

switch(s)
{
case 3: cout<<"*";
case 2: cout<<"*";
case 1: cout<<"*";
break;
default: cout << "\nWrong Number!.";
}














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:

You might also like