Homework... help!

This is a discussion on Homework... help! within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program for my intro to comp science class, here is the problem: Write a program that the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    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!

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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??

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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?

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    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.

  6. #6
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Quote Originally Posted by MacGyver View Post
    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.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    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

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    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)))
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    With that, now I'm just getting 0.990 for every year...

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Turn up the warning level. population is still uninitialized, yet you query it in the loop header.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Again, like I said, the only thing that is incremented each time is 'years', so the population is always going to be the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 03:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21