Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// Day of the year. // This program takes an integer representing a day of the Y

ID: 3621211 • Letter: #

Question


// Day of the year.
// This program takes an integer representing a day of the Year and translates it to an description of the form Month - day of month.
// Assumes all years have 365 days

#include <iostream>
#include <string>

using namespace std;

class DayOfYear
{
public: static const int daysAtEndOfMonth[ ];
static const string monthName[ ];
void print();
void setDay(int day){this->day = day;}
private: int day;
};

const int DayOfYear::daysAtEndOfMonth[ ] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const string DayOfYear::monthName[ ]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"};

//****************************************************
// DayOfYear::print. Convert and print day of year *
//****************************************************
void DayOfYear::print()
{
int month = 0;

while (daysAtEndOfMonth[month] < day)
month = (month + 1) %12;
// DaysAtEndOfMonth >= day
if (month == 0)
cout << "January " << day;
else
cout << DayOfYear::monthName[month] << " " << day - DayOfYear::daysAtEndOfMonth[month-1];
}
//=================================== Main Program ==============================================
int main()
{
// Tell user what program does
cout << "This program converts a day given by a number 1 through 365" << " into a month and a day.";
// Get user input
cout << " Enter a number: ";
int day;
cin >> day;
if (day < 0 || day > 365)
{ cout << "Invalid range for a day.";
exit(1);
}
// Do computation and print result
DayOfYear dy;
dy.setDay(day);
dy.print();
return 0;
}

Explanation / Answer

#include #include using namespace std; class DayOfYear { private: int day,monthend[13]; string months[12]; public: DayOfYear(int d) { day=d; setmonthend(); setmonth(); } void DayOfYear::setmonthend() { monthend[0] = 0; monthend[1] = 31; monthend[2] = 59; monthend[3] = 90; monthend[4] = 120; monthend[5] = 151; monthend[6] = 181; monthend[7] = 212; monthend[8] = 243; monthend[9] = 273; monthend[10] = 304; monthend[11] = 334; monthend[12]=365; } void DayOfYear::setmonth() { months[0]="January"; months[1]="February"; months[2]="March"; months[3]="April"; months[4]="May"; months[5]="June"; months[6]= "July"; months[7]="August"; months[8]="September"; months[9]="October"; months[10]="November"; months[11]="December"; } void DayOfYear::print() { int m=1; while(monthend[m]