Lab DayOfWeek - Day of the week
Day of the week
The equation given in the lab for computing the day of the week is:
$dayOfWeek = ( 1 + nYears + nLeapYears + nDaysToMonth + day ) % 7$
- $nYears$: the number of years since 1901, e.g. for the year 1916, $nYears = 15$
- $nLeapYears$: the number of leap years between 1901 and this year (not inclusive). For example, for year 1916 the number of years between 1901 and 1916 is 3 (years 1904, 1908 and 1912). Notice that 1916 is not counted.
- $nDaysToMonth$: the number of days since the start of the year to the start of this month. For example, in year 2001, the number of days since the start of the year until the start of March is 31 (the total days of January) + 28 (the total days of February) = 59 days.
- $day$: this is simply the day of the month.
Example: Computing the day of the week for April 3, 1933.
- $nYears = 1933 - 1901 = 32$
- $nLeapYears = 8$ (The leap years are 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932)
- $nDaysToMonth = 31 + 28 + 31 = 90$
- $day = 3$
- $dayOfWeek = ( 1 + 32 + 8 + 90 + 3) \, \textrm{mod} \, 7 = 134 \, \textrm{mod} \, 7 = 1$ .
You may use the following link to validate your calendar functions http://people.albion.edu/imacinnes/calendar/Day_of_the_Week.html
Output formating
Use the iomanip (#include <iomanip>
) functions * setfill()
and std::setw()
to force your numbers to be right aligned
For example:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "without setfill, setw\n";
cout << 18 << endl;;
cout << 222 << endl;
cout << 2321 << endl;
cout << "\nwith setfill, setw\n";
cout << setfill(' ') << std::setw(5) << 18 << endl;;
cout << setfill(' ') << std::setw(5) << 222 << endl;
cout << setfill(' ') << std::setw(5) << 2321 << endl;
}
Output:
without setfill, setw
18
222
2321
with setfill, setw
18
222
2321