I just posted this broken. Here is the code that works. I was told there is 2 ways this can be written more efficient. I can't see it, can you?

#include <iostream>
#include <string>
using namespace std;
void daysOfChristmas (int days, char switcher, string b[12]){(days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false; 
    if (days > 0) { daysOfChristmas(days - 1, 'n', b);
    cout << "on the " << days << " day of Christmas my true love gave me " << b[(days-1)] << endl;}}
int main() {string gifts [12] = {"a partridge in a Pear Tree", "two turtle doves", "three french hens", "four colly birds", "five golden rings", "six gees-a-laying", "seven swans-a-swimming", "eight maids a miliking", "nine ladies dancing", "ten lords a leaping", "eleven pipiers piping", "drummers drumming"};
    daysOfChristmas(12, 'y', gifts);}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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

void daysOfChristmas (int days, char switcher, string b[12]){
	(days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false;

	if (days > 0) { 
		daysOfChristmas(days - 1, 'n', b);
		cout << "on the " << days << " day of Christmas my true love gave me " << b[(days-1)] << endl;
	}

}
int main() {
	string gifts [12] = {
		"a partridge in a Pear Tree", 
		 "two turtle doves", 
		"three french hens", 
		"four colly birds", 
		"five golden rings", 
		"six gees-a-laying", 
		"seven swans-a-swimming", 
		"eight maids a miliking", 
		"nine ladies dancing", 
		"ten lords a leaping", 
		"eleven pipiers piping", 
		"drummers drumming"
	};

	daysOfChristmas(12, 'y', gifts);
}

It appears that is the formatted code we were looking for. I compiled the code and all seems to be running without errors.

It looks like you are trying to use recursion. I tried, but I'm not sure if this is what you are looking for. Instead of continuously checking to see if 12 was reached by a boolean expression, or otherwise, y/n char variable, I just iterated the function, each time used a for loop to reduce the CURRENT DAY down to 1 and spammed cout for each string in the array. May not be the approach your professor was looking for.

It was good practice for the both of us. I just started learning recursive functions about 5 hours ago. Let me know if this is acceptable by your professor/teacher. Even PM if you don't want to respond on the open forums. I'm interested in your response. Thanks and good luck!

-Saith

/*
	Purpose: Recursion forum 344701
	Name: Saith
	Date: 2/3/11
*/
#include <iostream>
#include <string>
using namespace std;

/* OLD FORMAT, NOT USED IN PROGRAM, COMMENTED OUT

void daysOfChristmas (int days, char switcher, string b[12]){
	(days > 0 && switcher == 'y') ? daysOfChristmas(days - 1, 'y', b): false;

	if (days > 0) { 
		daysOfChristmas(days - 1, 'n', b);
		cout << "on the " << days << " day of Christmas my true love gave me " << b[(days-1)] << endl;
	}
}
*/   

void daysOfChristmas (int day, string b[12]){

	if(day <= 12){
		cout << "on the " << day << " day of Christmas my true love gave me " << b[day-1] << endl;
			if( day > 1)
				for(int index = day - 1; index > 0; index--){
					cout << b[(index - 1)] << endl;				
			}
		daysOfChristmas(day + 1, b);
	}
}

int main() {
	string gifts [12] = {
		"a partridge in a Pear Tree", 
		 "two turtle doves", 
		"three french hens", 
		"four colly birds", 
		"five golden rings", 
		"six gees-a-laying", 
		"seven swans-a-swimming", 
		"eight maids a miliking", 
		"nine ladies dancing", 
		"ten lords a leaping", 
		"eleven pipiers piping", 
		"twelve drummers drumming"
	};
	daysOfChristmas(1, gifts);
}

>> I was told there is 2 ways this can be written more efficient.

I imagine that if one was looking for execution time efficiency you wouldn't bother with recursion at all and just use a nested loop in main. The way to get a real feel for it is to make the array way longer, like 365 days of Christmas. It's short enough that even inefficient code is going to execute pretty fast.

It might be interesting to play around with. My first thought was there was a pass by reference versus value and a const issue, but since it's an array, does it maybe not matter? char versus bool on the switcher variable?

It looks like we're developing one hell of a big call stack. Just avoid recursion altogether.

I'm not sure if I'm misinterpreting your question, VernonDozier. The array isn't really passed to the function, really a pointer to the first location of the array; technically, it's not pass by value or pass by reference. Arrays are funny like that.

But yes, overall, I'm very new to recursion myself. Seems like a sketchy set up as is.

Any array is just a pointer to the first element of the array, and when you dereference and element in the array with the brackets like arr[i]; it is the exact same thing as *(arr + i); The amount of elements in the array just means that the pointer is pointing to more data, that is it.

So the array is really a pass-by-value, but all the elements are dereferenced from the same address as that was used to initialize the array in the calling stack frame, so the changes made to the array elements are consistent.

>> But yes, overall, I'm very new to recursion myself


I'm not new to recursion, but I had a definite newbie question there.:$ "const" shouldn't matter here.

Edited 5 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.