0% found this document useful (0 votes)
11 views

PS2 - Selection Statements

Uploaded by

sabasandhu0010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

PS2 - Selection Statements

Uploaded by

sabasandhu0010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CTIS 151 Introduction to Programming

PROBLEM SET 2

Selection Statements

1. Find the results of the following logical expressions, if a = 3, b = 5. Show each step of
calculation clearly.

a) ! (b / a * 2 == 2) || pow (a, 2) >= b


b) !(b / a * 2 == 2 || pow (a, 2) >= b)
c) b / 2 + 1 <= a || abs (a - b) > 1 && ! (b > a + 1)
d) a + b * 2 != sqrt (b + 4) + 10 || !(5 / a == 2) && b + 16 /a/b == 6
e) a / 2 == 1 && b / 2 * 2 == b || ! (b – a * 2 <= 0)

2. Write a single selection statement (if, if … else, or nested if) for the following
conditions.

a) if a > b calculate c as a + b, and output a, b, and c


if a <= b do nothing

b)  if b <= 2 calculate c as 5ab+2, output c


if a == b 
 if b > 2 calculate c as,  4b – 2a2 , output c

Do you think that it would make any difference if the output statement was outside the block?

c) if both a and b are non-negative, output their sum


if a is non-negative, but b is negative, output a, but don’t output b
if a is negative, but b is non-negative, output b, but don’t output a
if both a and b are negative, output their product

d)  if a == b calculate c as the square of a, output c


if a is negative 
 if a != b calculate c as the cube of a, output c

if a is zero output the message “Zero”

 if a < b output a
if a is positive  if a == b output the message “They are equal”
 if a > b output b

3. Draw the flowchart of each of the conditions in Question 2.

Prb Set 2 - Selection Statements Page 1 / 6


CTIS 151 Introduction to Programming

4. Write a selection statement for each of the following operations

a) Display B for grades larger than 80 and smaller than or equal to 90.
b) Assign x+1 to y if x is outside the interval 2 to 5, x otherwise.
c) Display Unsuccessful if a grade is not greater than 50.

5. Trace and find the output of the following program segments.

a)
int n1, n2;
n1 = 25;
n2 = 50;
if (n1 < n2 && !(n2 != 2 * n1))
{
n1 = n1 * 2;
n2 = n2 * 2;
printf(“%d %d\n”, n1, n2);
}
if (n2 < 2 * n1 || n1 == n2)
n1 = n1 / 2;
n2 = n2 / 2;
printf(“%d %d\n”, n1, n2);

b) Trace for a = -5 and a = 50

if (a > 0)
if (a < 40)
printf ("\nYES");
else
printf (“\nNO“);
printf ("\n%d", a);

6. Consider the following program segment:


int x, y;
double a, b;
x = 0;
y = 0;
scanf("%lf %lf", &a, &b);
if (x + a > y + b)
{
x = a;
y = x + b;
}
else if (a == 0)
b = b / 5;
else if (b == 0)
a = a * 2;
else
b = b * 2;
printf("%f %d %f %d", b, y, a, x);

Trace the above program segment and show its output for each of the following sets of
input data.

a) 2 5 b) 5 -2 c) -2.5 0 d) 0 2.5

Prb Set 2 - Selection Statements Page 2 / 6


CTIS 151 Introduction to Programming

7. Trace the following program segment and show its output, for all sets of data given in
question 6.

double a, k, z;
scanf(“%lf %lf”, &k, &a);
if (a > 0 && k > 0)
a = a + k;
else if (k == 0)
{
z = a;
a = k;
k = z;
}
else if (a < 0)
a = a + 1;
else
k = k * 2;
printf(“%f %f”, a, k);

8. Rewrite the following program segment using a single nested if statement, without
using logical operators (&&, ||, !).

if (n == 1 && m == 10)
x = 0;
if (n == 1 && m == 20)
x = 1;
if (n == 1 && m == 30)
x = 2;
if (n == 2 && m == 10)
x = 3;
if (n == 2 && m == 20)
x = 4;

9. Rewrite the selection statement in question 8 as a nested switch statement.

Prb Set 2 - Selection Statements Page 3 / 6


CTIS 151 Introduction to Programming

10. Analyze the following problems and write a program for each of them.

a) Input an integer number. If it is greater than 0, find its square. Otherwise find its cube.
b) Given a value x, calculate y according to the following formula:
-x if x < 0
y= 0 if x = 0
x2 if x > 0

c) A student fails a course if s/he takes less than 50 as overall grade. Given the overall grade
of a student, display whether s/he passed or failed.
d) A supermarket offers a 5% discount if your bill is more than 100 YTL. Given the customer’s
id number and bill, display his/her id number with the amount he/she has to pay (including
the discount if any).
3
e) Calculate the water bill for a customer where the price of water / m is 3 YTL for domestic
users and 2.5 YTL for industrial users. Input the user-id, consumption (amount of water
used), user code (D - domestic, I - industrial), and output the user-id and the amount to be
paid.
f) Given the loudness of a noice in decibels, display a perception message, using the following
table:
Loudness in Decibels Perception
50 or lower quiet
51 – 70 intrusive
71 – 90 annoying
91 – 100 very annoying
above 110 uncomfortable

