Arrays, Recursion and the 12 days of christmas

Page 1 of 1

2 Replies - 2867 Views - Last Post: 06 June 2014 - 04:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=348247&amp;s=139d25934b43ff01739b6c7bd101b8b9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 DJOpus

Reputation: 0
• Posts: 1
• Joined: 06-June 14

Arrays, Recursion and the 12 days of christmas

Posted 06 June 2014 - 11:50 AM

Thanks for taking some time to help me out here. I'm a hands on learner and I'm trying the best I can to understand what is being taught. As you can see from the topic, we have an assignment to produce the 12 days of christmas with only using recursion. I think I have an ok idea but it's still not producing what I need, any help or suggestions would be great!

Thanks,

```#include <iostream>
using namespace std;

int twelveDays(int firstDay, int lastDay, int endOf)
{
if(firstDay <= lastDay)
{
return twelveDays(++firstDay, lastDay, endOf + 1);
}
else
{
return twelveDays(++firstDay, lastDay, endOf);
}
return endOf;
}

int countDays(int firstDay, int lastDay)
{
return twelveDays(firstDay, lastDay, 0);
};

int main()
{
int Days[] = {1,2,3,4,5,6,7,8,9,10,11,12};
const char* Verses[] = {"A Partridge in a Pear Tree","Two Turtle Doves",
"Three French Hens","Four Calling Birds",
"Five Golden Rings","Six Geese a Laying",
"Seven Swans a Swimming","Eight Maids a Milking",
"Nine Ladies Dancing","Ten Lords a Leaping",
"Eleven Pipers Piping","12 Drummers Drumming"};

cout << "On the " << countDays << " of Christmas my true love gave to me " << twelveDays << endl;
cout << countDays << endl;

system("pause");
return 0;
}

```

I have a slight learning disability so the more simple you can put it, the better. I'm not getting any error after the build and running it, but it's only producing one like and it's giving me random numbers in the banks of mem it's pulling from (is my guess).

Thanks.

Is This A Good Question/Topic? 0

Replies To: Arrays, Recursion and the 12 days of christmas

#2 fryishone

Reputation: 18
• Posts: 75
• Joined: 09-May 14

Re: Arrays, Recursion and the 12 days of christmas

Posted 06 June 2014 - 03:31 PM

I would probably focus on a small portion of the recursive program instead of completing what you think might be close to what you are looking for a posting for help. I would start with recursively printing an array containing 12 items...

#3 jimblumberg

Reputation: 4647
• Posts: 14,572
• Joined: 25-December 09

Re: Arrays, Recursion and the 12 days of christmas

Posted 06 June 2014 - 04:37 PM

Quote

I'm not getting any error after the build and running it,

Then you need to increase your compiler warning level and never ever ignore warnings. Warnings should be treated like errors until you are much much more familiar with the language. Look at the warnings and errors my compiler complains about with your code.

Quote

main.cpp||In function ‘int twelveDays(int, int, int)’:|
main.cpp|63|warning: no previous declaration for ‘int twelveDays(int, int, int)’ [-Wmissing-declarations]|
main.cpp||In function ‘int countDays(int, int)’:|
main.cpp|77|warning: no previous declaration for ‘int countDays(int, int)’ [-Wmissing-declarations]|
main.cpp|80|error: extra ‘;’ [-Wpedantic]|
main.cpp||In function ‘int main()’:|
main.cpp|92|warning: the address of ‘int countDays(int, int)’ will always evaluate as ‘true’ [-Waddress]|
main.cpp|92|warning: the address of ‘int twelveDays(int, int, int)’ will always evaluate as ‘true’ [-Waddress]|
main.cpp|93|warning: the address of ‘int countDays(int, int)’ will always evaluate as ‘true’ [-Waddress]|
main.cpp|97|error: ‘system’ was not declared in this scope|
main.cpp|84|warning: unused variable ‘Days’ [-Wunused-variable]|
main.cpp|85|warning: unused variable ‘Verses’ [-Wunused-variable]|
||=== Build finished: 2 errors, 7 warnings (0 minutes, 0 seconds) ===|

These warnings are probably a big part of your problem, especially the ones dealing with lines 92 and 93. You may want to read the function tutorials contained in my signature.

Jim

This post has been edited by jimblumberg: 06 June 2014 - 04:37 PM