CSCS1320 C/C++ Programming
PROJECT: DAY OF WEEK (dow0)
OBJECTIVE
To begin our exploration of decision-making and more detailed process-following in C, implementing a program that uses a mental math technique to determine the day of the week that January 1st falls on for any year in the 21st century.
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 |
NOTE: Depending on how you implement your algorithm, you may find your approach prefers 7s OR 0s… you aren't necessarily going to be encountering them at random. Once you work through your approach, you will discover which one you end up with.
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% AND 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.
- Half of 100 is 50 (50%)
- Half of 50 is 25 (25%) – hence the "half of half"
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".
SELECTION
The computer follows instructions, one step at a time, in a top-down manner (starting at the first instruction, proceeding to the next, then the next, then the next, etc.) until it arrives at the end of the provided instructions.
One such instruction available to us is that of the selection statement, of which we will focus specifically on the general-purpose and broadly used if().
An if() statement can make a decision, assuming that either the result of a comparison of two values, or the provided value, results in a true or false result
Should the condition presented be true, perform a specific subset of instructions. If false, skip the processing of those instructions.
The general presentation of an if() statement in your code will look like:
int x = 7;
int y = 2;
int z = 0;
...
z = x % y; // take the remainder of x divided by y
if (z == 0)
{
first thing to do;
next thing to do;
...
nth thing to do;
}
The condition present will typically be in terms of two values being compared in some way, using one of the following relational operators available to us in C:
-
==is equal to -
!=is not equal to -
<is less than -
>is greater than -
<=is less than or equal to -
>=is greater than or equal to
In the example above, we were comparing the relationship of the value currently contained within the z variable, and the constant 0, with respect to being equal. If the two match, the result is true (and we run what is inside the if() block). If false, we skip the if() block.
There are further variations of the if(), which have to do with capturing additional scenarios.
The next one is the else, a companion to the if(). Where the if() processes a set of included instructions in the event some provided condition is true, the else contains a set of instructions to process in the event that condition is false.
int x = 7;
int y = 2;
int z = 0;
...
z = x % y; // take the remainder of x divided by y
if (z == 0)
{
first thing to do;
next thing to do;
...
nth thing to do;
}
else
{
first thing when false;
next thing;
...
nth thing;
}
When doing selection, the minimum needed is the if() statement. Once you have an if() statement, you can optionally append up to ONE else clause (or you can have none: it all depends on the nature of the condition you are testing, and what you'd like to do as a result).
You can have multiple if() statements, each checking their unique condition, and each of those if() statements potentially having an associated else clause.
Should the process call for a finer level of evaluation (say, you want to do something unique when a given variable is 0, and something different if 1, yet different if 2, etc.), we also have the ability to encapsulate that into our if() block (assuming we wouldn't be better served just having individual if() statements), by the use of the else if():
int x = 17;
int y = 5;
int z = 0;
...
z = x % y; // take the remainder of x divided by y
if (z == 0)
{
first thing to do;
next thing to do;
...
nth thing to do;
}
else if (z <= 3)
{
first thing to do;
next thing to do;
...
nth thing to do;
}
else if (z > 3)
{
first thing to do;
next thing to do;
...
nth thing to do;
}
else
{
first thing when false;
next thing;
...
nth thing;
}
Note that whether or not we have an else if(), and whether or not we have an else, we MUST (and first) have an if().
If we have an else, it necessarily comes last (it has no condition, it is the catch-all, the clause to handle things in the event none of the previous conditioned clauses matched).
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:
- prompt the user for the four digit year (input)
- perform the task (process)
- display the final value (output)
VERIFICATION
Included in the grabit is a dow0verify script that can run your program through the entire gamut of years in this century, displaying the accuracy of results.
It would be worthwhile to use it to ensure your final result is functional.
GRABIT
I have prepared a grabit for resources related to this project. To
obtain it:
lab46:~/src/SEMESTER/DESIG$ grabit DESIG dow0
At which point you can change into the newly created and populated
dow0 directory.
Be sure, using git, to add, commit, and push this to your lab46 clone of your repository.
Then, if working on your pi/system, git pull to get the files available there, or continue working on it on lab46.
SUBMISSION
To successfully complete this project, the following criteria must be met:
- Code must compile/execute cleanly (no notes, warnings, nor errors)
- Code must be nicely and consistently indented
- Code must be well commented
- Do NOT double space your code. Group like statements together.
- Track/version the source code in your private semester 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:
lab46:~/src/SEMESTER/cprog/dow0$ make submit
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:
RUBRIC
I'll be evaluating the project based on the following criteria:
156:dow0:final tally of results (156/156)
*:dow0:grabit the code on lab46 by Sunday before deadline [26/26]
*:dow0:code is pushed to private semester repository [13/13]
*:dow0:clean compile, no compiler messages [26/26]
*:dow0:program conforms to project specifications [52/52]
*:dow0:project submitted with 'make submit' [13/13]
*:dow0:program passes verification checks [26/26]
NOTE: The spirit of program implementation includes your process using the mental math approach, not a more seemingly-straightforward computational approach, or one given to you out of context by AI.
Additionally:
- Solutions not abiding by spirit of project will be subject to a 50% overall deduction
- Solutions not utilizing descriptive why and how comments will be subject to a 25% overall deduction
- Solutions not utilizing indentation to promote scope and clarity will be subject to a 25% overall deduction
- Solutions not organized and easy to read are subject to a 25% overall deduction