60-140
Introduction to Algorithms and Programming I
Dr. Christie Ezeife
Lab. Exercises #5 Solution (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.
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
Solution
to Que 1.
Script started
on Fri Sep 03 00:02:35 2010
sol:~/bk2010/programs>cat
lab5slnq1.c
#include <stdio.h>
int main (void){
/* 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.
*/
int
days;
float
miles;
float
plan1, plan2;
printf("Enter Number of Days : ");
scanf("%d",&days);
printf("Enter Number of Miles: ");
scanf("%f",&miles);
plan1 = days *
40;
plan2 = days * 30+ 0.15 * miles;
if (plan1 ==
plan2)
printf("Plan 1 (with
cost of $%0.2f) and Plan 2 (with cost of $%0.2f) are the Same cost \n",
plan1, plan2);
else if (plan1
< plan2)
printf("Plan 1 (with
cost of $%0.2f) is Cheaper than Plan 2 (with cost of $%0.2f) \n", plan1,
plan2);
else
printf("Plan 2 (with
cost of $%0.2f) is Cheaper than Plan 1 (with cost of $%
0.2f)\n",
plan2, plan1);
return 0;
}
sol:~/bk2010/programs>cc
lab5slnq1.c
sol:~/bk2010/programs>a.out
Enter Number of
Days : 7
Enter Number of
Miles: 1000
Plan 1 (with
cost of $280.00) is Cheaper than Plan 2 (with cost of $360.00)
sol:~/bk2010/programs>!a
a.out
Enter Number of
Days : 3
Enter Number of
Miles: 3400
Plan 1 (with
cost of $120.00) is Cheaper than Plan 2 (with cost of $600.00)
sol:~/bk2010/programs>!a
a.out
Enter Number of
Days : 20
Enter Number of
Miles: 600
Plan 2 (with
cost of $690.00) is Cheaper than Plan 1 (with cost of $800.00)
sol:~/bk2010/programs>exit
exit
script
done on Fri Sep 03 00:03:29 2010
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 tax 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.
Solution to Que 2:
Script started on Fri Sep 03 00:33:50 2010
sol:~/bk2010/programs>cat lab5slnq2.c
#include <stdio.h>
/*
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.
*/
float PositiveTop (float);
float PositiveBottom (float);
float NegativeBottom (float);
float NegativeTop (float);
float SwitchCase (float);
int main (void){
float income;
scanf("%f",&income);
printf("PositiveTop = %.2f\n",PositiveTop(income));
printf("PositiveBottom = %.2f\n",PositiveBottom(income));
printf("NegativeBottom = %.2f\n",NegativeBottom(income));
printf("NegativeTop = %.2f\n",NegativeTop(income));
printf("SwitchCase = %.2f\n",SwitchCase(income));
return 0;
}
float PositiveTop (float income){
if (income>=200000)
return income * .5;
else
if (income>=150000)
return income * .36;
else
if (income>=100000)
return income * .3;
else
if (income>=50000)
return income * .2;
else
if (income>=20000)
return income * .1;
else
return 0;
}
float PositiveBottom (float income){
if (income<20000)
return 0;
else
if (income<50000)
return income * .1;
else
if (income<100000)
return income * .2;
else
if (income<150000)
return income * .3;
else
if (income<200000)
return income * .36;
else
return income * .5;
}
float NegativeBottom (float income){
if (income>=20000)
if (income>=50000)
if (income>=100000)
if (income>=150000)
if (income>=200000)
return income * .5;
else
return income * .36;
else
return income * .3;
else
return income * .2;
else
return income * .1;
else
return 0;
}
float NegativeTop (float income){
if (income<200000)
if (income<150000)
if (income<100000)
if (income<50000)
if (income<20000)
return 0;
else
return income * .1;
else
return income * .2;
else
return income * .3;
else
return income * .36;
else
return income *.5;
}
float SwitchCase (float income){
/* since switch_case instruction is good for exact scalar values
like integers and characters and not float, we first place these
ranges in integer categories using an if instruction. */
int incomebracket; /* local variable for category of income */
if (income>=200000)
incomebracket = 1;
else
if (income>=150000)
incomebracket = 2;
else
if (income>=100000)
incomebracket = 3;
else
if (income>=50000)
incomebracket = 4;
else
if (income>=20000)
incomebracket = 5;
else
incomebracket = 6;
/* Now, use the integer incomebracket to make the correct selection */
switch (incomebracket){
case 1 : return income * 0.5; break;
case 2 : return income * 0.36; break;
case 3 : return income * 0.30; break;
case 4 : return income * 0.20; break;
case 5 : return income * 0.10; break;
case 6 : return 0; break;
}
}
sol:~/bk2010/programs>cc lab5slnq2.c
sol:~/bk2010/programs>a.out
210000
PositiveTop = 105000.00
PositiveBottom = 105000.00
NegativeBottom = 105000.00
NegativeTop = 105000.00
SwitchCase = 105000.00
sol:~/bk2010/programs>!a
a.out
150000
PositiveTop = 54000.00
PositiveBottom = 54000.00
NegativeBottom = 54000.00
NegativeTop = 54000.00
SwitchCase = 54000.00
sol:~/bk2010/programs>!a
a.out
120000
PositiveTop = 36000.00
PositiveBottom = 36000.00
NegativeBottom = 36000.00
NegativeTop = 36000.00
SwitchCase = 36000.00
sol:~/bk2010/programs>!a
a.out
66000
PositiveTop = 13200.00
PositiveBottom = 13200.00
NegativeBottom = 13200.00
NegativeTop = 13200.00
SwitchCase = 13200.00
sol:~/bk2010/programs>!a
a.out
21000
PositiveTop = 2100.00
PositiveBottom = 2100.00
NegativeBottom = 2100.00
NegativeTop = 2100.00
SwitchCase = 2100.00
sol:~/bk2010/programs>!a
a.out
12000.56
PositiveTop = 0.00
PositiveBottom = 0.00
NegativeBottom = 0.00
NegativeTop = 0.00
SwitchCase = 0.00
sol:~/bk2010/programs>exit
exit
script done on Fri Sep 03 00:36:05 2010
Que. 3. Begin to prepare for midterm test.