2 Replies - 1075 Views - Last Post: 06 June 2014 - 04:37 PM Rate Topic: -----

#1 DJOpus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 42
  • 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...
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1