Advertisement
Conner_36

interest...

Sep 18th, 2011
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  C4
  4. //
  5. //  Created by Betsalel Williamson on 9/18/11.
  6. //  Chapter 4:
  7. //  Compute and output compound interest on $1000.00 for 10 years, at interest rates of 5%, 6%, 7%, 8%, 9% and 10%.
  8. //
  9. //  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.
  10. //
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. //  You can change the start and end interest rates to show interest rates above and below 10 percent.
  16. #define START_INTEREST_RATE 5
  17. #define END_INTEREST_RATE 10
  18. #define ACCOUNTS ((END_INTEREST_RATE-START_INTEREST_RATE)+1)
  19.  
  20. int main (int argc, const char * argv[])
  21. {
  22.     double moneyInBank = 1000.00;
  23.     int years = 10;
  24.    
  25.     int variableInterestRate; //  doubles as a counter
  26.     int counter; //  a variable only for use in the for loops
  27.     double bankAccount[ACCOUNTS]; //  will hold the money and interest
  28.    
  29.     //  add the 1000.00 to each bank account
  30.     for (counter = 0; counter < ACCOUNTS; counter++) {
  31.         bankAccount[counter] = moneyInBank;
  32.     }
  33.    
  34.     //  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.
  35.     for (variableInterestRate  = START_INTEREST_RATE;
  36.          variableInterestRate <= END_INTEREST_RATE;
  37.          variableInterestRate++) {
  38.        
  39.         for (counter = 0; counter < years; counter++) {
  40.             //  the position in the array can be found by subtracting the first counter variableInterestRate by 5.
  41.             //  so when the counter is first run I will be at position 0, and will increment the array as the variableInterestRate increases.
  42.             //  again I use the counter variableInterestRate to compute the current iteration of interest rate.
  43.             //  its bad practice to use variables cleverly, but it works.
  44.             bankAccount[variableInterestRate-START_INTEREST_RATE] += bankAccount[variableInterestRate-START_INTEREST_RATE]*(variableInterestRate*(.01));
  45.         }
  46.     }
  47.    
  48.     //  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.
  49.     for (counter = 0; counter < ACCOUNTS; counter++) {
  50.         printf("$%.2f at %%%-2d interest for %d years = $%.2f\n",
  51.                moneyInBank,
  52.                (counter+START_INTEREST_RATE),
  53.                years,
  54.                bankAccount[counter]);
  55.     }
  56.    
  57.     return EXIT_SUCCESS;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement