School again

Hey guys, sorry, it's me again.

This time I have a school assignment where we are supposed to find the weekday of a date, and whether it's a leap year. IE, "February 12, 2011, is a Saturday in a non-leapyear." And I'm having trouble trying to figure this out. The way we're supposed to find the date is
1. u = 2(3−(century mod 4)), where century is the first two digits of the
year. For example, century is 20 for the year 2011.
2. v = the last two digits of the year. For example, v is 11 for 2011.
3. w = v4 , using integer division. For example, v = 11/4 = 2 for 2011.
Month x
1 0 (6 in a leap year)
2 3 (2 in a leap year)
3 3
4 6
5 1
6 4
7 6
8 2
9 5
10 0
11 3
12 5
Table 1: Month Table

4. x is determined using the month Table 1.
For example, x is 3 for February 2011.
5. y = u + v + w + x + day. y = 6 + 11 + 2 + 3 + 3 = 25 for February 3,
2011.
6. day of the week = y mod 7. For example, February 3, 2011 oc-
curs on Thursday, since 25 mod 7 is 4. Note that 0 = Sunday, 1 =
Monday, . . . , 6 = Saturday.

Those are the rules I am to follow, and I am confused as hell. I have no clue how they want me to code this. I don't even understand the equation. Any help would be greatly appreciated.
Last edited on
Forget about parsing the input for now. Suppose you have three variables year, month, and day. Make the program run and output the result for values that you have hard coded in the initialization of those three variables and only then, when everything is working, wrap it with UI. One of the big mistakes IMO is to start planning from the UI portion.

Taking (the number in) the first two digits of a 4 digit integer: n / 100
Taking (the number in) the last two digits of a 4 digit integer: n % 100

Store the table thing (for the month --> x mapping) in array with 12 elements. Each element stores the corresponding value for a single month. Fill the array during initialization with the = {..} syntax. The cases for the leap years in the table can be handled directly by the code, skipping the array look-up entirely.

For the algorithm for leap year detection go to the link below. I would implement it in a function. But if I were you, this would be the first thing I would implement. And I would implement it directly in main at first, test it to see it works, then move it to a separate function, then proceed with the rest of the problem.
http://en.wikipedia.org/wiki/Leap_year#Algorithm

For outputting the names of the days, define array of strings like below and index it with the day # you get:

const char* daystr[] = {"Sunday", "Monday", ... "Saturday"};

Then for the last touches - the user input. What is the format of that supposed to be?

Regards
Last edited on
Topic archived. No new replies allowed.