In the problem I'm trying to do, I have to calculated the projected growth over a number of years..Problem is I'm having trouble calculating the population after year 1..
Can anyone see where I'm making the problem?
#include %26lt;iostream%26gt;
using namespace std;
int main()
{
int i=1;
double pop, pergro, years;
int growth,total_growth;
cout %26lt;%26lt; "Enter population:";
cin %26gt;%26gt; pop;
cout %26lt;%26lt; "Enter percentage rate of growth:";
cin %26gt;%26gt;pergro;
cout %26lt;%26lt; "Enter years:";
cin %26gt;%26gt; years;
for (int i=1; i%26lt;=years; i++)
{
growth = pop*(pergro*0.01);
total_growth = growth+pop;
cout%26lt;%26lt;"Population after "%26lt;%26lt;i%26lt;%26lt;"year is:"%26lt;%26lt;total_growth++%26lt;%26lt;endl;
}
return 0;
}
C++ question?
hum are you sure you aren't messing up with types ?
I see sums of int and double, sum of double stored in a int. Add some cast to make sure it isn't your trouble.
For instance :
growth = pop*(pergro*0.01);
is :
(int) = (double) * ((double)*(*double))
try
growth = (int)(pop*(pergro*0.01));
(there are other places in your code where this problem occurs)
total_growth = growth+pop;
(int) = (int) + (double)
Make sure you always sums stuff of the same type :o)
Reply:cout%26lt;%26lt;"Population after "%26lt;%26lt;i%26lt;%26lt;"year is:"%26lt;%26lt;total_growth++%26lt;%26lt;endl;
the problem is in this line . u have written total_growth++ which is wrong but its working for i=1 bcoz u have used post increment operator in total_growth. just remove ++ and check i hope it will work.
Reply:for (int i=1; i%26lt;=years; i++)
{
growth = pop*(pergro*0.01);
total_growth = growth+pop;
}
cout%26lt;%26lt;"Population after "%26lt;%26lt;i%26lt;%26lt;"year is:"%26lt;%26lt;total_growth++%26lt;%26lt;endl;
return 0;
}
flower garden
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment