Public Group

# Some help on a program

This topic is 2948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Disclaimer: I don't want someone to give me the answer, I just need a little guidance.

Ok, the program I am supposed to code is using Fibonacci's numbers.(Yes, this is an assignment) I thought I had the answer, so I got to coded. But my program keeps yielding a ridiculous random number.
The assignment is
" Assume that green crud grows at the rate of Fibonnaci's numbers and has a 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; 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 the crud 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."

Here is my source code:
#include<iostream>using namespace std;int greencrud(int numberof_crud, int crud_days, int amount_crud);int greencrud(int numberof_crud, int crud_days, int amount_crud){        for(int i = 1; i <= crud_days; i++)    {        do{            amount_crud = amount_crud + amount_crud;            numberof_crud = amount_crud;        }        while(crud_days % 5 == 0);	}return numberof_crud;}int main()    {        cout << "Welcome to Crud Estimator\n";        cout << "Please enter the number of days the crud has been grown \n";        int crud_days = 0;        cin >> crud_days;        cout << "Please enter the original amount of crud ";        int amount_crud;        cin >> amount_crud;        cout << "\nThe number of crud you have in your household is " << greencrud(numberof_crud);        return 0;}

[Edited by - Blckknight118 on October 18, 2010 6:20:40 PM]

##### Share on other sites
You need to pass parameters to your function. Without parameters, the function name can be interpreted as a pointer value, which when printed can look like a random number, or sometimes the compiler will try to interpret it as a boolean.

Also, please post the full code in future, including any #include statements, using directives and function declarations. Posting an example that we can quickly try out is essential.

##### Share on other sites
I don't understand, I already set the parameters once I declared the function greencrud.

##### Share on other sites
A function call must specify parameters. Your attempt at a function call is at the end of this line:
cout << "\nThe number of crud you have in your household is " << greencrud;

You didn't pass any parameters here, which means you didn't call the function. Instead, you asked the compiler to print the function itself, not its result for certain inputs.

An example of calling the function would be:
cout << "\nThe number of crud you have in your household is " << greencrud(13, 42, 15);

Finally, note that the variable names in the calling function and called function have no relationship. That is, "crud_days" inside main() is a totally separate value from "crud_days" in greencrud(). If you want these values to match up, you will have to pass the values in the correct "slots" in the function call.

In your revised code, you will have to declare the greencrud() function before main() so you can call it, or more simply move the implementation of greecrud() before main(). C++ requires that you declare all variables, functions and types before you make use of them.

##### Share on other sites
I revised my code, but when I try to call numberof_crud it says it is undefined.

##### Share on other sites
That's because in your main() function you never define a variable named numberof_crud. You do have some variables named crud_days and amount_crud. Maybe you should try using those.

##### Share on other sites
Because you do not define numberof_crud in main.

When you call a function you have to pass the information (paramaters) it asks for. The function takes a copy of that information and runs with it.

But the parameters must be local or global variables available in the scope (in this case: 'main') that is calling the function.

You can also pass constants (as in actual numbers)

##### Share on other sites
I still don't understand....I returned the value of numberof_crud at the end of the greencrud function. Also, I declared the parameters before. This is sort of confusing me.

##### Share on other sites
Ok.

Each function works an a separate 'component' of a program. You have to think of both main and greencrud as separate entities.

greencrud requires three pieces of information to do its stuff. When you call the function the call (in this case, in main) must provide that information.

---Lesson on parameters---
Consider the following:

{
return Value1+Value2;
}

The above function needs two numbers and it will add them together.

Lets look at ways we can use this function:

main()
{
int A=5;
int B=7;
}

Add needs two numbers, in the above we gave it A and B which at that time contained the values 5 and 7 respectively.

As a result the Add function was called. Its Parameters (Value1 and Value2) were set to 5 and 7 (the contents of A & B, in the order they are given). The function is executed with those values.

But although 'Param1' and 'A' now contain the same value (5) they are separate variables. If Add was to change Param1 to something A would NOT be changed. They are separate.

The Add function however simply adds those two and returns the result. Therefore our program now returns to main and 'Result' becomes 12.

Because Add takes an int, we can also pass real numbers. We can do this:

main()
{
int A=5;
}

The value of 'Result' would now be set to 32.

The fact you have named the parameters in 'greencrud' witht he same names as you use for variables in 'main' is irrelevant, they are separate entities.

Only if you declare them globally can they be 'shared', but that is a later lesson.

##### Share on other sites
First off, variable names only work in a scope. Function calls change scope. Function calls also remap variable names from the caller to the callee.
Secondly, functions take in parameter between (), and the number of parameters has to match exactly.
Lastly, the function return value comes back where the function call is as the "value" of that function.

Maybe the following working example will help you see some of this.
#include <iostream>int foo( int x, int y, int z ){  std::cout << y << std::endl;  std::cout << z << std::endl;  return x;}int main( int argc, char **argv ){  int y = 4;  int a = 6;  int b = 9;  std::cout << foo( y, a, b ) << std::endl;  return 0;}

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633313
• Total Posts
3011315
• ### Who's Online (See full list)

There are no registered users currently online

×