BeerSong

#21

Hey everyone, I’m new on here and having just completed a intro to Java class over the summer i decided to tackle this book before the next semester starts. The explanation of the stacks of paper really helped me a lot!

Also cesarjardim, your else statement is closing out before the end. Try moving your “}” at the end of your else printf function to after the printf “Recycling” message. Thats the only difference i noticed just skimming through the code posted, i haven’t tested it the way you have it posted, but thats just an initial first thought. Hope it helps!

#22

Oh man thanks! this makes it so easy! ^^

[quote=“ibex10”]To visualise what’s happening, try the following exercise for: singSongFor (4).

Take a blank sheet of paper, and cut it into four equal pieces.

Start building a call stack:

``````numberOfBottles == 4
1. Take a blank piece and write the number 4 on it, and put it aside.
--> singSongFor (3)

numberOfBottles == 3
2. Take another blank piece and write the number 3 on it, and place it on top of number 4.
--> singSongFor (2)

numberOfBottles == 2
3. Take another blank piece and write the number 2 on it, and place it on top of number 3.
--> singSongFor (1)

numberOfBottles == 1
4. Take another blank piece and write the number 1 on it, and place it on top of number 2.
--> singSongFor (0)

numberOfBottles == 0``````

You have no more blank pieces left.
And you have a stack of 4 pieces numbered 1, 2, 3, and 4 - 1 at the top and 4 at the bottom.
This is akin to function calling itself four times.

Now start emptying the stack, by removing the pieces of paper one at a time:

``````numberOfBottles == 0
1. Remove the top piece (number 1) from the stack, you see the number 2.
--> return

numberOfBottles == 1
2. Remove the top piece (number 2) from the stack, you see the number 3.
--> return

numberOfBottles == 2
3. Remove the top piece (number 3) from the stack, you see the number 4.
--> return

numberOfBottles == 3
4. Remove the top piece (number 4) from the stack, the stack is empty now.
--> return

numberOfBottles == 4``````

This is akin to function returning four times.[/quote]

#23

Can someone tell me how the program knows to start at 100?

There’s no reference to it in the code. Why doesn’t it start at 1000 or 56,891?

If anything you’d think it was a negative number;

numberOfBottles == 0…
int oneFewer = numberOfBottles - 1; //0-1

Edit - Nevermind. Got it. Silly billy

#24

I don’t understand how

[quote]Put a bottle in the recycling, 1 empty bottles in the bin.
Put a bottle in the recycling, 2 empty bottles in the bin.
Put a bottle in the recycling, 3 empty bottles in the bin.
Put a bottle in the recycling, 4 empty bottles in the bin.[/quote]
gets printed when the code is suppose to terminate after printing:

Can someone please explain this to me?

#25

Understand the concept of stack and frame. There is a picture provided in the book that shows how frames are placed in a stack.
After the program prints “There are simply no more bottles of beer on the wall.”, the last frame pops out and for each frame left on the stack the following line will be executed :

``````  printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",numberOfBottles);
``````

as it wasn’t executed earlier because singSongFor(oneFewer) function was calling itself.

Happy Coding.

#26

Do stacks usually just work in order rather than a sequence of numbers.

I am having trouble grasping the concept of how the program starts counting down. The explanation I came up with is that it start stacking all the functions, and then when the recursion calls the original function, it taking the stacks off back down to the originating number.

#27

Just saying…

I’m a Chinese so I bought a Chinese version.
But the translator accidentally changed one word on this paragraph. He changed all the “singSongFor” into “singTheSong” but not the last one.
Just incase anyone else is confused by this…

#28

when I import “singSongFor(4)”, it shows: Implicit declaration of function ‘singSongFor’ is invalid in C99. WHY?