This program displays the lyrics to The Twelve Days of Christmas... in binary.
It uses a function called convertToBinary to convert the integer day number from base-10 to base-2, returning the result as a string of binary digits.
The day numbers are stored in an array of string values called day. The gifts are stored in an array of string values called gift.
The program uses a for loop to access each particular day name and present from the arrays.
Select and copy the C++ source-code at the bottom of this post.
Paste into a text editor, such as Nano or Geany.
Then save the new file, ending in .cpp
I used days.cpp
To compile from the command-line:
g++ -o days.cpp days
To run from the command-line:
./days
string day[] = { "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" };
Code is below...
#include <iostream> // Program uses the screen
#include <cmath> // Math function pow - raises value to a certain power
using namespace std; // Abbreviations for "channel out" (screen) and "end-of-line"
// --------------------
string convertToBinary( int base10Num )
{
// Converts an integer in Base 10 to Base 2 pattern of binary digits
const int NUMBER_OF_BITS = 8;
string binString = "";
int placeValue;
for ( int column = NUMBER_OF_BITS - 1; column >= 0; column-- )
{
placeValue = pow( 2, column );
if ( base10Num >= placeValue )
{
base10Num -= placeValue;
binString += "1";
}
else
binString += "0";
} // end of for loop
return binString;
}
// --------------------
int main()
{
const char TAB = '\t'; // Make it more obvious this is to display a tab character
const int NUMBER_OF_DAYS = 12;
string day[NUMBER_OF_DAYS];
day[0] = "first";
day[1] = "second";
day[2] = "third";
day[3] = "fourth";
day[4] = "fifth";
day[5] = "sixth";
day[6] = "seventh";
day[7] = "eighth";
day[8] = "ninth";
day[9] = "tenth";
day[10] = "eleventh";
day[11] = "twelfth";
string gift[NUMBER_OF_DAYS];
gift[0] = "a partridge in pear tree.";
gift[1] = "turtle doves";
gift[2] = "french hens";
gift[3] = "calling birds";
gift[4] = "gold riiiiiiinnngggs!!!";
gift[5] = "geese a-laying";
gift[6] = "swans a-swimming";
gift[7] = "maids a-milking";
gift[8] = "ladies dancing";
gift[9] = "lords a-leaping";
gift[10] = "pipers piping";
gift[11] = "drummers drumming";
// Display each day and the fantastic gifts to be bestowed upon thee etc. etc.
for (int dayNum = 0; dayNum < NUMBER_OF_DAYS; dayNum++ )
{
cout << "On the ";
cout << day[dayNum];
cout << " day of Christmas, my true love gave to me...";
// Display each gift for the current day
for ( int giftNum = dayNum; giftNum >= 0; giftNum-- )
{
cout << endl;
cout << TAB;
// Decide whether to display quantity of each gift
// (all except partridge are preceded by a quantity in the song)
if ( giftNum > 0 )
{
cout << convertToBinary( giftNum + 1 );
cout << " ";
}
else
if ( dayNum > 0 )
cout << "and ";
// Display the name of the current gift
cout << gift[giftNum];
} // end of gift loop
cout << endl << endl; // Leaves a blank line
} // end of day loop
// --------------------
cout << endl << endl << endl;
// What happens after Christmas?
cout << "Right, that's Christmas over. Where's the pumpkin?" << endl;
return 0; // Return error status - 0 indicates no errors occurred
} // end of main function
Find out more - buy the book on Amazon...