Help - bottles of beer


#1

Just logged into this system. 59 year old kid trying his best to learn iOS programming … I have taken several courses on Lynda.com and read several books, to no avail … I get stuck in my brain.

I am working the program bottles of beer … I get it to work fine, but have no earthly idea why the function counts back up again.

printf(“Put the bottle in the recycling bin, %d empty bottles in the bin.\n”, numberOfBottles); , it goes back up, but I for the life of me I cannot figure out why …

MauiMano … thanks for any explanation …


#2

If I were you I would not worry about it too much. However, you should make sure that you understand the concept of recursion and how it works in a program. If you can write a program on your own, using recursion, that adds up all the numbers from 0 to N, you are pretty much done.


#3

The “numbers” from 0 to N? I was not aware N was a number … please explain …


#4

MauiMano, you might read through some of the other forums; here’s a sample thread: viewtopic.php?f=137&t=3200

If it helps, you might think of recursion like a storyteller who likes to take tangents.

Imagine that the story starts out describing two buddies out hunting when one of them trips over a piece of trash, and that incident sends the storyteller into a story about the clumsiest dog he ever knew, which turns into a story about watching To Kill a Mockingbird as a kid.

Now, after the last story wraps up, the storyteller returns to the clumsy dog, and when he’s done with the dog, we find ourselves back in the woods with the hunting buddies. (Anyone who has listened to enough Garrison Keillor should be familiar with the “Oh yeah, that’s where we started this story” revelation.)

That process of stepping deeper and deeper into a sequence of stories, and then unwinding the stories in reverse order, is very much akin to recursion. Each function call is suspended while the next executes, but eventually the earlier ones have to be finished.

(The “0 to N” comment by ibex10 is just a way of describing an arbitrary set of numbers starting at 0.)