ECE-1021
HOMEWORK #5A Solution Notes
(Last Mod: 27 November 2010 21:38:38 )
Problem
The first step in the problem solving process is to understand the problem:
If an item is purchased with a credit card and paid back making only the minimum required payment each month, how much will that item end up costing the purchaser once it is finally paid off?
We are asked to prepare a table of the monthly transactions that would result under these conditions and to prepare a summary of the overall impact of using a credit card in this manner.
Inputs (Specifically stated)
"One of the first tasks your program should perform is to ask the user for the relevant information, namely the purchase price, interest rate, minimum payment percentage, and minimum absolute payment."
Outputs (Specifically stated)
1) Until the item is paid off:
"Your program should print a month-by-month table that gives the month number and the basic information that would be on the statement for that month, namely the old balance, the finance charges, the payment credited, the new balance, and the minimum payment due."
On the first statement, our "old balance" will be the purchase price and we will have made no payment so the payment credited will be zero.
We need to know how to calculate each of the items in the table:
Month: Just the number of the previous month plus one. We can the first month "month 0" or "month 1", it doesn't really matter. But, in our summary, we probably want the length of time required to pay off the item to reflect the number of months in that we actually made payments - although the difference is pretty minor if we choose otherwise.
Old Balance: Just the "New Balance" from the prior month. The "Old Balance" for the first month is simply the purchase price.
old_balance = new_balance; // using new_balance from previous month
Finance Charge: The "Finance Charge" for a given month is "one month's interest on [the old] balance." Using the basic relationship that interest = principle * rate * time, this becomes:
interest = old_balance * apr * (1.0 / 12.0);
Payment: Simply the "Minimum Due" from the prior month. The "Payment" for the first month is zero since we make no payments until after we receive a statement telling us how much to pay.
payment = minimum_due; // using minimum_due from previous month
New Balance: This is specifically spelled out in the problem assignment: "Therefore the new balance (upon which the minimum payment is calculated) is the old balance, plus one month's interest on that balance, minus the previous month's payment."
new_balance = old_balance + interest - payment;
Minimum Due: This is explicitly spelled out in the problem assignment: "Your minimum payment is either a specific fraction of the new balance (typically between 2% and 3%) but not less than some absolute minimum (typically between $10 and $20) or the remaining balance if it is less than the absolute minimum."
if(new_balance > abs_minimum_pmt)
{
minimum_due = new_balance * fraction;
if(minimum_due < abs_minimum_pmt)
minimum_due = abs_minimum_pmt;
}
else
{
minimum_due = new_balance;
}
The output for this section should therefore look something like:
Month |
Old Balance |
Fin. Charge |
Payment |
New Balance |
Minimum Due |
0 |
$ 2500.00 |
$ 39.38 |
$ 0.00 |
$ 2539.38 |
$ 50.79 |
1 |
$ 2539.38 |
$ 40.00 |
$ 50.79 |
$ 2528.59 |
$ 50.57 |
2 |
$ 2528.59 |
$ 39.83 |
$ 50.57 |
$ 2517.85 |
$ 50.36 |
2) After the item is paid off:
"Once the item is paid off, you should print a summary that gives the following information:
The original purchase amount, the total number of months required to pay of the purchase, the total amount of the finance charges, and the total cost of the purchased item."
The output for this section should therefore look something like (using random values):
Number of months to pay off: .......... 37
Original Purchase Price: ....... $ 2500.00
Total Finance Charges: ......... $ 300.00
Total Cost of Item: ............ $ 2800.00
The number of months required to pay off the item comes directly from the number of the last month printed in the table. The original purchase price was entered by the user. The Total Finance Charge and the Total Cost can simply be a running total of the corresponding quantity for each month.
Top-Down Pseudocode Development
Top Level:
TASK: Get Input Data from User.
TASK: Generate a Month-by-Month Table until item is paid off.
TASK: Generate a Final Summary after the item is paid off.
Next Level of Detail:
TASK: Get Input Data from User.
GET: purchase price (purchase)
GET: interest rate (apr)
GET: minimum payment percentage (fraction)
GET: minimum absolute payment (abs_minimum_pmt)
TASK: Generate a Month-by-Month Table until item is paid off.
TASK: Output Table Header (first row of table)
TASK: Initialize variables for first month
TASK: Initialize variable for running totals
WHILE (balance > 0)
TASK: Update variables for that month's activity
TASK: Update running totals
TASK: Print out activity for that month
TASK: Generate a Final Summary after the item is paid off.
OUT: Number of months
OUT: Purchase Price
OUT: Total Finance Charges
OUT: Total Cost
At this point, we can see exactly how to implement all of the tasks provided we are willing to look back at the discussions leading up to this point. The pseudocode that is turned in for grading needs to be able to stand on its own showing enough detail that it convinces the grader that an adequate understanding of the problem exists and that a viable solution is represented. So the pseudocode turned in should have at least one more level of detail:
Submitted Level of Detail:
TASK: Get Input Data from User.
GET: purchase price (purchase)
GET: interest rate (apr)
SET: apr = apr / 100.0
GET: minimum payment percentage (fraction)
SET: fraction = fraction / 100.0
GET: minimum absolute payment (abs_minimum_pmt)
TASK: Generate a Month-by-Month Table until item is paid off.
TASK: Output Table Header (first row of table)
TASK: Initialize variables for first month
SET: month = -1 (i.e., month prior to "month 0")
SET: new_balance = 0.0
SET: minimum_due = 0.0
TASK: Initialize variable for running totals
SET: TotalFinanceCost = 0.0
SET: TotalCost = 0.0
WHILE (balance > 0)
TASK: Update variables for that month's activity
SET: month++
SET: old_balance = new_balance
SET: interest = old_balance * apr * (1.0/12.0)
SET: payment = minimum_due
SET: new_balance = old_balance + interest - payment
TASK: Set Minimum Payment
IF: (new_balance > abs_minimum_pmt)
SET: minimum_due = new_balance * fraction
IF: (minimum_due < abs_minimum_pmt)
SET: minimum_due = abs_minimum_pmt
ELSE:
SET: minimum_due = new_balance
TASK: Update running totals
TotalFinanceCost += interest
TotalCost += payment
TASK: Print out activity for that month
OUT: month
OUT: old_balance
OUT: interest
OUT: payment
OUT: new_payment
OUT: minimum_due
TASK: Generate a Final Summary after the item is paid off.
OUT: Number of months (month)
OUT: Purchase Price (purchase)
OUT: Total Finance Charges (TotalFinanceCost)
OUT: Total Cost (TotalCost)
Planned Input/Output Format:
Enter
Original Purchase Price ($): ...... 2500
Enter
Annual Percentage Rate (%): ....... 18.9
Enter
Minimum Payment Fraction (%): ..... 2
Enter
Minimum Payment Amount ($): ....... 10
MONTH-BY-MONTH SUMMARY
Month |
Old Balance |
Fin. Charge |
Payment |
New Balance |
Minimum Due |
0 |
$ 2500.00 |
$ 39.38 |
$ 0.00 |
$ 2539.38 |
$ 50.79 |
1 |
$ 2539.38 |
$ 40.00 |
$ 50.79 |
$ 2528.59 |
$ 50.57 |
2 |
$ 2528.59 |
$ 39.83 |
$ 50.57 |
$ 2517.85 |
$ 50.36 |
FINAL SUMMARY
Number of months to pay off: .......... 37
Original Purchase Price: ....... $ 2500.00
Total Finance Charges: ......... $ 300.00
Total Cost of Item: ............ $ 2800.00
I strongly recommend including a planned I/O format section with your pseudocode. It gets you to consider such items as how you will word your prompts and whether you want the user to enter data, such as the interest rate, as a percentage (18.9) or as a fraction (0.189). In general, choose the one that is the most natural and convenient for the user and then perform any necessary manipulation in your code. Having a planned I/O format also makes it so that you don't have to be as explicit in your pseudocode regarding your GET and OUT statements since the reader can look at your I/O format to see what the prompts, headers, delimiters, or format specifications are likely going to be.