using recursion to generate verses of Twelve Days of Christmas

Im trying to make a program that generates all the verses of the song "The Twelve Days of Christmas" using recursion (its homework). Ive read what the book says several times, researched online, etc etc. Im not looking for anyone to do the homework for me...I just need some guidance. Im really lost on this and any help would be appreciated.

I cant use for loops or while loops.
I can only have lines from each verse appear only once in the code.
I'm surprised that ANY teacher would give you homework to do that uses recursion.

I recommend that you create a function that takes a counter from outside the function (it's irrelevant how you do this, so long as the counter isn't reset per recursion and you can increment it). Have a chain of if statements (depending on how you do this, it's critical that you use no else statements, do you see why?) that each print one verse, and one final if statement that if true calls the function from within itself (again, do you see why we need an if statement around the recursion bit?). I'll leave you to determine the conditions for the if chain. Hint: the last one should use a less than operator.

EDIT: Welcome to the forum!

-Albatross
Last edited on
I don't know if this is what you want but I hope it helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

void foo(int i, int len, string* arr) {
    if(i==len) return;
    cout << arr[i] << endl;
    foo(++i, len, arr);
}

int main() {

    string words[] = {"hello", "world", "c++", "java"};
    foo(0, 4, words);
    return 0;
}
Thanks guys for the help!
Topic archived. No new replies allowed.