/************************** * bankAccount.cpp * Section 4002 IC210 * 2 Oct 2009 * Compute the balance in a bank account after 5 years */ #include using namespace std; //function prototypes // transaction - does the interactive part for you //- taking the initial balance B //and returning the balance after the transaction. double transaction(double B); //rate - returns the interest rate based on the account balance. double rate(double B); //compound - gives the value of a single dollar //after a year's monthly compounding with annual interest rate r. double compound(double r); int main() { //declare variables double B,r; //initialize variables B = 0; //perform transactions and compute balance for(int i = 0; i< 5; i++) { B = transaction(B); r = rate(B); B = B*compound(r); } cout << "Balance after 5 years is " << B << endl; return 0; } // transaction - does the interactive part for you //- taking the initial balance B //and returning the balance after the transaction. double transaction(double B) { char type; cout << "Type of transaction "; cin >> type; double amount; cout << "amount? "; cin >> amount; //update if (type == 'w') { B = B - amount; } else { B = B + amount; } //return return B; } //rate - returns the interest rate based on the account balance. double rate(double B) { // Get # of thousands int T = B/1000; // Calc rate double r = 3 + T*0.5; if (r > 8) r = 8; return r; } double compound(double r) { // Sim. year with monthly compounding double R = r/100, total = 1.0; for(int i = 0; i < 12; i++) total = total*(1 + R/12); return total; }