Reputation: 13
I'm currently trying to use an array in a while loop in C for class. The goal is to take the portfolio value and add it to the dividend rate, then times it by 1.10 to simulate a 10% return. When I run the program it does the first loop correctly, but all years after that show the same amount. So if I have an initial value of 1000 and a dividend rate of 5, I would expect 1105 on the first year, 1220 on the second and so on. I'm getting 1105 for every year.
Thanks in advance.
Edit: chopped out a lot of unneeded code that I don't think is pertinent to the issue.
So what I think I need to do is figure out how to increment x
, and add the previous value to it. So if the first year was 1,000, then the second year would be it's value + 1,000. etc...
I'm trying to figure out the best way to execute that.
while (year <= 19)
{
(totalvalue[x] = dividend + portfoliovalue *1.10);
year++;
printf("The total value of the portfolio after %u year will be approximately %u.\n", year, totalvalue[x]);
}
system("PAUSE");
return 0;
}
Upvotes: 0
Views: 165
Reputation: 29266
The formula totalvalue[x] = dividend + portfoliovalue *1.10
doesn't make any use of year
or any previous value etc and so is clearly wrong as it doesn't use any value that actually changes in the loop. I'm not going to put in the "full answer" as you have said this is homework.
In addition you don't need an array just to print the values. If you do want an array to keep the values for something else later then you probably want to index on year
and not x
.
Upvotes: 4