Table of Contents

Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: MENTAL MATH DAY OF WEEK (DOW0)

Objective

To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of determining what day of the week January 1 of any given year (in the 21st century) falls on.

Assumptions

To assist you in completing this project, you may make the following assumptions:

Prerequisites/Corequisites

In addition to the new skills required on previous projects, to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

Background

Mental Math constitutes an intersection of mental tricks and math- instead of utilizing a purely math-only solution, textual manipulations or simplifications in the computational process may take place enabling an individual to, once having learned the process, solve such problems in their head, and typically without the use of a calculating device.

The process in this case is one of simple (reduced) multiplication and mapping against a table. To wit:

Day values

For this trick to work, we need to be familiar with the following table (a map of days to numeric values):

Monday Tuesday Wednesday Thursday Friday Saturday Sunday
1 2 3 4 5 6 7 or 0

Calculating day of the week based on year

Okay, time for the magic.

Let us try it on January 1st, 2014.

Step 1: Obtain last two digits of the year

In our example, we're working with 2014, the last two digits are therefore: 14

You should be able to come up with a means of extracting this information in your program.

Step 2: Compute 25% (drop the decimal)

Even this is something we can do in our heads. I can think of two approaches right off the bat:

Approach 1: 10 + 10 + 5

10% percent of anything is merely moving the decimal over one place to the left. 10% of 54 is 5.4

For our 2014 example, 10% of 14 is therefore 1.4

So we need two 10 percents… 1.4 + 1.4 = 2.8

Finally, 5% is half of 10% (half of 1.4 is 0.7), so 1.4 + 1.4 + 0.7 = 3.5

But, since we do not care about the decimal, we drop it and are left with just 3.

Approach 2: half of half

25% is a convenient value for us with respect to 100, allowing this optimized approach to work.

So, 14 cut in half is 7.

7 cut in half is 3.5.

Once again, dropping the decimal yields 3.

Step 3: Add 25% to year value

Once we have our 25% value, go and add it back to our two-digit year value:

14 + 3 = 17

Step 4: Subtract the largest fitting multiple of 7

Some multiples of 7:

0 7 14 21 28 35 42 49

So, with a value of 17, what is the largest multiple of 7 that is still less than (or equal to) 17?

Hopefully you identified the 14 as the likely candidate.

17 - 14 = 3

Step 5: Look up day in table

We ended up with a 3 as the result for January 1st, 2014.

Go and reference the 3 from that table… what day do we get? Does it match the actual day of the week for January 1st, 2014?

lab46:~$ cal 01 2014
    January 2014
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

lab46:~$ 

Pretty neat, eh?

Exception: Leap Years

In the event of a leap year, we simply subtract 1 from the 25% value, and continue on as usual.

Makes sense, right? Leap years add a day, so something ends up being “off by one”.

Program

It is your task to write the program that will use the above method to determine the day of the week any given January 1st in the 21st century falls on.

Your program should:

Execution

lab46:~/src/cprog/dow0$ ./dow0
Which year: 2014
January 1st, 2014 falls on: Wednesday
lab46:~/src/cprog/dow0$ 

The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.

Output Specifications

Your program must output as follows:

Reflection

Be sure to provide any commentary on your journal regarding realizations had and discoveries made during your pursuit of this project.

This isn't just about implementing a particular algorithm, it is about understanding an algorithm- its domain of correctness, and its limitations.

Submission

To successfully complete this project, the following criteria must be met:

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog dow0 dow0.c
Submitting cprog project "dow0":
    -> dow0.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

What I'll be looking for:

52:dow0:final tally of results (52/52)
*:dow0:data stored and calculated pursuant to algorithm and specifications [4/4]
*:dow0:code is pushed to lab46 repository [4/4]
*:dow0:adequate indentation and comments in dow0.c [4/4]
*:dow0:program uses indicated algorithm [12/12]
*:dow0:output conforms to project specifications [12/12]
*:dow0:runtime tests of dow0.c succeed [12/12]
*:dow0:no negative compiler messages for dow0.c [4/4]