//========================================================================= #define PROGRAMMER "SOLUTIONS, NoFrills" #define PROG_CODE "soln" #define COURSE "ECE-1021" #define YEAR (2003) #define TERM "Fall" #define SECTION (0) #define ASSIGNMENT "HW #1A" #define REVISION (0) #define TITLE "How much did that REALLY cost" #define SUBTITLE "" #define EMAIL "wbahn@eas.uccs.edu" #define FILENAME "01a0soln.c" //========================================================================= // PROBLEM: // // Given the Terms for a typical credit card, determine the total cost // of an item purchased on credit and how long it will take to pay it off // if only the minimum payment due is made each month // // PSEUDOCODE: // // The full pseudocode is in file 01A0SOLN.TXT // // A high level abstract is given below: // // 1) TASK: Get necessary data from user. // 2) TASK: Generate Monthly Statements until paid off // 2.1) TASK: Print Column Headings. (all on same line) // 2.2) TASK: Initialize necessary variables. // 2.3) WHILE: (balance > 0) // 2.3.1) TASK: Calculate Monthly Statement values // 2.3.1.6) TASK: Compute Minimum Payment for next month // 2.3.2) TASK: Print Monthly Statement values. // 2.3.3) TASK: Update necessary variables // 3) TASK: Print Summary Information // //== SUMMARY OF DEVIATIONS FROM PSEUDOCODE ================================ // // The computed Finance Charge is rounded to the nearest penny. // // 1) TASK: Compute Finance Charge (f) // 2) TASK: Round f to the nearest penny // 2.1) SET: f = (int(100*f + 0.5) )/100 (use type casting in C) //== INCLUDE FILES ======================================================== #include // printf() //== FUNCTION PROTOTYPES ================================================== void PrintHeader(void); //== MAIN FUNCTION ======================================================== int main(void) { int Month; double Price, Rate, FractionalMin, AbsoluteMin; double Opening, Closing, FinCharge, PmtRec, MinDue, TotalCost; PrintHeader(); printf("\n"); // Print a blank line // 1) TASK: Get necessary data from user. printf("Enter the purchase price: "); scanf("%lf", &Price); printf("Enter the annual interest rate (as a percentage): "); scanf("%lf", &Rate); Rate /= 100.0; // Adjust the entered percentage to a decimal value printf("Enter the minimum monthly fraction (as a percentage): "); scanf("%lf", &FractionalMin); FractionalMin /= 100.0; // Adjust to a decimal value printf("Enter the minimum monthly payment: "); scanf("%lf", &AbsoluteMin); // 2) TASK: Generate Monthly Statements until paid off // 2.1) TASK: Print Column Headings. (all on same line) printf("\n"); printf("Amortization Schedule assuming Minimum Payments\n"); printf("\n"); printf("Month | Opening | Pmt Rec | Finance | Closing | Min Due\n"); // 2.2) TASK: Initialize necessary variables. Month = 0; Closing = Price; MinDue = 0.0; TotalCost = 0.0; // 2.3) WHILE: (balance > 0) while(Closing > 0.0) { // 2.3.1) TASK: Calculate Monthly Statement values Month++; Opening = Closing; FinCharge = Opening * Rate / 12.0; FinCharge = (double)((long int)((FinCharge * 100.0) + 0.5) )/100; PmtRec = MinDue; Closing = Opening + FinCharge - PmtRec; // 2.3.1.6) TASK: Compute Minimum Payment for next month MinDue = Closing*FractionalMin; if(MinDue < AbsoluteMin) MinDue = AbsoluteMin; if(MinDue > Closing) MinDue = Closing; // 2.3.2) TASK: Print Monthly Statement values. printf(" %4i |", Month); printf(" % 7.2f |", Opening); printf(" % 7.2f |", PmtRec); printf(" % 7.2f |", FinCharge); printf(" % 7.2f |", Closing); printf(" % 7.2f", MinDue); printf("\n"); // 2.3.3) TASK: Update necessary variables TotalCost += PmtRec; } // 3) TASK: Print Summary Information printf("\n"); printf("Total Cost Summary\n"); printf("Purchase Price: .............. %7.2f\n", Price); printf("Months Required to Pay Off:... %i\n", Month); printf("Total Finance Charge: ........ %7.2f\n", TotalCost - Price); printf("Total Cost: .................. %7.2f\n", TotalCost); return(0); } //== SUPPORT FUNCTIONS ==================================================== void PrintHeader(void) { 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); printf("========================================" "=======================================\n"); return; }