User Tools

Site Tools


Sidebar

projects

wcp1 (due 20230125)
ntr0 (due 20230126)
pct0 (bonus; due 20230129)
pct1 (bonus; due 20230129)
dap0 (due 20230201)
wcp2 (due 20230201)
pct2 (due 20230202)
ngf0 (due 20230208)
pct3 (bonus; due 20230208)
wcp3 (due 20230208)
wcp4 (due 20230215)
dtr0 (due 20230216)
pct4 (due 20230216)
bwp1 (bonus; due 20230301)
ngf1 (due 20230301)
pct5 (bonus; due 20230301)
wcp5 (due 20230301)
dow0 (due 20230308)
gfo0 (due 20230308)
wcp6 (due 20230308)
pct6 (due 20230309)
ngf2 (due 20230315)
pct7 (bonus; due 20230315)
wcp7 (due 20230315)
pct8 (due 20230322)
wcp8 (due 20230322)
cnv0 (due 20230323)
ngf3 (due 20230329)
pct9 (bonus; due 20230329)
wcp9 (due 20230329)
bwp2 (bonus; due 20230412)
gfo1 (due 20230412)
ngf4 (due 20230412)
pctA (due 20230412)
wcpA (due 20230412)
oop0 (due 20230419)
pctB (bonus; due 20230419)
wcpB (due 20230419)
oop1 (due 20230426)
pctC (due 20230426)
wcpC (due 20230426)
oop2 (due 20230503)
pctD (bonus; due 20230503)
wcpD (bonus; due 20230503)
gfo2 (due 20230510)
pctE (bonus; due 20230510)
wcpE (bonus; due 20230510)
EoCE (due 20230518)
haas:spring2023:cprog:projects:dow0

Corning Community College

CSCS1320 C/C++ Programming

PROJECT: MENTAL MATH - 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.

GRABIT

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

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% (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 the following:

  • 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.

SUBMISSION

To be successful in this project, the following criteria (or their equivalent) must be met:

  • Project must be submit on time, by the deadline.
    • Late submissions will lose 33% credit per day, with the submission window closing on the 3rd day following the deadline.
  • All code must compile cleanly (no warnings or errors)
    • Compile with the -Wall and –std=gnu18 compiler flags
    • all requested functionality must conform to stated requirements (either on this document or in a comment banner in source code files themselves).
  • Executed programs must display in a manner similar to provided output
    • output formatted, where applicable, must match that of project requirements
  • Processing must be correct based on input given and output requested
  • Output, if applicable, must be correct based on values input
  • Code must be nicely and consistently indented
  • Code must be consistently written, to strive for readability from having a consistent style throughout
  • Code must be commented
    • Any “to be implemented” comments MUST be removed
      • these “to be implemented” comments, if still present at evaluation time, will result in points being deducted.
      • Sufficient comments explaining the point of provided logic MUST be present
  • No global variables (without instructor approval), no goto statements, no calling of main()!
  • Track/version the source code in your lab46 semester repository
  • Submit a copy of your source code to me using the submit tool (make submit on lab46 will do this) by the deadline.

Submit Tool Usage

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.

RUBRIC

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]

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 or otherwise maintaining consistency in code style and presentation will be subject to a 25% overall deduction
  • Solutions not organized and easy to read (assume a terminal at least 90 characters wide, 40 characters tall) are subject to a 25% overall deduction
haas/spring2023/cprog/projects/dow0.txt · Last modified: 2023/02/12 13:19 by 127.0.0.1