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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//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 <iostream>

using std::cout;
using std::endl;

#include <string>

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

using std::cout;
using std::endl;

#include <string>

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

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:
1
2
    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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
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
Registered users can post here. Sign in or register to post.