Question
Please explain me this code in detail because the question will asked from this code in the final exam Please save me! // final.cpp *preparation
Please explain me this code in detail because the question will asked from this code in the final exam
Please save me!
// final.cpp *preparation version* Copyright (c) 2023 #include
/* **** Start of lines from file DaysOfChristmas_smallCode.cpp **** */ // Assignment 3: DaysOfChristmas_smallCode.cpp sample solution // Copyright (c) 2023 Katy Liu, Dominic Maddalozzo, and W. Craig Scratchley // Should occupy 522 bytes if you remove comments and any allowable whitespace.
// preprocessor # directives must each be on their own line #include
// A preprocessor macro for inserting onto std::cout -- whitespace on next line needed #define C std::cout<< int /*whitespace*/ d, q; // day and quantity counters -- here each default to 0 std::vector /* must leave whitespace here */ s { "st", "nd", "rd" }, // suffixes for first 3 ordinal numbers g { "a Partridge in a Pear Tree", "Turtle Doves and ", "French Hens", "Calling Birds", "Golden Rings", "Geese a Laying", "Swans a Swimming", "Maids a Milking", "Ladies Dancing", "Lords a Leaping", "Pipers Piping", "Drummers Drumming" }; // gifts void DaysOfChristmas_smallCode() // ..() is the same as ..( void ) { C "The 12 Days of Christmas"; // macro C is expanded here and other locations while (d < 12) { C " On the " << d + 1 << (d < 3 ? s[d] : "th") << " day of Christmas my true love sent to me:"; if (!d) C " "; for (q = ++d; q;) q - 1 ? C " " << q << " " : C "", C /*whitespace*/ g[--q]; // comma operator at top level of expression } } /* **** End of lines from file DaysOfChristmas_smallCode.cpp **** */
// The below is Copyright (c) 2023 W. Craig Scratchley using namespace std;
void processGift(const int &day) { static int count; count += 1; cout << fmt::format("{:>2} {} ", day, g.at(day - 1)); }
inline void DaysOfChristmas_switch2 (int dayLimit = 6 ) { // This function outputs to standard output the lyrics for "The X Days of Christmas".
cout << "The " << dayLimit << " Days of Christmas" << flush; // Title for (int day{1}; day <= dayLimit; ++day) { cout << " On the " << day << flush;
// Determining day's ordinal suffix ie. st, nd, rd depending on day switch (day) { case 1: cout << "st" << flush; break; case 2: cout << "nd" << flush; break; case 3: cout << "rd" << flush; break; default: cout << "th" << flush; }
cout << " day of Christmas my true love sent to me: " << flush;
switch (day) { // note starting at case 6 here case 6: processGift(12); // note 12 as argument [[fallthrough]]; case 5: processGift(5); [[fallthrough]]; case 4: processGift(4); [[fallthrough]]; case 3: processGift(3); [[fallthrough]]; case 2: cout << " 2 Turtle Doves and " << flush; [[fallthrough]]; case 1: cout << g.at(1 - 1) << flush; } } }
int main( ) { cout << "a) q is " << q << endl; // don't let the debugger trick you constexpr long long q{1}; try { cout << "b) q is " << q << endl; // don't let the debugger trick you const double q{2.2}; cout << "c) q is " << q << endl; array
cout << " The gifts are: "; cout << std::accumulate(rbegin(g), rend(g), ""s, // iterators rbegin and rend are 'reverse' iterators [](const auto& x, const auto& y){return x + (y[0]=='a'?"":" ") + y;} );
cout << " "; DaysOfChristmas_smallCode( );
cout << " "; DaysOfChristmas_switch2();
cout << " "; processGift(13); } catch (const std::out_of_range& ex) { cout << "An exception occurred: " << ex.what() << ' '; return 3; }
return q; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started