• Advertisement
Sign in to follow this  

Some help on a program

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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:

int Add(int Value1,int Value2)
{
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;
int Result=Add(A,B);
}

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;
int Result=Add(A,27);
}

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 this post


Link to post
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;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
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.
*** Source Snippet Removed ***


Wow.....I completely don't understand why its displaying 6,9 and 4. I don't understand when the numbers are being swapped.

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
ely don't understand why its displaying 6,9 and 4. I don't understand when the numbers are being swapped.


Well the first output is from this line in 'foo'....

std::cout << y << std::endl;

Now what is 'y' defined as? Remember the 'y' in foo() is NOT the same as the y in main(). So where and how is this particular y defined?

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
Quote:
Original post by Blckknight118
ely don't understand why its displaying 6,9 and 4. I don't understand when the numbers are being swapped.


Well the first output is from this line in 'foo'....

std::cout << y << std::endl;

Now what is 'y' defined as? Remember the 'y' in foo() is NOT the same as the y in main(). So where and how is this particular y defined?


'Y' is definted as 4 in main, but the display says that it comes out as 6....
I know the Ys aren't the same but where the hell is the 'y' in foo getting the damn 6 from?

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118


'Y' is definted as 4 in main, but the display says that it comes out as 6....


Nope. I didn't ask about the 'y' in main(), I asked about the one in foo(), which is completely separate to the one in main(). They happen to have the same name 'y' but they are totally separate.

Where is the 'y' that is used in foo() defined?

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
Quote:
Original post by Blckknight118


'Y' is definted as 4 in main, but the display says that it comes out as 6....


Nope. I didn't ask about the 'y' in main(), I asked about the one in foo(), which is completely separate to the one in main(). They happen to have the same name 'y' but they are totally separate.

Where is the 'y' that is used in foo() defined?


Well, I know it's definted in the parameters of foo. That is, int y in foo's declaration. I just don't know where its getting the 6 from if its not in the function and main() only has two declarations.

Share this post


Link to post
Share on other sites
Remove the variables from the equation...

#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 )
{
std::cout << foo( 4, 6, 9 ) << std::endl;
return 0;
}




Now do you see what happened?

or... maybe this could make it more clear that the names are totally arbitrary and only have to match within any one particular scope:

#include <iostream>

// prototype defining foo() taking 3 ints
int foo( int, int, int );

// main function with 2 parameters
int main( int argc, char **argv )
{
std::cout << foo( 4, 6, 9 ) << std::endl;
return 0;
}

