C++ Question: Calculate the Investment at time n

  • Context: Comp Sci 
  • Thread starter Thread starter Hughng
  • Start date Start date
  • Tags Tags
    C++ Time
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming assignment related to calculating investment growth in the stock market. Participants are addressing issues with the implementation of a program that estimates how long it takes to reach a desired investment amount, considering factors such as initial investment, yearly growth rate, annual withdrawals, and the target balance.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their program's logic for calculating investment growth and the conditions for withdrawals.
  • Another participant requests clarification on the input values used, particularly the growth rate.
  • Some participants express confusion regarding the discrepancy between expected and actual outputs, specifically the expected withdrawal amount of 340.11 versus the calculated 360.
  • A participant asserts that with a negative growth rate of -0.1, reaching the desired balance of 1,000,000 is impossible given the initial investment and withdrawal amount.
  • There is a discussion about the implications of the growth rate on the investment outcome, with one participant apologizing for initially miscommunicating the growth rate.
  • Another participant emphasizes the need to print the number of years until the account is exhausted, regardless of the final balance.

Areas of Agreement / Disagreement

Participants generally agree that with a growth rate of -0.1, the desired investment amount cannot be reached. However, there is disagreement regarding the correct withdrawal amount and the expected output of the program, with no consensus on why the expected result differs from the actual calculation.

Contextual Notes

Participants mention specific input values and conditions that affect the program's output, but there are unresolved issues regarding the calculations and assumptions made in the code.

Who May Find This Useful

Readers interested in programming assignments related to financial calculations, investment growth modeling, or C++ coding practices may find this discussion relevant.

Hughng
Messages
26
Reaction score
0

Homework Statement


Pretend you have some money (ha!) that you want to invest in the stock market. Ask the user for:

  1. The initial investment
  2. Yearly estimated market growth
  3. How much (if any) you wish to withdraw from the account per year
  4. The desired level of money you wish to get
The growth formula per year is simply:
I_new = I_old x (1+g)
Assume you withdraw money from the account after it grows (you cannot withdraw more than the account has!). Find out how long it takes to reach the desired amount of money (if it is reachable at all).

Homework Equations


I completed the program but when I ran g =-0.1 with other given values, I was supposed to obtain 340.11 as my instructor required, but I got a whole number: 360.

The Attempt at a Solution


C:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double I_0, g, W, BAL, I;
    count << " Initial Investment? ";
    cin >> I_0;
    count << " Yearly growth rate? ";
    cin >> g;
    count << " Yearly withdraw amount? ";
    cin >> W;
    count << " Desired balance? ";
    cin >> BAL;
    I = I_0*(1 + g);
    int i = 1;
    if (W <= I_0 && g >= 0)
    {
        for (i = 1; I < BAL; i++)
            {  
                I = I - W;
                I = I*(1 + g);
            }
                count << " Years: " <<  i << endl;
                count << " Balance at the end: "<< I << endl;
                count << " Amount withdrawn over period: " << W*i << endl;  
    }
    else if (W >= I_0)
    {
        count << " Years: " <<  i << endl;
        count << " Balance at the end: "<< 0 << endl;
        count << " Amount withdrawn over period: " << I << endl;
    }
  
    else if (W <= I_0 && g <= 0)
    {
        for (i = 1; I >= 0; i++)
            {  
                I = I - W;
                I = I*(1 + g);
            }
                count << " Years: " <<  ceil(i) << endl;
                count << " Balance at the end: "<< 0 << endl;
                count << " Amount withdrawn over period: " <<W*i << endl;  
    }
    return 0;
}
 
Last edited by a moderator:
Physics news on Phys.org
Hughng said:

Homework Statement


Pretend you have some money (ha!) that you want to invest in the stock market. Ask the user for:

  1. The initial investment
  2. Yearly estimated market growth
  3. How much (if any) you wish to withdraw from the account per year
  4. The desired level of money you wish to get
The growth formula per year is simply:
I_new = I_old x (1+g)
Assume you withdraw money from the account after it grows (you cannot withdraw more than the account has!). Find out how long it takes to reach the desired amount of money (if it is reachable at all).

Homework Equations


I completed the program but when I ran g =-0.1 with other given values, I was supposed to obtain 340.11 as my instructor required, but I got a whole number: 360.
What were the other input values you used?

Also, when you post code, please include code tags. I have added them in your post.

They look like this:
C:
<your code>
Hughng said:

The Attempt at a Solution


C:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    double I_0, g, W, BAL, I;
    count << " Initial Investment? ";
    cin >> I_0;
    count << " Yearly growth rate? ";
    cin >> g;
    count << " Yearly withdraw amount? ";
    cin >> W;
    count << " Desired balance? ";
    cin >> BAL;
    I = I_0*(1 + g);
    int i = 1;
    if (W <= I_0 && g >= 0)
    {
        for (i = 1; I < BAL; i++)
            { 
                I = I - W;
                I = I*(1 + g);
            }
                count << " Years: " <<  i << endl;
                count << " Balance at the end: "<< I << endl;
                count << " Amount withdrawn over period: " << W*i << endl; 
    }
    else if (W >= I_0)
    {
        count << " Years: " <<  i << endl;
        count << " Balance at the end: "<< 0 << endl;
        count << " Amount withdrawn over period: " << I << endl;
    }
 
    else if (W <= I_0 && g <= 0)
    {
        for (i = 1; I >= 0; i++)
            { 
                I = I - W;
                I = I*(1 + g);
            }
                count << " Years: " <<  ceil(i) << endl;
                count << " Balance at the end: "<< 0 << endl;
                count << " Amount withdrawn over period: " <<W*i << endl; 
    }
    return 0;
}
 
Here are the data:

Initial investment? 1000
Yearly growth rate? 0.1
Yearly withdraw amount? 20
Desired balance? 1000000
Years: 18
Balance at end: 0
Amount withdrawn over period: 340.114
 
Hughng said:
Yearly growth rate? 0.1
You said in an earlier post that you were using -0.1 as the growth rate.
Which is it, a growth rate of 0.1 or -0.1?
 
The growth rate is -0.1. I am sorry I did not notice. Thank you.
 
If the growth rate is -0.1 and you start with 1000 and withdraw 20 each year, you'll never get to 1,000,000.
 
Hello Mark,
Yes, if you keep withdrawing the money given the initial amount above, you will not get the desire investment. The assignment will ask you to "count" O for final balance, but you still have to print out the number of years that will take to exhaust your account, and the total amount you can withdraw until you have nothing to withdraw. However, I had an issue which was I could not print out the desired withdrawn amount as the assignment require: 340.11. I got a whole number which is 360.
Thank you.
 
Hughng said:
I had an issue which was I could not print out the desired withdrawn amount as the assignment require: 340.11. I got a whole number which is 360.
Why do you think that $340.11 is the correct amount? If you withdraw $20 per year for 18 years, that's $360.
 
  • #10
I did not think so, but the assignment asked me to print out the desired result. I submitted the assignment, but I really appreciate your help.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 15 ·
Replies
15
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K