Corning Community College
CSCS1320 C/C++ Programming
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.
To assist with consistency across all implementations, data files for use with this project are available on lab46 via the grabit tool. Be sure to obtain it and ensure your implementation properly works with the provided data.
lab46:~/src/SEMESTER/DESIG$ grabit DESIG PROJECT
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:
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.
Okay, time for the magic.
Let us try it on January 1st, 2014.
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.
Even this is something we can do in our heads. I can think of two approaches right off the bat:
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.
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.
Once we have our 25% value, go and add it back to our two-digit year value:
14 + 3 = 17
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
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?
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”.
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 the following:
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:
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).
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:
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.
To be successful in this project, the following criteria (or their equivalent) must be met:
Let's say you have completed work on the project, and are ready to submit, you would do the following (assuming you have a program called uom0.c):
lab46:~/src/SEMESTER/DESIG/PROJECT$ 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.
I'll be evaluating the project based on the following criteria:
39:dow0:final tally of results (39/39) *:dow0:used grabit to obtain project by the Sunday prior to duedate [6/6] *:dow0:clean compile, no compiler messages [7/7] *:dow0:program conforms to project specifications [13/13] *:dow0:code tracked in lab46 semester repo [7/7] *:dow0:project submitted with 'make submit' [6/6]