User Tools

Site Tools


Sidebar

projects

haas:spring2014:cprog:projects:dayofweek

This is an old revision of the document!


Project: MENTAL MATH (DAY OF WEEK)

A project for C/C++ Programming.

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.

Prerequisites

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:

  • ability to construct and use selection statements (if)

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.

Step 2: Add 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 dropping the last digit. 10% of 54 is 5.

For our 2014 example, 10% of 14 is therefore 1.

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

Finally, 5% is half of 10% (half of 1 is 0.5), so 1.4 + 1.4 + 0.5 = 3.3

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

Look in the table above… what day of the week corresponds with 3?

What day of the week did January 1st, 2014 fall on?

Program

It is your task to write the program that will use the above method to compute the square of the requested value ending with a 5.

Your program should:

  • prompt the user for the number (input)
  • perform the task (process)
  • display the final value (output)

Execution

lab46:~/src/cprog/squares$ ./squares
Enter value: 75
75 x 75 = 5625
lab46:~/src/cprog/squares$ 

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

Reflection

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

  • Does this trick work (or can it be adapted to work) for three digit values ending with 5?
  • How about 4 digits?

Submission

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

  • Code must compile cleanly (no warnings or errors)
  • Executed program must display a total of 2 lines, one for input, one for output.
  • Output must be correct, and match the form given in the sample output above.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must be commented
    • have a properly filled-out comment banner at the top
    • have at least 20% of your program consist of //-style descriptive comments
  • Output Formatting (including spacing) of program must conform to the provided output (see above).
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

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

$ submit cprog squares squares.c
Submitting cprog project "squares":
    -> squares.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.

Verify submission

To verify you submitted successfully, you may run the following (from anywhere on lab46):

lab46:~$ verify cprog squares
squares: submitted successfully

Note if automated assessment is available for the project, you may actually see results in the output as well.

haas/spring2014/cprog/projects/dayofweek.1389946227.txt.gz · Last modified: 2014/01/17 08:10 by wedge