// definition of foo, naming the 3 input ints as x,y, and z
int foo( int x, int y, int z )
{
std::cout << y << std::endl;
std::cout << z << std::endl;
return x;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118

Well, I know it's definted in the parameters of foo. That is, int y in foo's declaration. I just don't know where its getting the 6 from if its not in the function and main() only has two declarations.


You are on the right track. It IS defined in the parameter list you are correct, specifically as the SECOND parameter. The first is x and last is z.

Now take a look at the call to foo() thats in main()

std::cout << foo( y, a, b ) << std::endl;

What is being passed as the SECOND parameter when foo() is called?

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
Quote:
Original post by Blckknight118

Well, I know it's definted in the parameters of foo. That is, int y in foo's declaration. I just don't know where its getting the 6 from if its not in the function and main() only has two declarations.


You are on the right track. It IS defined in the parameter list you are correct, specifically as the SECOND parameter. The first is x and last is z.

Now take a look at the call to foo() thats in main()

std::cout << foo( y, a, b ) << std::endl;

What is being passed as the SECOND parameter when foo() is called?


So 'y' is being specifically passed as 'a' since they are both the second parameters. And since 'y' is the first variable to be declared the display is y=a=6 since the a in main equals 6 and the a is being called a parameter. But the z isn't being called main...Never mind I got that. I just don't understand the return x; or how the last cout is showing 4.

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
So 'y' is being specifically passed as 'a' since they are both the second parameters. And since 'y' is the first variable to be declared the display is y=a=6 since the a in main equals 6 and the a is being called a parameter. But the z isn't being called main...


No. 'a' is being passed. Its contents, '6', is then copied into the 'y' which is defined in foo(). Remember that the 'y' in foo() is NOTHING to do with the 'y' in main(). At all.

There is no 'z' in main() its true, but there IS in foo. The 'z' in foo() is the THIRD parameter.

What is being passed as the THIRD parameter in the line below?

std::cout << foo( y, a, b ) << std::endl;

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
Quote:
Original post by Blckknight118
So 'y' is being specifically passed as 'a' since they are both the second parameters. And since 'y' is the first variable to be declared the display is y=a=6 since the a in main equals 6 and the a is being called a parameter. But the z isn't being called main...


No. 'a' is being passed. Its contents, '6', is then copied into the 'y' which is defined in foo(). Remember that the 'y' in foo() is NOTHING to do with the 'y' in main(). At all.

There is no 'z' in main() its true, but there IS in foo. The 'z' in foo() is the THIRD parameter.

What is being passed as the THIRD parameter in the line below?

std::cout << foo( y, a, b ) << std::endl;


b is the third parameter. So does that mean that b is passing the value 9 to y. I understand that, but the last cout that has the whole function makes no sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by Blckknight118
[

b is the third parameter. So does that mean that b is passing the value 9 to y.


No. 'b' is passed to the third parameter. The third parameter in foo() is 'z' (not 'y') so 'z' gets a copy of what was in 'b'.

Quote:
I understand that, but the last cout that has the whole function makes no sense.


The last cout, the one that is in main takes what foo returns. To save time,

foo returns x, x within foo is the first parameter, and in the call in main is y (which is 4) is passed as the first parameter.

So foo's 'x' becomes 4, foo returns it's x so foo returns a value of 4. cout is taking the value that foo is returning (4) and doing what it does.

This can only happen AFTER all the functions called on the line

std::cout << foo( y, a, b ) << std::endl;

have been resolved.

So the process to executing the above line is:

1. Call foo (with y,a and b).
2. As a result of being called foo outputs the two values.
3. foo returns a value, in this case 4 because that was the contents of 'x'.
4. The result is now used in place of the function call, like this: std::cout << 4 << std::endl;
5. So the std::cout is called with 4 (and then a new line). So it outputs 4.

Share this post


Link to post
Share on other sites
First, did you look at my other examples?

Second
std::cout << foo( 4, 6, 9 ) << std::endl;
is saying "output to the cout the value of foo( 4, 6, 9 )". So what is the "value" of foo()? If you look inside foo() there is a statement "return x;". That sets the value of foo() to whatever was in x.
When you called foo(4,6,9) you set the value of x inside foo to be 4. So, you have "return 4;". So foo() inside main becomes "4", and 4 outputs to the screen.


edit:
empirical2, sorry for the cross-talk. we seem to be typing at the same time....

Share this post


Link to post
Share on other sites
I looked at all the examples and I fully understand the concept of parameters now. But now I don't understand why when I compile this code, nothing happens.
#include<iostream>
using namespace std;


int greencrud(int crud_days, int amount_crud, int numberof_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()
{
int numberofcrud2 = 0;
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(crud_days, amount_crud, numberofcrud2);
return 0;
}









More specificall, I enter in two numbers and no value is returned..

Share this post


Link to post
Share on other sites
Thats because (I think) your do...while loop has a problem where the condition required to leave it can, in many cases, never be met. So it stays in the loop forever.

Share this post


Link to post
Share on other sites
Quote:
Original post by empirical2
Thats because (I think) your do...while loop has a problem where the condition required to leave it can, in many cases, never be met. So it stays in the loop forever.


Then there is absolutely no way I can make the program add every 5 days..This is impossible

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement