Objective : To develop an understanding of loops , their logic and the correct u
ID: 3831681 • Letter: O
Question
Objective: To develop an understanding of loops, their logic and the correct use of the while(), do .. while() and for control structures in the C++ programming language. Specifically to show an understanding of the logically appropriate use of nested loops and embedded conditional statements. To reinforce our understanding of data types and variables. Namely, how to create, initialize, display and perform basic arithmetic operations on those variables.
Assignment: Write a program in C++ that simulates the singing of the song The Twelve Days of Christmas.
Twelve Days of Christmas
On the first day of Christmas, my true love sent to me
A partridge in a pear tree.
On the second day of Christmas, my true love sent to me
Two turtle doves
And a partridge in a pear tree.
On the third day of Christmas, my true love sent to me
Three French hens, two turtle doves
And a partridge in a pear tree.
On the fourth day of Christmas, my true love sent to me
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the fifth day of Christmas, my true love sent to me
Five golden rings.
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the sixth day of Christmas, my true love gave to me
Six geese a-laying,
Five golden rings.
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the seventh day of Christmas, my true love gave to me
Seven swans a-swimming, six geese a-laying,
Five golden rings.
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the eighth day of Christmas, my true love gave to me
Eight maids a-milking, seven swans a-swimming, six geese a-laying,
Five golden rings.
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the ninth day of Christmas, my true love gave to me
Nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying,
Five golden rings.
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the tenth day of Christmas, my true love gave to me
Ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swi'mmi'ng, six geese a-laying,
Five golden rings.
Fou'r calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
On the eleventh day of Christmas, my true love gave to me
Eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming,
Six geese a-laying,
Five golden rings.
Four calling birds, three French hens, two turtle doves And a partridge in a pear tree.
On the twelfth day of Christmas, my true love gave to me
Twelve drummers drumming, eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying,
Five golden rings.
Four calling birds, three French hens, two turtle doves
And a partridge in a pear tree.
Approach: Consider the following approach to working out the logic of the loops:
Write a single loop, where the control variable represents the numeric day (i.e. 1-12) and the body of the loop determines which day is to be sung (i.e. first, second, third, etc.)
Write a second loop (initially independent of the first) where the control variable represents the gift that is to be presented on a specific day.
Consider how the second loop can be changed to produce the gifts in the order they should be sung as the day of the gift increases.
Consider how these loops can be nested to produce the song, sung in the correct order. Hint: On any given day, how many gifts are given. This tells you how many times the loop that represents the gifts should be executed.
Following is one possible sample run, but feel free to be creative:
Explanation / Answer
#include <iostream>
using namespace std;
int main() // Main Function
{
int days, num;
int counter = 1,choice;
char name[100],response;
cout<<"Who am I singing with today ";
cin>>name;
do{
cout<<" Thank you "<<name<<" Spending money on your shopping spree has put you in the Christmas Spirit, are you ready to sing(y/n)?";
cin>>response;
if (response=='y' ||response=='Y' ){
choice=1;}
else{
choice=2;
}
switch(choice)
{
case 1:
cout<<" ***TWELVE DAYS OF CHRISTMAS*** ";
for (counter=1; counter<=13; counter++)
{
cout<<" On the ";
switch(counter){
case 1:
cout<<"1st";
break;
case 2:
cout<<"2nd";
break;
case 3:
cout<<"3rd";
break;
default:
cout<<counter + "th";
break;
}
cout<<" day of Christmas my true love sent to me ";
for (int x= counter; x > 0; x--)
{
switch(x)
{
case 1: cout<<" ";
if (counter > 1 )
cout<<"And ";
cout<<"A Partridge in a Pear Tree ";break; // Day 12
case 2: cout<<" Two Turtle Doves "; break;
case 3: cout<<" Three French Hens "; break;
case 4: cout<<" Four Calling Birds "; break;
case 5: cout<<" Five Golden Rings "; break;
case 6: cout<<" Six Geese a Laying "; break;
case 7: cout<<" Seven Swans a Swimming "; break;
case 8: cout<<" Eight Maids a Milking "; break;
case 9: cout<<" Nine Ladies Dancing "; break;
case 10: cout<<" Ten Lords a Leaping "; break;
case 11: cout<<" Eleven Pipers Piping "; break;
case 12: cout<<" Twelve Drummers Drumming "; break; // Day 1
}
}
}
break;
case 2:
cout <<" Your wish Good bye Merry Chritmas!! ";
break;
default:
cout<<"That does not compute!!!!! " ;
break;
}
}while(choice!=2);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.