Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // C4
- //
- // Created by Betsalel Williamson on 9/18/11.
- // Chapter 4:
- // Compute and output compound interest on $1000.00 for 10 years, at interest rates of 5%, 6%, 7%, 8%, 9% and 10%.
- //
- // This piece of code contains more comments than usual because of the for loops and clever use of variables. They make things harder to remember down the road, so I documented why and how I did things.
- //
- #include <stdio.h>
- #include <stdlib.h>
- // You can change the start and end interest rates to show interest rates above and below 10 percent.
- #define START_INTEREST_RATE 5
- #define END_INTEREST_RATE 10
- #define ACCOUNTS ((END_INTEREST_RATE-START_INTEREST_RATE)+1)
- int main (int argc, const char * argv[])
- {
- double moneyInBank = 1000.00;
- int years = 10;
- int variableInterestRate; // doubles as a counter
- int counter; // a variable only for use in the for loops
- double bankAccount[ACCOUNTS]; // will hold the money and interest
- // add the 1000.00 to each bank account
- for (counter = 0; counter < ACCOUNTS; counter++) {
- bankAccount[counter] = moneyInBank;
- }
- // this is the loop where I add the interest to the bank accounts from with the first account at %5 interest for 10 years until the sixth account with %10 interest.
- for (variableInterestRate = START_INTEREST_RATE;
- variableInterestRate <= END_INTEREST_RATE;
- variableInterestRate++) {
- for (counter = 0; counter < years; counter++) {
- // the position in the array can be found by subtracting the first counter variableInterestRate by 5.
- // so when the counter is first run I will be at position 0, and will increment the array as the variableInterestRate increases.
- // again I use the counter variableInterestRate to compute the current iteration of interest rate.
- // its bad practice to use variables cleverly, but it works.
- bankAccount[variableInterestRate-START_INTEREST_RATE] += bankAccount[variableInterestRate-START_INTEREST_RATE]*(variableInterestRate*(.01));
- }
- }
- // I used counter here even though I incremented the array with variableInterestRate in the previous loop. Counter has no connection to years or bankAccount, it's just a one use variable.
- for (counter = 0; counter < ACCOUNTS; counter++) {
- printf("$%.2f at %%%-2d interest for %d years = $%.2f\n",
- moneyInBank,
- (counter+START_INTEREST_RATE),
- years,
- bankAccount[counter]);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement