Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,089 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,295 people online right now. Registration is fast and FREE... Join Now!




Fibonacci Numbers

2 Pages V  1 2 >  
Reply to this topicStart new topic

Fibonacci Numbers

duskmon10
post 7 Oct, 2008 - 04:43 PM
Post #1


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 94

The program is similar to the fibonnaci numbers. Here's the entire problem:

One place the fibonnaci numbers occur is as certain population growth rates. If a population has no deaths, then the series shows the size of the population after each time period. The formula applies most straightforwardly to asexual reproduction at a rate of one offspring per time period.

Assume that the green crud population grows at this rate and has a time period of 5 days. Hence, if a green crud population starts out as 10 pounds of crud, then in 5 days there is still 10 pounds of curd; in 10 days there is 20 pounds of crud, in 15 days 30 pounds in 20 days 50 pounds, and so forth. Write a program that takes both the initial size of a green crud population (in pounds) and a number of days as input, and that outputs the number of pounds of green crud after that many days. Assume that the population size is the same for four days and then increases every fifth day. Your program should allow the user to repeat this calculation as often as desired.

Here's what I have so far:
CODE

#include <iostream>
using namespace std;
double population(int pop, int time);
int main()
{
    int final, pop, time;
    cout<<"Plese enter in the initial size of the green crud."<<endl;
    cin>>pop;
    cout<<"Please enter in the time in days."<<endl;
    cin>>time;
    final = population(int pop, time)
    cout<<"The number of crud is "<<final<<endl;
    return 0;
}
double population(int pop, int time)
{
if(time%5==0)
{
    pop = time + pop;
    if(time<=5)
        pop = 5;
}
else(pop = pop);
return pop;
}


And I got the following errors:

1>c:\users\owner\documents\visual studio 2008\projects\homework5-1\homework5-1\question1.cpp(11) : error C2144: syntax error : 'int' should be preceded by ')'
1>c:\users\owner\documents\visual studio 2008\projects\homework5-1\homework5-1\question1.cpp(11) : error C2660: 'population' : function does not take 0 arguments
1>c:\users\owner\documents\visual studio 2008\projects\homework5-1\homework5-1\question1.cpp(11) : error C2059: syntax error : ')'

Thanks in advance for your help.
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 04:54 PM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,149



Thanked 16 times

Dream Kudos: 450
My Contributions


final = population(int pop, time)
You don't need that 'int' in there when you call population. just do
poulation(pop, time)

This post has been edited by GWatt: 7 Oct, 2008 - 04:55 PM
User is online!Profile CardPM

Go to the top of the page

BetaWar
post 7 Oct, 2008 - 04:56 PM
Post #3


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,981



Thanked 77 times

Dream Kudos: 1175
My Contributions


I have debugged it, but I don't think it works as it should, the population function seems to be off in its results.:

CODE
#include <iostream>
using namespace std;
int population(int pop, int time);
int main(){
    int pop;
    int time;
    int final;
    cout<<"Plese enter in the initial size of the green crud."<<endl;
    cin>>pop;
    cout<<"Please enter in the time in days."<<endl;
    cin>>time;
    final = population((int)pop, (int)time);
    cout<<"The number of crud is "<<final<<endl;
    return 0;
}
int population(int pop, int time){
    if(time%5==0){
        pop += time;
        if(time<=5)
            pop = 5;
    }
    return pop;
}
User is offlineProfile CardPM

Go to the top of the page

duskmon10
post 7 Oct, 2008 - 05:05 PM
Post #4


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 94

I fixed all of the errors, here's the new code:

CODE

#include <iostream>
using namespace std;
int population(int pop, int time);
int main()
{
    int final, pop, time;
    cout<<"Plese enter in the initial size of the green crud."<<endl;
    cin>>pop;
    cout<<"Please enter in the time in days."<<endl;
    cin>>time;
    final = population(pop, time);
    cout<<"The number of crud is "<<final<<endl;
    return 0;
}
int population(int pop, int time)
{
if(time%5==0)
{
    pop = time + pop;
    if(time<=5)
        pop = 5;
}
else(pop = pop);
return pop;
}


Now I just need to add in the code to let the user repeat the program. How do I do that? I want to ask them if they want to continue and ask them to either enter in Y or N for an answer.
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 05:15 PM
Post #5


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,149



Thanked 16 times

Dream Kudos: 450
My Contributions


Put the code for getting user input, and printing out the result inside of a do while loop
CODE

char repeat;
do
{
    //stuff
}
while (repeat = 'y' || repeat = 'Y')
User is online!Profile CardPM

