//========================================================================= #define PROGRAMMER "SOLUTIONS, Nofrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2004) #define TERM "Spring" #define SECTION (0) #define ASSIGNMENT "HW5A" #define REVISION (0) #define TITLE "How much did that REALLY cost?" #define SUBTITLE "Last modified on 12 MAR 04" #define EMAIL "ece1021@eas.uccs.edu" #define FILENAME "05a0soln.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); //== MAIN FUNCTION ======================================================== int main(void) { double price; double apr; double min_percentage, abs_minimum_pmt; int month; double old_balance, new_balance, interest, payment, minimum_due; double TotalFinanceCost, TotalCost; PrintHeader(); BLANKLINE; //----------------------------------------------------------------- // 1. 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); apr /= 100.0; printf("Enter the Minimum Payment Percentage (in %): "); scanf("%lf", &min_percentage); min_percentage /= 100.0; printf("Enter the Minimum Payment (in $): "); scanf("%lf", &abs_minimum_pmt); //----------------------------------------------------------------- // 2. TASK: Generate a Month-by-Month Table until item is paid off. //----------------------------------------------------------------- // 2.1. TASK: Output Table Header (first row of table) 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"); // 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; interest = old_balance * apr * (1.0/12.0); payment = minimum_due; new_balance = old_balance + interest - payment; // 2.4.1.6. TASK: Set Minimum Payment if(new_balance > abs_minimum_pmt) { minimum_due = new_balance * min_percentage; if(minimum_due < abs_minimum_pmt) minimum_due = abs_minimum_pmt; } else minimum_due = new_balance; // 2.4.2. TASK: Update running totals TotalFinanceCost += interest; TotalCost += payment; // 2.4.3. TASK: Print out activity for that month printf(" %4i ", month); printf(" %8.2f", old_balance); printf(" %8.2f", interest); printf(" %8.2f", payment); printf(" %8.2f", new_balance); printf(" %8.2f", minimum_due); printf("\n"); } //----------------------------------------------------------------- // 3. 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", month); 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(0); } //========================================================================= // PRIMARY FUNCTIONS (functions called directly by main() ) //========================================================================= //== FUNCTION PROTOTYPES (for Support Functions) ========================== int printc(char c, int n); //== 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; } //========================================================================= // SUPPORT FUNCTIONS (functions not called directly by main() ) //========================================================================= int printc(char c, int n) { while(0 < n--) printf("%c", c); return(n); }