60-140
Introduction to Algorithms and Programming I
Dr. Christie Ezeife
Lab. Exercises #5
(Lab Date: Week 7 of Classes)
Objectives
are to:
1.
Practise
on the use of decision instructions like if and switch-case
instructions in problem solving as taught in chapter 6 of book.
2.
Begin
to prepare for midterm test by revising how to write programs with valid C
instructions, data types and functions discussed in chapters 1 to 6 of text
book.
Que. 1. Do question 3 of Section 6.4 of
book.
A car rental shop has two plans. In the first plan, you can rent a car for $40 per day with unlimited mileage. In the second plan, you can rent a car for $30 per day with an extra charge of 15c per mile. Write a C program to determine which is the better plan for a customer to purchase, given the number of days and the total number of miles she plans to drive the car.
Hints on how to solve
i.
You
can use the following program template for your solution.
#include <stdio.h>
void main(void)
{
/*
Declare input data variables */
-------------------------------------------
-------------------------------------------
-------------------------------------------
-------------------------------------------
/* Write the executable instructions next */
-------------------------------------------
-------------------------------------------
:
-------------------------------------------
-------------------------------------------
} /* end of program */
ii. Compile and run your program with the
following sample data and show your work in a script file.
7
days and 1000 miles
3
days and 3400 miles
Que. 2. Write a C
program to output the income tax paid by a person given their annual
income. It is stated that with an
income of $200,000.00 or more, a person pays 50% of their income as tax, but
pays 36% of their income when it is $150,000.00 to $199,999.99. The income tax paid is 30% of the income
when the income is $100,000.00 to $149,999.99. The income tax paid is 20% of the income
with an income of $50,000.00 to $99,999.99. The tax on income of $20,000.00 to
$49,999.99 is 10% of the income, while the tax on income of less than
$20,000.00 is $0.
(a) Give 4 different solutions using nested
if instruction.
(a)
Set up 4 flowcharts corresponding to (a).
(b)
Give a solution using switch-case instruction.
Hints on how to solve
i.
To
solve a decision problem with multiple conditions like the above, set up a condition_action table like the following:
Income in $ |
Tax in $ |
>= 200,000.00 |
50% * income |
150,000.00 to 199,999.99 |
36% * income |
100,000.00 to 149,999.99 |
30% * income |
50,000.00 to 99,999.99 |
20% * income |
20,000.00 to 49,999.99 |
10% * income |
< 20,000.00 |
0 |
The
correct grade can be obtained using a nested if instruction that can be
written in four different ways corresponding to:
a. Positive nested if logic
starting from top to bottom of the table.
b. Positive nested if logic
starting from bottom to top of the table.
c. Negative nested if logic
starting from top to bottom of the table.
d. Negative nested if logic
starting from bottom to top of the table.
ii.
Now
answer questions 2(a) of the lab #5 exercise by writing the 4 programs and
testing with sample data (may use the sample below). Show the lab instructor/GA
in a script file.
210000
150000
120000
66000
21000
12000
iii.
Complete
questions 2(b) and 2(c) of lab exercise #5.
Que. 3. Begin to prepare for midterm test.