//========================================================================= #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Spring" #define SECTION (0) #define ASSIGNMENT "HW5B" #define REVISION (0) #define TITLE "How much did that REALLY cost? (modularized)" #define SUBTITLE "Last modified on 12 MAR 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "05b0soln.c" //========================================================================= // NOTE: This file was prepared using the new template.c //========================================================================= // PROBLEM //========================================================================= // Generate an amortization table for a credit card on which a single // purchase is made and which is paid back according to the minimum payment // terms of the card. //========================================================================= // PSEUDOCODE (top levels only) //========================================================================= // 1. TASK: Get Input Data from User. // // 2. TASK: Generate a Month-by-Month Table until item is paid off. // 2.1. TASK: Output Table Header (first row of table) // 2.2. TASK: Initialize variables for first month // 2.3. TASK: Initialize variable for running totals // 2.4. WHILE (balance > 0) // 2.4.1. TASK: Update variables for that month's activity // 2.4.2. TASK: Update running totals // 2.4.3. TASK: Print out activity for that month // // 3. TASK: Generate a Final Summary after the item is paid off. //========================================================================= // DEVIATIONS FROM SUBMITTED PSEUDOCODE //========================================================================= // // Change: (See pseudocode task 2.2.2) // Was SET: new_balance = 0.0 // Now SET: new_balance = purchase // // Reason For Change: // Careless mistake in typing pseudocode. // // Change: (See pseudocode task 2.4) // The main loop test was (balance > 0) // It has been changed to (balance > 0.005) // // Reason For Change: // The account is considered paid off once less than one cent is owed. // Otherwise, the program keeps expecting payments until the balance // finally underflows (i.e., gets smaller than ~1e-308). //========================================================================= // WAIVED COMPILER WARNINGS //========================================================================= // // Linker Warning: No module definition file specified: using defaults // Reason for Waiver: Can't suppress. Does not have adverse impact. //========================================================================= // CODE SECTION //========================================================================= //== INCLUDE FILES ======================================================== #include // printf() //== MACRO DEFINITIONS ==================================================== #define FALSE (0) #define TRUE (!FALSE) #define BLANKLINE printf("\n") //== FUNCTION PROTOTYPES (for Primary Functions) ========================== void PrintHeader(void); void GetInputData(double *price, double *apr, double *min_percentage, double *abs_minimum_pmt); int MonthByMonthTable(double price, double apr, double min_percentage, double abs_minimum_pmt, double *TotalFinanceCost, double *TotalCost); void FinalSummary(double price, int months, double TotalFinanceCost, double TotalCost); //== MAIN FUNCTION ======================================================== void GetInputData(double *price, double *apr, double *fraction, double *minimum); int MonthByMonthTable(double price, double apr, double fraction, double minimum, double *TotalFinance, double *TotalCost); void FinalSummary(double price, int months, double TotalFinance, double TotalCost); int main(void) { int months; double purchase; double apr, fraction, abs_minimum_pmt; double TotalFinance, TotalCost; PrintHeader(); BLANKLINE; GetInputData(&purchase, &apr, &fraction, &abs_minimum_pmt); months = MonthByMonthTable(purchase, apr, fraction, abs_minimum_pmt, &TotalFinance, &TotalCost); FinalSummary(purchase, months, TotalFinance, TotalCost); return(0); } //========================================================================= // PRIMARY FUNCTIONS (functions called directly by main() ) //========================================================================= //== FUNCTION PROTOTYPES (for Support Functions) ========================== int printc(char c, int n); void PrintMonthlyHeader(void); void PrintMonthlyInfo(int month, double old_bal, double interest, double pmt, double new_bal, double min_due); double MonthlyInterest(double balance, double apr); double MinDue(double balance, double percentage, double abs_min); double MontlyInterest(double balance, double apr); //== PRIMARY FUNCTIONS ==================================================== void PrintHeader(void) { printc('=', 79); printf("\n"); printf("Course....... %s-%i (%s %i)\n", COURSE, SECTION, TERM, YEAR); printf("Programmer... %s (%s)\n", PROGRAMMER, PROG_CODE); printf("Assignment... %s (Rev %i) (Source Code in %s)\n", ASSIGNMENT, REVISION, FILENAME); printf("Description.. %s\n", TITLE); printf(" %s\n", SUBTITLE); printc('=', 79); printf("\n"); return; } void GetInputData(double *price, double *apr, double *min_percentage, double *abs_minimum_pmt) { //----------------------------------------------------------------- // TASK: Get Input Data from User. //----------------------------------------------------------------- printf("Enter the purchase price (in $): "); scanf("%lf", price); printf("Enter the Annual Percentage Rage (in %): "); scanf("%lf", apr); printf("Enter the Minimum Payment Percentage (in %): "); scanf("%lf", min_percentage); printf("Enter the Minimum Payment (in $): "); scanf("%lf", abs_minimum_pmt); return; } int MonthByMonthTable(double price, double apr, double min_percentage, double abs_minimum_pmt, double *TotalFinanceCost, double *TotalCost) { //----------------------------------------------------------------- // TASK: Generate a Month-by-Month Table until item is paid off. //----------------------------------------------------------------- int month; double interest, payment, minimum_due; double old_balance, new_balance; // 2.1. TASK: Output Table Header (first row of table) PrintMonthlyHeader(); // 2.2. TASK: Initialize variables for first month month = -1; new_balance = price; minimum_due = 0.0; // 2.3. TASK: Initialize variable for running totals *TotalFinanceCost = 0.0; *TotalCost = 0.0; while (new_balance > 0.005) { // 2.4.1. TASK: Update variables for that month's activity month++; old_balance = new_balance; // new_balance from previous month payment = minimum_due; // minimum_due from previous month interest = MonthlyInterest(old_balance, apr); new_balance = old_balance + interest - payment; // 2.4.1.6. TASK: Set Minimum Payment minimum_due = MinDue(new_balance, min_percentage, abs_minimum_pmt); // 2.4.2. TASK: Update running totals *TotalFinanceCost += interest; *TotalCost += payment; // 2.4.3. TASK: Print out activity for that month PrintMonthlyInfo(month, old_balance, interest, payment, new_balance, minimum_due); } return(month); } void FinalSummary(double price, int months, double TotalFinanceCost, double TotalCost) { //----------------------------------------------------------------- // TASK: Generate a Final Summary after the item is paid off. //----------------------------------------------------------------- BLANKLINE; BLANKLINE; printf("SUMMARY\n"); BLANKLINE; printf("Number of Months required to pay off: %5i\n", months); printf("Original Purchase Price: .... $%9.2f\n", price); printf("Total Finance Charges: ...... + %9.2f\n", TotalFinanceCost); printf("Total Cost of Item: ......... = $%9.2f\n", TotalCost); return; } //========================================================================= // SUPPORT FUNCTIONS (functions not called directly by main() ) //========================================================================= int printc(char c, int n) { while(0 < n--) printf("%c", c); return(n); } // Support Functions for TASK #2 void PrintMonthlyHeader(void) { BLANKLINE; BLANKLINE; printf("MONTH BY MONTH ACTIVITY\n"); BLANKLINE; printf(" MONTH "); printf(" OLD BAL "); printf(" FIN CHG "); printf(" PAYMENT "); printf(" NEW BAL "); printf(" MIN DUE "); printf("\n"); return; } void PrintMonthlyInfo(int month, double old_bal, double interest, double pmt, double new_bal, double min_due) { printf(" %4i ", month); printf(" %8.2f", old_bal); printf(" %8.2f", interest); printf(" %8.2f", pmt); printf(" %8.2f", new_bal); printf(" %8.2f", min_due); printf("\n"); return; } double MonthlyInterest(double balance, double apr) { return(balance * ((0.01 * apr)/12.0)); // apr -> monthly fraction } double MinDue(double balance, double percentage, double abs_min) { double minimum_due; if(balance > abs_min) { minimum_due = balance * (0.01 * percentage); if(minimum_due < abs_min) minimum_due = abs_min; } else minimum_due = balance; return(minimum_due); }