You are on page 1of 5

Algorithms Design and Programming Techniques Tutorial 4

Tutorial 4: Simple Problems

1. If a=1, b=2, and c=3, what are the values of a, b and c at the end of the
following program segment?
if (a <= b)
if (c > 2)
c =2;
if (c < 3)
a = 0;
else
b = 0;
if (ch >= '0' && ch <= '9')
cout << "kind = digit";
else if (ch >= 'A' && ch <= 'Z')
cout << "kind = capital letter";
else if (ch >= 'a' && ch <= 'z')
cout << "kind = small letter";
else
cout << "kind = special";

2. Suppose the input is 5. What is the value of alpha after executing the following
C++ code?
cin>>alpha;
switch(alpha)
{
case 1:
case 2: alpha = alpha + 2; break;
case 4: alpha++;
case 5: alpha = 2 * alpha;
case 6: alpha = alpha +5; break;
default: --alpha;
}

3. Suppose the input is 3. What is the value of b after executing the following
C++ code?
cin>>b;
switch(b)
{
case 3: b = b + 3;
case 1: b++; break;
case 5: b = b + 5;
case 4: b = b + 4;
}

© Asst. Lect. Wasseem Nahi Ibrahem Page 1


Algorithms Design and Programming Techniques Tutorial 4

4. Write a program that reads a number and determines whether the number is
positive, negative, or zero.
5. Write a C++ program that computes the following equation:
5𝑥 2 + 6 0 ≤ 𝑥 ≤ 100
𝑦={ 0 𝑥≤0
2
𝑥 + 4𝑥 + 4 𝑥 > 100
6. Write a program that reads a character and prints out whether it is a vowel or a
consonant.

© Asst. Lect. Wasseem Nahi Ibrahem Page 2


Algorithms Design and Programming Techniques Tutorial 4

Tutorial 4: Real World Problems


1. Calculate the gross (total) pay of ten employees each given the hours worked
and the rate of pay. Write a C++ program to solve this problem.

2. The Floral Company sells to wholesale and retail buyers. The wholesale buyer
needs a resale number in order to buy at no tax and to receive discounts. The
retail buyer pays 6% tax. These are the discounts to the wholesale buyer:
Amount < $100 Discount = 2%
Amount >= $100 AND < $500 Discount = 5%
Amount >= $500 Discount = 10%
Given an amount of purchase, write a C++ program to show how much will
the customer owe (pay) the company?

3. Develop a C++ program to calculate the water bill given the cubic feet of
water used for a Water Company, which charges the homeowner one of the
following:
a. A flat rate of $15.00 for usage up to and including 1000 cubic feet.
b. $0.0175 per cubic foot for usage over 1000 cubic feet and up to and
including 2000 cubic feet.
c. $0.02 per cubic foot for usage over 2000 cubic feet and up to and
including 3000 cubic feet.
d. A flat rate of $70.00 for usage over 3000 cubic feet.

4. An admission charge for a Theater varies according to the age of the person.
Develop a C++ program to print the ticket charge given the age of the person.
The charges are as follows:
a. Over 55: $10.00
b. 21–54: $15.00
c. 13–20: $10.00
d. 3–12: $5.00
e. Under 3: Free

© Asst. Lect. Wasseem Nahi Ibrahem Page 3


Algorithms Design and Programming Techniques Tutorial 4

5. A hotel has a pricing policy as follows:


a. 2 people: $85
b. 3 people: $90
c. 4 people: $95
d. Additional people: $6 per person
If the customer is staying on company business, there is a 20% discount. If the
customer is over 60 years of age, there is a 15% discount. A customer does not
receive both discounts. Given the above data, write a C++ program to print the
cost of the room.

6. A student wants to know his grade point average (GPA) for the semester. The
grades are given in letter grades with numeric equivalents. Write a C++
program to calculate a grade point average given the letter grades. (Remember,
the grade point average is figured per unit of credit, not per course.) An A =
4.0, B = 3.0, C = 2.0, D = 1.0, F = 0.0. Test the solution with the following
data:
History B 3 units
Economics A 3 units
PE A 1 unit
Chemistry C 4 units
Art B 3 units

7. A company that issues check-cashing cards uses an algorithm to create card


numbers. The algorithm adds the digits of a four-digit number, and then adds a
fifth digit of 0 or 1 to make the sum of the digits even. The last digit in the
number is called the check digit. Develop a C++ program that accepts a four-
digit number into one variable, adds the check digit, and prints the original
number and the new number. Test your solution with the following data:
Original number = 4737 (47371) and 4631 (46310).

8. Mary Smith is looking for the bank that will give the most return on her money
over the next five years. She has $2,000 to put into a savings account. The
standard equation to calculate principal plus interest at the end of a period of
time is:

© Asst. Lect. Wasseem Nahi Ibrahem Page 4


Algorithms Design and Programming Techniques Tutorial 4

𝐴𝑚𝑜𝑢𝑛𝑡 = 𝑃 × (1 + 𝐼/𝑀)(𝑁∗ 𝑀)
where P: Principal (amount of money to invest, in this case $2,000)
I: Interest (percentage rate the bank pays to the investor)
N: Number of Years (time for which the principal is invested)
M: Compound Interval (the number of times per year the interest
is calculated and added to the principal)
Write a C++ program to solve this problem.

9. Develop a C++ program to tell the user whether a number is a palindrome. (A


palindrome is a number that is the same written both forward and backward,
such as 81318.)

10.Mary Smith, a student, has borrowed $3,000 to help pay her college expenses.
After setting up a budget, $85 was the maximum monthly payment she could
afford to make on the loan. Develop a C++ program to calculate and print the
interest, the principal, and the balance on the loan per month. Other
information she would like to know is the number of years and months it will
take to pay the loan back and the total interest she will pay during that period.
The interest rate is 1% per month on the unpaid balance. Keep in mind these
formulas:
interest normal = balance * interest rate
payment = balance - interest
new balance = balance - payment

© Asst. Lect. Wasseem Nahi Ibrahem Page 5

You might also like