g) Given a temperature in degrees Fahrenheit (F), convert it into degrees Centigrade (C) using
the formula given in the first problem set, and output the temperature value in degrees
Centigrade together with one of the messages:
Very Cold if C is in the range [–40, 0)
Cold if C is in the range [0, 10)
Normal if C is in the range [10, 20)
Hot if C is in the range [20, 30)
Very Hot if C is in the range [30, 50)
Out of Range if C is out of the range [-40, 50)

h) Given the letter grade of a student, output his/her situation according to the following table:

A High Honor
B Honor
C Satisfactory
D Poor
F Fail

i) Calculate the absolute difference of two given numbers (x and y), where the absolute
difference is x – y or y – x, whichever is positive. Do not use the abs or fabs function in
your solution.

Prb Set 2 - Selection Statements Page 4 / 6


CTIS 151 Introduction to Programming

j) Calculate the life time of a light bulb according to the following table (use switch statement)
Watts Life time in hours
15 2000
25 1500
40 1500
60 1000
75 1000
100 500

k) Given two integers, divide the larger one by the smaller, and display the result. Do not
assume that the numbers are entered in any order, and be careful not to cause to “Division
by zero” error. Display the message “INFINITE” in such a case.
l) Compute the area of a square (area = side2) or a triangle (area = ½ x base x height), given
the first character of the figure name (S or T). Notice that, you need to get different inputs
depending on the figure.
3
m) A substance floats in water if its density (mass / volume) is less than 1 gr/cm (1 gram per
cubic centimeter) but sinks otherwise. Given the mass in kg. and volume of an object in cm 3,
display whether it will sink or float.
n) The monthly telephone bills are calculated as follows: Each customer must pay 9.2 YTL as
the constant fee. If the cost of the telephone calls the customer made in that month does not
exceed that amount, the customer only pays that constant fee. However, if the customer
made too many calls, and the cost exceed that amount, he/she pays the cost of his/her calls.
The cost for one-minute of in-town calls is 0.25 YTL, of out-town calls is 0.35 YTL. Given the
durations for the in-town and out-town calls of a customer, calculate the bill he/she has to
pay.
o) In a company, a worker normally gets 250 YTL per week for upto 40 hours work. For each
extra working hour, he gets 7.5 YTL extra pay. Given a worker’s ID number and hours he
worked this week, display his ID number and his pay for the week.
p) A company gives 5% salary increase to its workers whose salaries are less than 750 YTL,
and 3% salary increase to the other workers. However, 5% of the increase a worker takes is
cut for a foundation. Given the amount of the previous salary of a worker in that company,
calculate his new salary.
q) The company in the previous question decided to give an extra 1.5% increase to the workers
who have been working in that company for more than 5 years. Modify your program, to
include this decision.
r) Given the lengths of three sides of a triangle, determine whether that triangle is equilateral
(all sides are equal), isosceles (two sides are equal) or scalene (no sides are equal).
s) Given the lengths of three sides of a triangle, determine whether that triangle is a right-
triangle or not, using the Pythagorean theorem (hypotenuse of a right-triangle is equal to the
square root of the sum of the squares of its perpendicular sides). Do not assume that, the
sides are given in any order.
t) Write a program that
 multiplies two numbers if they are both even
 adds two numbers if they are both odd
 subtracts bigger from the smaller if first is odd and second is even
 subtracts smaller from the bigger if first is even and second is odd

and output the result.

Prb Set 2 - Selection Statements Page 5 / 6


CTIS 151 Introduction to Programming

u) The cost to the consumer of a new car is the sum of the wholesale cost of the car, the
dealer’s percentage markup, and the state sales tax (applied to the markup price). The
dealer’s markup is 12% and the sales tax is 6% for the cars whose wholesale cost is
cheaper than 25000 YTL. However, these percentages increase to 14% and 7%,
respectively, for more expensive cars. Calculate the consumer’s cost, given the wholesale
cost of the car he buys.

v) Given a three-digit integer, display the sum of its digits, if the integer is greater than 500, the
product of its digits, if the integer is less than or equal to 500. Display an error message if the
given integer is not three-digit. For example, if the given integer is 760, the output should be
13, but if it is 352, the output should be 30.
w) The sign at a parking lot shows the parking costs as follows:
Cars:
First 2 hours No charge
Next 3 hours $0.50 / hour
Next 10 hours $0.25 / hour
Trucks:
First 1 hour No charge
Next 2 hours $1.00 / hour
Next 12 hours $0.75 / hour
Senior citizens: No charge

Given a character representing the vehicle type (C, T, or S) followed by the number of
minutes that vehicle has stayed in the lot, compute the appropriate charge for the customer.
Any part of an hour is to be counted as a full hour. For example, if a truck has stayed 250
minutes in the lot, the customer must pay as if it stayed 5 hours, thus he must pay $3.50
(= 0x1 + 1.00x2 + 0.75x2).
x) Calculate the total bill for having carpet laid in our home. The carpet shop says that the price
is 8.5 YTL/m2 plus 8% tax. There is a 15% discount if you buy more than 50 m2. Each worker
wants 35 YTL. You need 2 workers for more than 40 m2.
y) Calculate water bill
Price up to 50 m3 1.5 YTL/m3
Above 50 m3 2 YTL/m3 for each extra m3.
z) Calculate the total bill for a customer who buys books
Price of one book is 15 YTL + 12% VAT
If you buy more than 20 books, you get 5% discount

Prb Set 2 - Selection Statements Page 6 / 6

You might also like