Investment program C++ interest rate declines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jwilley
    New Member
    • Oct 2014
    • 3

    Investment program C++ interest rate declines

    I just can't get the gradually declines part.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Instead of using the formula to calculate all 10 years at once, use it to calculate 1 year at a time in a loop. Adjusting the interest within the loop.

    Comment

    • jwilley
      New Member
      • Oct 2014
      • 3

      #3
      I'm a very beginner. Would it be possible to do an example for please?
      i appreciate it.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Please post the code that you currently have, we can then tell you what you need to change.

        But basically, in pseudocode, you would want to do something like this.
        Code:
        loop from 1 to 10
           runningTotal = growthFormula(1 year, interest rate, runningTotal)
           interest rate = interest rate - decrement
        end loop

        Comment

        • jwilley
          New Member
          • Oct 2014
          • 3

          #5
          Below is my program, but I'm probably not doing anything right here. I need a program that calculates the final value of a ten year, $20,000 investment whose annual return gradually declines from 5% to 2% over that term.

          Code:
          #include <iostream>
          using namespace std;
           
          int main()
          {
                 
              float balance      = 10000;     // account balance
              float interest     = .02;       // interest rate
              float rateOfChange = .001;          // interest rate of change
             
              for(int i = 0;i < 10; i++)
              {
                  balance += balance * interest;
                  interest -= rateOfChange;
              }
             
              cout << "The value after ten years is: $" << balance << endl;
             
              return 0;
          }

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            "gradually declines" is very vague. How about "declines from 5% to 2% in annual 1/4 % decrements?

            That way you could reduce the rate of return by a known amount over a defined period.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              You said you wanted the interest to start at 5% but you have it starting at 2. And you said you want it to drop 3% over the 10 years but you only have it dropping a tenth of a percent each year. If you want it to drop 3 percent, it needs to drop three tenths each year.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                "gradually declines" is very vague. You may need to seek clarification from the instructor. I can think of three ways to interpret this (listed below in order from easy to hard):
                1. The interest rate declines like a staircase: it stays constant until it abruptly decreases; the changes occur once each year on the anniversary of the investment and each step is the same size each time. (This is what weaknessforcats proposed above.)
                2. Same as above except the changes occur each day (or each minute or each second).
                3. No staircase: linear decline (interest is never constant, it changes from moment to moment).

                The difficulties with declines #2 and #3 are primarily in determining the right mathematical formula to use ... not so much with implementing the formula on the computer.

                Comment

                Working...