Go to the top of the page

duskmon10
post 7 Oct, 2008 - 05:16 PM
Post #6


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 94

Where do I put that inside of the program?
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 05:22 PM
Post #7


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,149



Thanked 16 times

Dream Kudos: 450
My Contributions


wherever you have the code that interacts with the user.

you have yours inside of the main function

you should also ask them if they want to repeat, and get input for that.
User is online!Profile CardPM

Go to the top of the page

BetaWar
post 7 Oct, 2008 - 05:24 PM
Post #8


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,981



Thanked 77 times

Dream Kudos: 1175
My Contributions


Youu would put it in your main loop. However the code given above doesn't work out of the box, it needs some assembly (like getting it to compile correctly)

CODE
#include <iostream>
using namespace std;
int population(int pop, int time);
int main(){
    char repeater;
    do{
        repeater = 'n';
        int pop = 0;
        int time = 0;
        int final = 0;
        cout<<"Plese enter in the initial size of the green crud."<<endl;
        cin>>pop;
        cout<<"Please enter in the time in days."<<endl;
        cin>>time;
        final = population((int)pop, (int)time);
        cout<<"The number of crud is "<<final<<endl;
        cout<<"Would you like to repeat the program?"<<endl;
        cin>>(char)repeater;
    }
    while(repeater == 'y' || repeater == 'Y');
        return 0;
}
int population(int pop, int time){
    if(time%5==0){
        pop += time;
        if(time<=5)
            pop = 5;
    }
    return pop;
}


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

duskmon10
post 7 Oct, 2008 - 05:27 PM
Post #9


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 94

It doesn't work, it asks me if I want to repeat twice and then the program ends. Here's the code:

CODE

#include <iostream>
using namespace std;
int population(int pop, int time);
int main()
{
    int final, pop, time;
    cout<<"Plese enter in the initial size of the green crud."<<endl;
    cin>>pop;
    cout<<"Please enter in the time in days."<<endl;
    cin>>time;
    final = population(pop, time);
    cout<<"The number of crud is "<<final<<endl;
    char ans;
    do
    {
        cout<<"Would you like to repeat the program? Y or N."<<endl;
        cin>>ans;
    }
    while ((ans == 'y'||ans == 'Y'));
    return 0;
}
int population(int pop, int time)
{
if(time%5==0)
{
    pop = time + pop;
    if(time<=5)
        pop = 5;
}
else(pop = pop);
return pop;
}
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 05:30 PM
Post #10


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,149



Thanked 16 times

Dream Kudos: 450
My Contributions


you have to pu the do { before you get user input about the program
cpp

do
{
cout<<"Plese enter in the initial size of the green crud."<<endl;
cin>>pop;
cout<<"Please enter in the time in days."<<endl;
cin>>time;
final = population(pop, time);
cout<<"The number of crud is "<<final<<endl;
char ans;

cout<<"Would you like to repeat the program? Y or N."<<endl;
cin>>ans;
}
while ((ans == 'y'||ans == 'Y'));
return 0;
User is online!Profile CardPM

Go to the top of the page

BetaWar
post 7 Oct, 2008 - 05:30 PM
Post #11


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,981



Thanked 77 times

Dream Kudos: 1175
My Contributions


All you are repeating with this code is asking if the user wants to repeat it.

CODE
    char ans;
    do
    {
        cout<<"Would you like to repeat the program? Y or N."<<endl;
        cin>>ans;
    }
    while ((ans == 'y'||ans == 'Y'));


Not only that, but it also will run ahead and not allow you to input more data unless you change the value of ans each time through the loop.

Look at the code I posted in my last post.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

duskmon10
post 7 Oct, 2008 - 05:33 PM
Post #12


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 94

Now I got an error saying that ans was undeclared:

CODE

#include <iostream>
using namespace std;
int population(int pop, int time);
int main()
{
    do
    {
    int final, pop, time;
    cout<<"Plese enter in the initial size of the green crud."<<endl;
    cin>>pop;
    cout<<"Please enter in the time in days."<<endl;
    cin>>time;
    final = population(pop, time);
    cout<<"The number of crud is "<<final<<endl;
    char ans;
    cout<<"Would you like to repeat the program? Y or N."<<endl;
    cin>>ans;
    }
    while ((ans == 'y'||ans == 'Y'));
    return 0;
}
int population(int pop, int time)
{
if(time%5==0)
{
    pop = time + pop;
    if(time<=5)
        pop = 5;
}
else(pop = pop);
return pop;
}
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/21/08 08:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month