I'd want to ask you a question concerning recursion in the C programming language. I'm reading a c book and came across some code that I didn't fully grasp.
first questions:
void singTheSong(int numberOfBottles)
{
if (numberOfBottles == 0)
{
printf("There are simply no more bottles of beer on the wall.\n");
}
else
{
printf("%d bottles of beer on the wall. %d bottles of beer.\n",
numberOfBottles, numberOfBottles);
int oneFewer = numberOfBottles - 1;
printf("Take one down, pass it around, %d bottles of beer on the wall.\n",
oneFewer);
singTheSong(oneFewer); // This function calls itself!
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
}
}
int main(int argc, const char * argv[])
{
singTheSong(99);
return 0;
}
So this is meant to be a song. I understand the if clause. I'm not sure how the number goes from 0 to 98.
second question:
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);
Why isn't it increasing to 99? It comes to an end at 98. Please explain in basic English, as I do not speak English as my first language (some difficulties)
According to one of the article I read, everything before singTheSong() is run, and then the recursive call occurs, creating a new stack frame. There will be no recycling print statements until numberOfBottles reaches 0.
Dhanapal ChandranPosted Mar 15, 2023, 9:22 AM
Got the problem, please find the explanation in the following link https://www.google.com/amp/s/takeuforward.org/data-structure/print-n-to-1-and-1-to-n-using-recursion/%3Famp=1
Printed output
/tmp/BmyDsKtoob.o
Hello world99 bottles of beer on the wall. 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall. 98 bottles of beer.
Take one down, pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall. 97 bottles of beer.
Take one down, pass it around, 96 bottles of beer on the wall.
96 bottles of beer on the wall. 96 bottles of beer.
Take one down, pass it around, 95 bottles of beer on the wall.
95 bottles of beer on the wall. 95 bottles of beer.
Take one down, pass it around, 94 bottles of beer on the wall.
94 bottles of beer on the wall. 94 bottles of beer.
Take one down, pass it around, 93 bottles of beer on the wall.
93 bottles of beer on the wall. 93 bottles of beer.
Take one down, pass it around, 92 bottles of beer on the wall.
92 bottles of beer on the wall. 92 bottles of beer.
Take one down, pass it around, 91 bottles of beer on the wall.
91 bottles of beer on the wall. 91 bottles of beer.
Take one down, pass it around, 90 bottles of beer on the wall.
90 bottles of beer on the wall. 90 bottles of beer.
Take one down, pass it around, 89 bottles of beer on the wall.
89 bottles of beer on the wall. 89 bottles of beer.
Take one down, pass it around, 88 bottles of beer on the wall.
88 bottles of beer on the wall. 88 bottles of beer.
Take one down, pass it around, 87 bottles of beer on the wall.
87 bottles of beer on the wall. 87 bottles of beer.
Take one down, pass it around, 86 bottles of beer on the wall.
86 bottles of beer on the wall. 86 bottles of beer.
Take one down, pass it around, 85 bottles of beer on the wall.
85 bottles of beer on the wall. 85 bottles of beer.
Take one down, pass it around, 84 bottles of beer on the wall.
84 bottles of beer on the wall. 84 bottles of beer.
Take one down, pass it around, 83 bottles of beer on the wall.
83 bottles of beer on the wall. 83 bottles of beer.
Take one down, pass it around, 82 bottles of beer on the wall.
82 bottles of beer on the wall. 82 bottles of beer.
Take one down, pass it around, 81 bottles of beer on the wall.
81 bottles of beer on the wall. 81 bottles of beer.
Take one down, pass it around, 80 bottles of beer on the wall.
80 bottles of beer on the wall. 80 bottles of beer.
Take one down, pass it around, 79 bottles of beer on the wall.
79 bottles of beer on the wall. 79 bottles of beer.
Take one down, pass it around, 78 bottles of beer on the wall.
78 bottles of beer on the wall. 78 bottles of beer.
Take one down, pass it around, 77 bottles of beer on the wall.
77 bottles of beer on the wall. 77 bottles of beer.
Take one down, pass it around, 76 bottles of beer on the wall.
76 bottles of beer on the wall. 76 bottles of beer.
Take one down, pass it around, 75 bottles of beer on the wall.
75 bottles of beer on the wall. 75 bottles of beer.
Take one down, pass it around, 74 bottles of beer on the wall.
74 bottles of beer on the wall. 74 bottles of beer.
Take one down, pass it around, 73 bottles of beer on the wall.
73 bottles of beer on the wall. 73 bottles of beer.
Take one down, pass it around, 72 bottles of beer on the wall.
72 bottles of beer on the wall. 72 bottles of beer.
Take one down, pass it around, 71 bottles of beer on the wall.
71 bottles of beer on the wall. 71 bottles of beer.
Take one down, pass it around, 70 bottles of beer on the wall.
70 bottles of beer on the wall. 70 bottles of beer.
Take one down, pass it around, 69 bottles of beer on the wall.
69 bottles of beer on the wall. 69 bottles of beer.
Take one down, pass it around, 68 bottles of beer on the wall.
68 bottles of beer on the wall. 68 bottles of beer.
Take one down, pass it around, 67 bottles of beer on the wall.
67 bottles of beer on the wall. 67 bottles of beer.
Take one down, pass it around, 66 bottles of beer on the wall.
66 bottles of beer on the wall. 66 bottles of beer.
Take one down, pass it around, 65 bottles of beer on the wall.
65 bottles of beer on the wall. 65 bottles of beer.
Take one down, pass it around, 64 bottles of beer on the wall.
64 bottles of beer on the wall. 64 bottles of beer.
Take one down, pass it around, 63 bottles of beer on the wall.
63 bottles of beer on the wall. 63 bottles of beer.
Take one down, pass it around, 62 bottles of beer on the wall.
62 bottles of beer on the wall. 62 bottles of beer.
Take one down, pass it around, 61 bottles of beer on the wall.
61 bottles of beer on the wall. 61 bottles of beer.
Take one down, pass it around, 60 bottles of beer on the wall.
60 bottles of beer on the wall. 60 bottles of beer.
Take one down, pass it around, 59 bottles of beer on the wall.
59 bottles of beer on the wall. 59 bottles of beer.
Take one down, pass it around, 58 bottles of beer on the wall.
58 bottles of beer on the wall. 58 bottles of beer.
Take one down, pass it around, 57 bottles of beer on the wall.
57 bottles of beer on the wall. 57 bottles of beer.
Take one down, pass it around, 56 bottles of beer on the wall.
56 bottles of beer on the wall. 56 bottles of beer.
Take one down, pass it around, 55 bottles of beer on the wall.
55 bottles of beer on the wall. 55 bottles of beer.
Take one down, pass it around, 54 bottles of beer on the wall.
54 bottles of beer on the wall. 54 bottles of beer.
Take one down, pass it around, 53 bottles of beer on the wall.
53 bottles of beer on the wall. 53 bottles of beer.
Take one down, pass it around, 52 bottles of beer on the wall.
52 bottles of beer on the wall. 52 bottles of beer.
Take one down, pass it around, 51 bottles of beer on the wall.
51 bottles of beer on the wall. 51 bottles of beer.
Take one down, pass it around, 50 bottles of beer on the wall.
50 bottles of beer on the wall. 50 bottles of beer.
Take one down, pass it around, 49 bottles of beer on the wall.
49 bottles of beer on the wall. 49 bottles of beer.
Take one down, pass it around, 48 bottles of beer on the wall.
48 bottles of beer on the wall. 48 bottles of beer.
Take one down, pass it around, 47 bottles of beer on the wall.
47 bottles of beer on the wall. 47 bottles of beer.
Take one down, pass it around, 46 bottles of beer on the wall.
46 bottles of beer on the wall. 46 bottles of beer.
Take one down, pass it around, 45 bottles of beer on the wall.
45 bottles of beer on the wall. 45 bottles of beer.
Take one down, pass it around, 44 bottles of beer on the wall.
44 bottles of beer on the wall. 44 bottles of beer.
Take one down, pass it around, 43 bottles of beer on the wall.
43 bottles of beer on the wall. 43 bottles of beer.
Take one down, pass it around, 42 bottles of beer on the wall.
42 bottles of beer on the wall. 42 bottles of beer.
Take one down, pass it around, 41 bottles of beer on the wall.
41 bottles of beer on the wall. 41 bottles of beer.
Take one down, pass it around, 40 bottles of beer on the wall.
40 bottles of beer on the wall. 40 bottles of beer.
Take one down, pass it around, 39 bottles of beer on the wall.
39 bottles of beer on the wall. 39 bottles of beer.
Take one down, pass it around, 38 bottles of beer on the wall.
38 bottles of beer on the wall. 38 bottles of beer.
Take one down, pass it around, 37 bottles of beer on the wall.
37 bottles of beer on the wall. 37 bottles of beer.
Take one down, pass it around, 36 bottles of beer on the wall.
36 bottles of beer on the wall. 36 bottles of beer.
Take one down, pass it around, 35 bottles of beer on the wall.
35 bottles of beer on the wall. 35 bottles of beer.
Take one down, pass it around, 34 bottles of beer on the wall.
34 bottles of beer on the wall. 34 bottles of beer.
Take one down, pass it around, 33 bottles of beer on the wall.
33 bottles of beer on the wall. 33 bottles of beer.
Take one down, pass it around, 32 bottles of beer on the wall.
32 bottles of beer on the wall. 32 bottles of beer.
Take one down, pass it around, 31 bottles of beer on the wall.
31 bottles of beer on the wall. 31 bottles of beer.
Take one down, pass it around, 30 bottles of beer on the wall.
30 bottles of beer on the wall. 30 bottles of beer.
Take one down, pass it around, 29 bottles of beer on the wall.
29 bottles of beer on the wall. 29 bottles of beer.
Take one down, pass it around, 28 bottles of beer on the wall.
28 bottles of beer on the wall. 28 bottles of beer.
Take one down, pass it around, 27 bottles of beer on the wall.
27 bottles of beer on the wall. 27 bottles of beer.
Take one down, pass it around, 26 bottles of beer on the wall.
26 bottles of beer on the wall. 26 bottles of beer.
Take one down, pass it around, 25 bottles of beer on the wall.
25 bottles of beer on the wall. 25 bottles of beer.
Take one down, pass it around, 24 bottles of beer on the wall.
24 bottles of beer on the wall. 24 bottles of beer.
Take one down, pass it around, 23 bottles of beer on the wall.
23 bottles of beer on the wall. 23 bottles of beer.
Take one down, pass it around, 22 bottles of beer on the wall.
22 bottles of beer on the wall. 22 bottles of beer.
Take one down, pass it around, 21 bottles of beer on the wall.
21 bottles of beer on the wall. 21 bottles of beer.
Take one down, pass it around, 20 bottles of beer on the wall.
20 bottles of beer on the wall. 20 bottles of beer.
Take one down, pass it around, 19 bottles of beer on the wall.
19 bottles of beer on the wall. 19 bottles of beer.
Take one down, pass it around, 18 bottles of beer on the wall.
18 bottles of beer on the wall. 18 bottles of beer.
Take one down, pass it around, 17 bottles of beer on the wall.
17 bottles of beer on the wall. 17 bottles of beer.
Take one down, pass it around, 16 bottles of beer on the wall.
16 bottles of beer on the wall. 16 bottles of beer.
Take one down, pass it around, 15 bottles of beer on the wall.
15 bottles of beer on the wall. 15 bottles of beer.
Take one down, pass it around, 14 bottles of beer on the wall.
14 bottles of beer on the wall. 14 bottles of beer.
Take one down, pass it around, 13 bottles of beer on the wall.
13 bottles of beer on the wall. 13 bottles of beer.
Take one down, pass it around, 12 bottles of beer on the wall.
12 bottles of beer on the wall. 12 bottles of beer.
Take one down, pass it around, 11 bottles of beer on the wall.
11 bottles of beer on the wall. 11 bottles of beer.
Take one down, pass it around, 10 bottles of beer on the wall.
10 bottles of beer on the wall. 10 bottles of beer.
Take one down, pass it around, 9 bottles of beer on the wall.
9 bottles of beer on the wall. 9 bottles of beer.
Take one down, pass it around, 8 bottles of beer on the wall.
8 bottles of beer on the wall. 8 bottles of beer.
Take one down, pass it around, 7 bottles of beer on the wall.
7 bottles of beer on the wall. 7 bottles of beer.
Take one down, pass it around, 6 bottles of beer on the wall.
6 bottles of beer on the wall. 6 bottles of beer.
Take one down, pass it around, 5 bottles of beer on the wall.
5 bottles of beer on the wall. 5 bottles of beer.
Take one down, pass it around, 4 bottles of beer on the wall.
4 bottles of beer on the wall. 4 bottles of beer.
Take one down, pass it around, 3 bottles of beer on the wall.
3 bottles of beer on the wall. 3 bottles of beer.
Take one down, pass it around, 2 bottles of beer on the wall.
2 bottles of beer on the wall. 2 bottles of beer.
Take one down, pass it around, 1 bottles of beer on the wall.
1 bottles of beer on the wall. 1 bottles of beer.
Take one down, pass it around, 0 bottles of beer on the wall.
There are simply no more bottles of beer on the wall.
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.
Put a bottle in the recycling, 5 empty bottles in the bin.
Put a bottle in the recycling, 6 empty bottles in the bin.
Put a bottle in the recycling, 7 empty bottles in the bin.
Put a bottle in the recycling, 8 empty bottles in the bin.
Put a bottle in the recycling, 9 empty bottles in the bin.
Put a bottle in the recycling, 10 empty bottles in the bin.
Put a bottle in the recycling, 11 empty bottles in the bin.
Put a bottle in the recycling, 12 empty bottles in the bin.
Put a bottle in the recycling, 13 empty bottles in the bin.
Put a bottle in the recycling, 14 empty bottles in the bin.
Put a bottle in the recycling, 15 empty bottles in the bin.
Put a bottle in the recycling, 16 empty bottles in the bin.
Put a bottle in the recycling, 17 empty bottles in the bin.
Put a bottle in the recycling, 18 empty bottles in the bin.
Put a bottle in the recycling, 19 empty bottles in the bin.
Put a bottle in the recycling, 20 empty bottles in the bin.
Put a bottle in the recycling, 21 empty bottles in the bin.
Put a bottle in the recycling, 22 empty bottles in the bin.
Put a bottle in the recycling, 23 empty bottles in the bin.
Put a bottle in the recycling, 24 empty bottles in the bin.
Put a bottle in the recycling, 25 empty bottles in the bin.
Put a bottle in the recycling, 26 empty bottles in the bin.
Put a bottle in the recycling, 27 empty bottles in the bin.
Put a bottle in the recycling, 28 empty bottles in the bin.
Put a bottle in the recycling, 29 empty bottles in the bin.
Put a bottle in the recycling, 30 empty bottles in the bin.
Put a bottle in the recycling, 31 empty bottles in the bin.
Put a bottle in the recycling, 32 empty bottles in the bin.
Put a bottle in the recycling, 33 empty bottles in the bin.
Put a bottle in the recycling, 34 empty bottles in the bin.
Put a bottle in the recycling, 35 empty bottles in the bin.
Put a bottle in the recycling, 36 empty bottles in the bin.
Put a bottle in the recycling, 37 empty bottles in the bin.
Put a bottle in the recycling, 38 empty bottles in the bin.
Put a bottle in the recycling, 39 empty bottles in the bin.
Put a bottle in the recycling, 40 empty bottles in the bin.
Put a bottle in the recycling, 41 empty bottles in the bin.
Put a bottle in the recycling, 42 empty bottles in the bin.
Put a bottle in the recycling, 43 empty bottles in the bin.
Put a bottle in the recycling, 44 empty bottles in the bin.
Put a bottle in the recycling, 45 empty bottles in the bin.
Put a bottle in the recycling, 46 empty bottles in the bin.
Put a bottle in the recycling, 47 empty bottles in the bin.
Put a bottle in the recycling, 48 empty bottles in the bin.
Put a bottle in the recycling, 49 empty bottles in the bin.
Put a bottle in the recycling, 50 empty bottles in the bin.
Put a bottle in the recycling, 51 empty bottles in the bin.
Put a bottle in the recycling, 52 empty bottles in the bin.
Put a bottle in the recycling, 53 empty bottles in the bin.
Put a bottle in the recycling, 54 empty bottles in the bin.
Put a bottle in the recycling, 55 empty bottles in the bin.
Put a bottle in the recycling, 56 empty bottles in the bin.
Put a bottle in the recycling, 57 empty bottles in the bin.
Put a bottle in the recycling, 58 empty bottles in the bin.
Put a bottle in the recycling, 59 empty bottles in the bin.
Put a bottle in the recycling, 60 empty bottles in the bin.
Put a bottle in the recycling, 61 empty bottles in the bin.
Put a bottle in the recycling, 62 empty bottles in the bin.
Put a bottle in the recycling, 63 empty bottles in the bin.
Put a bottle in the recycling, 64 empty bottles in the bin.
Put a bottle in the recycling, 65 empty bottles in the bin.
Put a bottle in the recycling, 66 empty bottles in the bin.
Put a bottle in the recycling, 67 empty bottles in the bin.
Put a bottle in the recycling, 68 empty bottles in the bin.
Put a bottle in the recycling, 69 empty bottles in the bin.
Put a bottle in the recycling, 70 empty bottles in the bin.
Put a bottle in the recycling, 71 empty bottles in the bin.
Put a bottle in the recycling, 72 empty bottles in the bin.
Put a bottlein the recycling, 73 empty bottles in the bin.
Put a bottle in the recycling, 74 empty bottles in the bin.
Put a bottle in the recycling, 75 empty bottles in the bin.
Put a bottle in the recycling, 76 empty bottles in the bin.
Put a bottle in the recycling, 77 empty bottles in the bin.
Put a bottle in the recycling, 78 empty bottles in the bin.
Put a bottle in the recycling, 79 empty bottles in the bin.
Put a bottle in the recycling, 80 empty bottles in the bin.
Put a bottle in the recycling, 81 empty bottles in the bin.
Put a bottle in the recycling, 82 empty bottles in the bin.
Put a bottle in the recycling, 83 empty bottles in the bin.
Put a bottle in the recycling, 84 empty bottles in the bin.
Put a bottle in the recycling, 85 empty bottles in the bin.
Put a bottle in the recycling, 86 empty bottles in the bin.
Put a bottle in the recycling, 87 empty bottles in the bin.
Put a bottle in the recycling, 88 empty bottles in the bin.
Put a bottle in the recycling, 89 empty bottles in the bin.
Put a bottle in the recycling, 90 empty bottles in the bin.
Put a bottle in the recycling, 91 empty bottles in the bin.
Put a bottle in the recycling, 92 empty bottles in the bin.
Put a bottle in the recycling, 93 empty bottles in the bin.
Put a bottle in the recycling, 94 empty bottles in the bin.
Put a bottle in the recycling, 95 empty bottles in the bin.
Put a bottle in the recycling, 96 empty bottles in the bin.
Put a bottle in the recycling, 97 empty bottles in the bin.
Put a bottle in the recycling, 98 empty bottles in the bin.
Put a bottle in the recycling, 99 empty bottles in the bin.
horald HenryPosted Mar 15, 2023, 7:26 AM
I don't understand. It's increasing from 0 to 98 and you're saying it decreases. So it can be done like this example?
int oneFewer = numberOfBottles - 1;Dhanapal ChandranPosted Mar 13, 2023, 5:24 PM
This is recursive call happening until it reaches count o. This code is not reachable as per logic
printf("Put a bottle in the recycling, %d empty bottles in the bin.\n",
numberOfBottles);.
So it start from 98 and print till 0