• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bottles of beer sone using recursion  RSS feed

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am writing this program to do the song "99 bottles of beer on the wall". So far it works, but it does not get past 97 bottles, why is that? Here is my code:



And the output of it is:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There is no recursion anywhere in the program.

It is just a single method call. And in the method, there are three conditionals, of which two are satisfied.

Henry
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are printing it once for 99 bottles, then testing whether you are over 0, which you are, then reducing the number by 1, but I can see no recursion or other form of repetition.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I was following some notes I was given and thought this was formatted as recursion. Where would the recursion go?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by putting an option in your print method for what happens if you get to 0. Then one for 1, and finish with options for > 1. You will have something in the print(n) method saying print(n − 1).
Remember that recursion means calling the same method again, so the recursive call usually goes in the method you want to recurse.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Start by putting an option in your print method for what happens if you get to 0. Then one for 1, and finish with options for > 1. You will have something in the print(n) method saying print(n − 1).


I did all that and where I have the code for (b>1) I put b-1 at the end of the print. Why doesn't that work?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . . I put b-1 at the end of the print. Why doesn't that work?
You are supposed to use n − 1 as an argument to call the method. Look what I said in my previous post.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are supposed to use n − 1 as an argument to call the method. Look what I said in my previous post.


So I am supposed to put all "n's" in my bottlesofbeer method even though it has int b?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Side note. So, feel free to ignore ... two points. One. Recursion is definitely not a beginner's topic. In fact, I can even say that we have developers today, who graduated (and even certified), who don't understand recursion all that well. And two. Recursion is about the algorithm. Discussion regarding recursion is generally a high level discussion, about the "termination condition" and how each recursive call, works towards that condition.

This discussion comes off as getting to recursion, without actually understanding it -- meaning I think Campbell has a recursive solution in mind, and with enough hints, the OP might get there, but none of the basic discussions regarding recursion is in this topic.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Side note. So, feel free to ignore ... two points. One. Recursion is definitely not a beginner's topic. In fact, I can even say that we have developers today, who graduated (and even certified), who don't understand recursion all that well. And two. Recursion is about the algorithm. Discussion regarding recursion is generally a high level discussion, about the "termination condition" and how each recursive call, works towards that condition.

This discussion comes off as getting to recursion, without actually understanding it -- meaning I think Campbell has a recursive solution in mind, and with in enough hints, the OP might get there, but none of the basic discussions regarding recursion is in this topic.

Henry


How come what we are talking about is not regarding recursion? Is this because recursion is about the algorithm?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:
How come what we are talking about is not regarding recursion? Is this because recursion is about the algorithm?


I didn't say this topic is not about recursion. As any recursive call is, by definition, about recursion.

And yes, if you want to learn recursion, it is more about understanding the algorthm, than setting variables in a particular way.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is my project not working when I am calling the variables over and over but with different circumstances. Ex: if (n == 0), or if (n == 1)
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you call methods, not variables.  And I don't see where you are calling anything over and over.

But back to recursion.

Recursion, from a very basic point of view, can be seen as a method calling itself.  So the first thing you have to do is write a line that will stop the recursion.  That often done with an if statement and a return.  Next, you do what you need to do for the "normal" situation.  Then you call the method itself with one less.  This is only one specific form or recursion but it will work for your situation.

So the basic shell is:

The question marks you should fill in.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Well, you call methods, not variables.  And I don't see where you are calling anything over and over.

But back to recursion.

Recursion, from a very basic point of view, can be seen as a method calling itself.  So the first thing you have to do is write a line that will stop the recursion.  That often done with an if statement and a return.  Next, you do what you need to do for the "normal" situation.  Then you call the method itself with one less.  This is only one specific form or recursion but it will work for your situation.

So the basic shell is:

The question marks you should fill in.


The only thing I was missing was to call the method and subtract one with it, and that is considered recursion? Thank you.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you, yes, but I'd like to see your code.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . . call the method and subtract one with it, and that is considered recursion? Thank you.
Yes. But make sure not to call the method again from n == 0.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have got the recursive call before the print instruction. You will need a recursive call when (n == 1) too.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You have got the recursive call before the print instruction. You will need a recursive call when (n == 1) too.


Oh okay. My problem has been solved, thank you to everyone!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lexi Turgeon wrote:. . . thank you to everyone!
That's a pleasure Please click the resolved button if you think it is resolved.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!