/** * Assignment #4, Program 1 * Program to print out a table giving total credit * card payments for various interest rates * and monthly payments. * Author: Mark Allen Weiss * SSN: 000-00-0000 * Course: COP-2210 Section 8 * Date: October 19, 1998 * * CERTIFICATION OF SOLE AUTHORSHIP * I certify that this work is solely my own and * that none if it is the work of any other person. * I further certify that I have not provided any * assistance to other students enrolled in COP-2210 that * would render their CERTIFICATION OF SOLE AUTHORSHIP * invalid. I understand that violations of this pledge * may result in severe sanctions. * * * * ----------------------------------- * (Mark Allen Weiss) <--- Put your name here, and sign above */ #include #include void PrintTableLine( double LoanAmt, double Apr, double LowPay, double HighPay, double PayInc ); double ComputeTotalPay( double InitialBalance, double Apr, double MonthlyPayment ); void PrintHeader( double InitialBalance, double LowMonthlyPay, double HighMonthlyPay, double MonthlyPayIncrement ); void PrintTable( double InitialBalance ); void PrintStars( int HowMany ); const double LOW_MONTH_PAYMENT = 100.00; const double HIGH_MONTH_PAYMENT = 400.00; const double MONTH_PAYMENT_INC = 50.00; const double LOW_APR = 7.0; const double HIGH_APR = 22.0; const double APR_INC = 0.1; const double WILL_NEVER_PAYOFF = 1e200; const int DECIMAL_PLACES = 2; const int MONEY_WIDTH = 8; // Because of roundoff errors, repeated additions of 0.1 are not exact. // So we use an extra FUDGE_FACTOR to make sure loops are correct. const double FUDGE_FACTOR = 0.01; int main( ) { double InitialBalance; cout << "Enter initial balance: " << endl; cin >> InitialBalance; cout.precision( DECIMAL_PLACES ); cout.setf( ios::fixed ); cout.setf( ios::showpoint ); PrintTable( InitialBalance ); return 0; } /** * Print out the table given an InitialBalance. */ void PrintTable( double InitialBalance ) { double Apr; PrintHeader( InitialBalance, LOW_MONTH_PAYMENT, HIGH_MONTH_PAYMENT, MONTH_PAYMENT_INC ); for( Apr = LOW_APR; Apr <= HIGH_APR + FUDGE_FACTOR; Apr += APR_INC ) PrintTableLine( InitialBalance, Apr, LOW_MONTH_PAYMENT, HIGH_MONTH_PAYMENT, MONTH_PAYMENT_INC ); } /* * Print a line corresponding to some fixed interest rate. */ void PrintTableLine( double LoanAmt, double Apr, double LowPay, double HighPay, double PayInc ) { double TotalPay, MonthPay; cout.precision( 1 ); cout << setw( 4 ) << Apr << "%:"; // interest rate cout.precision( DECIMAL_PLACES ); // Restore the precision for( MonthPay = LowPay; MonthPay <= HighPay + FUDGE_FACTOR; MonthPay += PayInc ) { TotalPay = ComputeTotalPay( LoanAmt, Apr, MonthPay ); cout << " "; if( TotalPay == WILL_NEVER_PAYOFF ) PrintStars( MONEY_WIDTH ); else cout << setw( MONEY_WIDTH ) << TotalPay; } cout << endl; } /** * Function that computes the total payments given the three parameters. */ double ComputeTotalPay( double InitialBalance, double Apr, double MonthPayment ) { double Mpr; // monthly interest rate double TotalPayment = 0.0; double CurrentBalance; CurrentBalance = InitialBalance; Mpr = Apr / 12.0 / 100.0; while( MonthPayment < CurrentBalance ) { TotalPayment += MonthPayment; // Monthly CurrentBalance -= MonthPayment; // payment CurrentBalance *= 1 + Mpr; // Interest if( CurrentBalance >= InitialBalance ) return WILL_NEVER_PAYOFF; } TotalPayment += CurrentBalance; return TotalPayment; } /** * Print the header for the table. */ void PrintHeader( double LoanAmount, double LowMonth, double HighMonth, double MonthInc ) { double MonthPay; cout << " PAYING OFF A $" << LoanAmount << " LOAN" << endl; cout << " TOTAL PAYMENTS FOR GIVEN INTEREST RATE AND MONTHLY PAYMENT"; cout << endl << " "; for( MonthPay = LowMonth; MonthPay <= HighMonth + FUDGE_FACTOR; MonthPay += MonthInc ) cout << " " << setw( MONEY_WIDTH ) << MonthPay; cout << endl; } /** * Print a bunch of stars. */ void PrintStars( int HowMany ) { for( int i = 0; i < HowMany; i++ ) cout << '*'; }