# Homework... help!

• 10-14-2007
xMEGANx
Homework... help!
I'm writing a program for my intro to comp science class, here is the problem:

Write a program that the initial population =.45 (in thousands) k = 4 and you calculate the population over 10,000 years. Find how many years have a population of [0 to .1], [.1 to .2], [.2 to .3] ... [.8 to .9] and so on...

Here is what I have...

Code:

```#include <iostream> using namespace std; int main() {     cout.setf(ios::fixed);     cout.setf(ios::showpoint);     cout.precision(3);     double initial_pop; // pop size this year     double population; // pop size next year     int k; // no idea what this is     int years = 0;     while (population >= 0)     {         years = years + 1;         if (years <= 10000 )         {             population = (k*(initial_pop) - k*(initial_pop)^2);             cout << years<<"\t"<<population << endl;         }         else         {             population = (k*(initial_pop) - k*(initial_pop)^2);             cout << years<<"\t"<<population << endl;         }     }     cout << "Enter any key to terminate the program." << endl;     char dummy;     cin >> dummy;     return 0; }```
I am getting an "Illegal use of floating point in function main ()" error, anyone have any ideas? Thanks!
• 10-14-2007
xMEGANx
OK so now I got it to compile and run, but it is just crazily adding up the years and giving me a value of 0.000 for the population...

My code looks like this...

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {     cout.setf(ios::fixed);     cout.setf(ios::showpoint);     cout.precision(3);     double initial_pop; // pop size this year     double population; // pop size next year     int k; // no idea what this is     int years = 0;     while (population >= 0)     {         years = years + 1;         if (years <= 10000 )         {             population = pow(k*initial_pop - k*(initial_pop),2);             cout << years<<"\t"<<population << endl;         }         else         {             cout << years<<"\t"<<population << endl;         }     }     cout << "Enter any key to terminate the program." << endl;     char dummy;     cin >> dummy;     return 0; }```
Help??
• 10-14-2007
xMEGANx
UPDATE:

Code:

```#include <iostream> #include <cmath> using namespace std; int main() {     cout.setf(ios::fixed);     cout.setf(ios::showpoint);     cout.precision(3);     double initial_pop = 0.45; // pop size this year     double population; // pop size next year     int k = 4; // no idea what this is     int years = 0;     while (population >= 0)     {         years = years + 1;         if (years <= 10000 )         {             population = pow(k*(initial_pop) - k*(initial_pop),2);             cout << years<<"\t"<<population << endl;         }         else         {             population = 0;             cout << years<<"\t"<<population << endl;         }     }     cout << "Enter any key to terminate the program." << endl;     char dummy;     cin >> dummy;     return 0; }```
Same problem.
• 10-14-2007
MacGyver
Maybe you're using some sort of new uber 1337 math, but... I'm pretty sure (k*(initial_pop) - k*(initial_pop)) is going to always be 0.... Zero raised to the power of 2 is.... still zero.

So what are you actually trying to do?
• 10-14-2007
mikeman118
I think it is because you are trying to multiply an int and a double, which I don't think it lets you do. Maybe try changing 'int k' to 'double k'.

Also, just a tip you could probably change the middle to a for loop, I don't know if that is better or worse but I think it makes more sense. For example:
Code:

```    while (population >= 0)     {                 for (years = 0 /*That's already been stated but it doesn't matter*/; years <= 10000; years++)                 {                         population = pow(k*(initial_pop) - k*(initial_pop),2);                         cout << years<<"\t"<<population << endl;                 }                 population = 0; // When years = 10,000 it will go here                 cout << years<<"\t"<<population << endl;     }```
Like I said, this isn't necessary and maybe you shouldn't use it in case you haven't learned it or something, but it's shorter and probably the better way.
• 10-14-2007
mikeman118
Quote:

Originally Posted by MacGyver
Maybe you're using some sort of new uber 1337 math, but... I'm pretty sure (k*(initial_pop) - k*(initial_pop)) is going to always be 0.... Zero raised to the power of 2 is.... still zero.

So what are you actually trying to do?

Yeah in my previous post I didn't realize what error you were getting. But that is always going to return zero, like he said. Also, even if it wasn't zero nothing is ever increased (well, 'years' is but it isn't part of the equation) so you'd always end up with the same result.
• 10-14-2007
xMEGANx
Yeah... I don't know, the equasion that my professor gave us was:

P_2 = k*p_1 - k*(p_1)^2

Where p_1= the initial population (.45)
and p_2= teh following population

Sooo I don't know
• 10-14-2007
MacGyver
Note the ()'s.... Square the second p_1. Then go. So it would be... I don't know. Something like this:

Code:

`P_2 = (k*p_1) - (k*((p_1)^2))`
Oh, btw.... ^ does not raise to a power, so you're going to get something weird. Use the pow() function.
• 10-14-2007
tjpanda
In case you're not allowed to use the pow() function, multiply the value by itself.

Code:

`P_2 = (k*p_1) - (k*((p_1)*(p_1)))`
• 10-14-2007
xMEGANx
With that, now I'm just getting 0.990 for every year...
• 10-15-2007
CornedBee
Turn up the warning level. population is still uninitialized, yet you query it in the loop header.
• 10-15-2007
mikeman118
Again, like I said, the only thing that is incremented each time is 'years', so the population is always going to be the same.