HyperSecret
2010-08-27 13:58:59
If you follow the link below it is the assignment I have to do.
http://staffwww.fullcoll.edu/sedwards/CS133/Announce/Announce.html#HW02_Rabbits
Rabbit (n) = (1 if n is 1 or 2) OR [Rabbit(n-1) + Rabbit(n-2); if n > 2]
The book gives this for an example code:
If you read the assignment you will see that I need to track when each 'n' enters and leaves the stack and what values are returned when they leave the stack.
I can't get the numbers 3+ to show 'Leaving Rabbit: n = 3 value = 2' etc…
I also can't figure out how to indent appropriately for the output…i know "\t" to tab but I don't know how to write something to figure out when and how many times…
my code:
any help will be appreciated.
http://staffwww.fullcoll.edu/sedwards/CS133/Announce/Announce.html#HW02_Rabbits
Rabbit (n) = (1 if n is 1 or 2) OR [Rabbit(n-1) + Rabbit(n-2); if n > 2]
The book gives this for an example code:
int rabbit(int n) {
if (n <= 2)
return 1;
else
return rabbit(n-1) + rabbit(n-2);
}If you read the assignment you will see that I need to track when each 'n' enters and leaves the stack and what values are returned when they leave the stack.
I can't get the numbers 3+ to show 'Leaving Rabbit: n = 3 value = 2' etc…
I also can't figure out how to indent appropriately for the output…i know "\t" to tab but I don't know how to write something to figure out when and how many times…
my code:
int rabbit(int n) {
cout << "Enter Rabbit: n = " << n << endl;
if (n <= 2) {
cout << "Leave Rabbit: n = << n << " value = 1" << endl;
return 1;
} else {
return rabbit(n-1) + rabbit(n-2);
}
}any help will be appreciated.