PAGETURNER
SOMECLEAN
ENCORE1
CLEAN
ENCORE
BOX
SOLUTION
GO
Let us look at this program together!
Step 1 is to establish a clear goal.
The goal is to determine the postage cost in cents.
The postage cost is determined based on the value
of the mail_code i.e. First Class, 2nd class, etc.
---------------------------------------------------
For the purpose of this program, the mail_code
can have 3 different values:
1 - the domestic first class rate
2 - domestic card rate
other - not 1 or 2, i.e., the default, invalid mail_code
Based on the program stipulation, which control construct would
best handle the logic for the 3 different cases?
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
There are 2 constructs for decision making: if and switch
Please enter switch or switch()
Please review Topic 5: The switch Statement
FORGET1
The correct choice is switch with the above syntax:
switch () {
case 1: .....
: ;
case 2: .....
: ;
default: .....
: ;
}
Assume that the variable local_cost holds the
postage cost. If the domestic first class rate
is $0.22, the domestic card rate is $0.15, and
the number of identical items is n, then which
of the following would be possible statements?
a local_cost = .22 * n;
b local_cost = .15 * n;
c return(-1);
d local_cost = .22 * n + .15 * n;
e a and b
f a, b, and c
g b, c, and d
h a and d
Answer here:
That is correct!
You've got it on the 2nd try.
You've got it finally!
Only items of one type are requested.
Three different cases are to be handled.
FORGET2
The correct choice is f ! This has the effect above
after the break(s) and the final return() are added to
exit after executing the statements for the case:
cost_get(n, mail_code)
int n, mail_code;
{
float local_cost;
switch ( mail_code) {
case 1: /* DOMESTIC FIRST CLASS */
local_cost = .22 * n; break;
case 2: /* DOMESTIC CARD RATE */
local_cost = .15 * n; break;
default: /* INVALID MAIL CODE */
return(-1);
}
printf("Postage Cost = %10.2f\n", local_cost);
return(local_cost * 100); /* in cents */
}