CS 1320 (Principles of Algorithm Design I):
Homework #5
- Assigned:
- February 28, 2000.
- Due:
- March 13, 2000, at start of class.
(Revised March 3: now due March 15, 2000, at start
of class.)
- Credit:
- 30 points (10 points per problem).
Instructions
Write a C++ program to solve each of the problems below.
Your code should begin with comments that describe
what the program is supposed to do, in terms of its inputs
and outputs.
See the sample programs
for examples.
Problems
Problem: Number sequence
A sequence begins 0, 1, 1, 2, 3, 5, 8, 13. Write a C++ program
producing the first thirty entries in this sequence. (You will
need to figure out the rule for producing the next entry from the
preceding entries.)
Problem: Exponentiation
Write a C++ program to compute the exponentiation function
BE
(B to the Eth power),
where B is any non-zero number (not necessarily an integer)
and E is an integer.
Remember that B0 is 1 for all B,
and B-E is 1/BE.
Examples:
- Input: 3.0 0
Output: 1
- Input: 2 3
Output: 8
- Input: 2 -2
Output: 0.25
- Input: 2.5 2
Output: 6.25
You can assume that B is non-zero.
Your program must include:
- A function to compute BE. This function
should take two input parameters, a double (B)
and an int (E), and return a double
representing BE.
Do not use functions from the C++ math library.
- A main program that prompts the user for B and E,
calls your function to compute BE, and
prints the result.
Remember to include, for each function you write,
comments describing its precondition(s) and postcondition(s).
Credit-card billing, revisited
Acme Bank's terms for its AcmeCard credit card were
described in
Homework #4.
Write a C++ program that computes information for an AcmeCard bill
for several months. Your program should first read in an interest
rate (as an annual percentage) and a starting balance, and then
repeatedly read in a last month's payment and current charges,
continuing until "end of file" (in Unix environments, signalled
by the user pressing control-D).
All amounts are to be entered in
floating-point form (e.g., "12.34" for $12.34). As each month's
information is entered, the program should
print out interest due, new balance, and minimum payment.
After "end of file" is detected on input, it should print out
the total interest charged.
All amounts should be printed out in dollars-and-cents form,
e.g. "$12.34".
Here is a sample execution of the program:
$ credit_card
Enter annual interest rate, as a percentage:
12
Enter initial balance, in dollars and cents:
100.00
Enter last payment and current month's charges, in dollars and cents (control-D to end):
100.00 200.00
Interest due = $0.00
New balance = $200.00
Minimum payment = $10.00
Enter last payment and current month's charges, in dollars and cents (control-D to end):
100.00 100.00
Interest due = $1.50
New balance = $201.50
Minimum payment = $10.08
Enter last payment and current month's charges, in dollars and cents (control-D to end):
101.50 100.00
Interest due = $1.50
New balance = $201.50
Minimum payment = $10.08
Enter last payment and current month's charges, in dollars and cents (control-D to end):
[ user presses control-D ]
Total interest = $3.00
You do not need to worry about rounding.
Use as many or as few functions as you choose.
What to turn in
Submit your source code as described in the
guidelines for programming assignments.
For this assignment:
- Use a subject header of "cs1320 homework 5".
- Submit three source files, one for each problem.
Please choose names for the files that indicate
which program solves which problem.
(For example,
you could use sequence.cc,
exponent.cc, and credit_card.cc.)