### consults on interpretation of the problem

i'd like to know if I have managed solve the problem regarding to the wording
On the other hand, to comment how it would be optimized this solution or to be simplified in order to get different solutions can be given to it.

thanks for attending my question

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194`` ``````//Ejercicio 5.28; Instrucciones de control II /* 5.28 (“The Twelve Days of Christmas” Song) Write an application that uses iteration and statements to print the song “The Twelve Days of Christmas.” One statement should be used to print the day (“first,” “second,” and so on). A separate statement should be used to print the remainder of each verse. */ #include using std::cout; using std::endl; #include using std::string; int main(int argc, char** argv) { int days = 1; //SENTENCES string oracion1 = "A partridge in a pear tree"; string oracion2 = "Two turtle doves, and"; string oracion3 = "Three french hens"; string oracion4 = "Four calling birds"; string oracion5 = "Five golden rings"; string oracion6 = "Six geese a-laying"; string oracion7 = "Seven swans a-swimming"; string oracion8 = "Eight maids a-milking"; string oracion9 = "Nine ladies dancing"; string oracion10 = "Ten lords a-leaping"; string oracion11 = "Eleven pipers piping"; string oracion12 = "Twelve drummers drumming"; cout << "12 days of christmas...\n" << endl; while(days <= 12) { switch(days) { case 1: cout << "FIRST"; break; case 2: cout << "SECOND"; break; case 3: cout << "THIRD"; break; case 4: cout << "FOURTH"; break; case 5: cout << "FIFTH"; break; case 6: cout << "SIXTH"; break; case 7: cout << "SEVENTH"; break; case 8: cout << "EIGHTH"; break; case 9: cout << "NINTH"; break; case 10: cout << "TENTH"; break; case 11: cout << "ELEVENTH"; break; case 12: cout << "TWELFTH"; break; } cout << "\nmy true love sent to me " << endl; switch(days) { case 1: cout << oracion1 << endl; break; case 2: cout << oracion2 << endl; cout << oracion1 << endl; break; case 3: cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 4: cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 5: cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 6: cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 7: cout << oracion7 << endl; cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 8: cout << oracion8 << endl; cout << oracion7 << endl; cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 9: cout << oracion9 << endl; cout << oracion8 << endl; cout << oracion7 << endl; cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 10: cout << oracion10 << endl; cout << oracion9 << endl; cout << oracion8 << endl; cout << oracion7 << endl; cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 11: cout << oracion11 << endl; cout << oracion10 << endl; cout << oracion9 << endl; cout << oracion8 << endl; cout << oracion7 << endl; cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; case 12: cout << oracion12 << endl; cout << oracion11 << endl; cout << oracion10 << endl; cout << oracion9 << endl; cout << oracion8 << endl; cout << oracion7 << endl; cout << oracion6 << endl; cout << oracion5 << endl; cout << oracion4 << endl; cout << oracion3 << endl; cout << oracion2 << endl; cout << oracion1 << endl; break; } days++; cout << endl; } return 0; } ``````
You can simplify this immensely by using two arrays:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include using std::cout; using std::endl; #include using std::string; string sentences[] = { "", // Not used "A partridge in a pear tree", "Two turtle doves, and", "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", "Twelve drummers drumming" }; string daynames[] = { "", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Nineth", "Tenth", "Eleventh", "Twelveth" }; int main(int argc, char** argv) { int days = 1; cout << "12 days of christmas...\n" << endl; while (days <= 12) { cout << "On the " << daynames[days] << " day of Christmas, my true love gave to me" << endl; for (int i = days; i >= 1; i--) cout << sentences[i] << endl; days++; cout << endl; } system("pause"); return 0; }``````
This is also an example where you can use the fact that cases in a switch statement fall through to the next case. I wouldn't say that this is a better solution, but it's a good demonstration of this rarely used structure
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include using std::cout; using std::endl; const char * daynames[] = { "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Nineth", "Tenth", "Eleventh", "Twelveth" }; int main(int argc, char** argv) { cout << "12 days of christmas...\n"; for (int days=1; days<13; ++days) { cout << "On the " << daynames[days-1] << " day of Christmas, my true love gave to me\n"; switch (days) { case 12: cout << "Twelve drummers drumming\n"; case 11: cout << "Eleven pipers piping\n"; case 10: cout << "Ten lords a-leaping\n"; case 9: cout << "Nine ladies dancing\n"; case 8: cout << "Eight maids a-milking\n"; case 7: cout << "Seven swans a-swimming\n"; case 6: cout << "Six geese a-laying\n"; case 5: cout << "Five golden rings\n"; case 4: cout << "Four calling birds\n"; case 3: cout << "Three french hens\n"; case 2: cout << "Two turtle doves, and\n"; case 1: cout << "A partridge in a pear tree\n\n"; } } return 0; }``````

@dhayden, Nice one!

EDIT: case 1 could be:
 ``12`` `````` case 1: cout << (days > 1 ? "And a" : "A"); cout << " partridge in a pear tree\n\n";``````

EDIT 2: Whoops, I didn't see that you already had an "and" at the end of case 2.
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include using namespace std; string ordinal[] = { "", "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" }; string gift[] = { "", "A partridge in a pear tree." , "Two turtle doves and" , "Three french hens," , "Four calling birds," , "Five gold rings," , "Six geese a-laying," , "Seven swans a-swimming," , "Eight maids a-milking," , "Nine ladies dancing," , "Ten lords a-leaping," , "Eleven pipers piping," , "Twelve drummers drumming," }; string what( int i ) { return ( i ? gift[i] + "\n" + what( i - 1 ) : "\n" ); } string when( int i ) { return ( 13 - i ? "On the " + ordinal[i] + " day of Christmas my true love sent to me:\n" + what( i ) + when( i + 1 ) : "\n" ); } int main() { cout << when( 1 ); }``````
[quote ]AbstractionAnon (6225)[/quote]

beautiful way to solve, but not understand by myself arrays in depth nowadays, i understand it even, simple and neat, thank you sir.

 dhayden

nice and wonderful way to solve it, it's almost hard usual way, but simple to understand.

 lastChance

man, it's hard to understand, but it's a wonderful solution, simply a lot of code and it's advanced to get over it