In this project we are to determine, given a year as input, which day of the week the January 1st of that year falls on. We can determine this with an algorithm known as Zeller's congruence. Once we have compiled our project, we will be able to test different cases with the mmf0verify file.
mmf0verify uses stdin to write the year
fscanf (stdin, “%hu\n”, &year);
The ampersand above is necessary to assign the inputted value to our variable. The ampersand tells the fscanf function where our variable is stored in memory, so that we may access and modify the value based on the input we receive from the verify script.
To obtain the last 2 digits of our inputted year, we can take our year and mod (%) it by 100. Because we are using Zeller's congruence, we must subtract 1 from the year beforehand since we're dealing with January, which is considered a part of the previous year.
To subtract the best fitting multiple of seven the % operator can be used
However, if we are using Zeller's congruence this is an unnecessary step as this is handled implicitly by Christian Zeller's algorithm. This is why we January is considered a part of the previous year and is labeled as the thirteenth month rather than the first month.
Day | Value |
---|---|
Monday | 1 |
Day | Value |
Tuesday | 2 |
Day | Value |
Wednesday | 3 |
Day | Value |
Thursday | 4 |
Day | Value |
Friday | 5 |
Day | Value |
Saturday | 6 |
Day | Value |
Sunday | 7 |
Day | Value |
Sunday | 0 |
Subtracting 1 because it is a leap year may result in a value either less than the variables range or outside of the expected range of outputs
An underflow can be remedied in many ways.
If statements allow code to be run conditionally, controlling the flow of code
An if statement has two parts; A boolean condition and a some block of code.
signed int a = 5; signed int b = 6; if(a == 5) { fprintf(stdout, "The value of a is 5\n"); b = b*2; } if(b != 6) fprintf(stdout, "The value of b is not 6\n"); fprintf(stdout, "The value of b is %d\n", b);
This code uses if statements to control the flow of code.
Else if is an extension of an if statement. An else if statement must come directly after an if statement. It functions identical to an if statement, but is only ran if the prior if statement results in false.
int myVariable = 7; if(myVariable == 7){ fprintf(stdout, "The value of myVariable is 7\n"); } else if(myVariable >= 7){ fprintf(stdout, "The value of myVariable is greater than 7\n"); }
The else if statement is used to further control the flow of code, while also reducing the amount of code.
Because else if is only ran if the prior if statement is false it completes the same task as if(myVariable != 7 && myVariable >= 7)
The else function is similar to the else if function in that it is only ran when the prior if statement results in false.
The else function however doesn't contain an if statement and will always run the following block of code.
int myVariable = 7; if(myVariable == 7){ fprintf(stdout, "The value of myVariable is 7\n"); } else{ fprintf(stdout, "The value of myVariable is not 7\n"); }
The line The Value of myVariable is not 7 will always print unless myVariable == 7 results to true
The else function allows simple construction of an if than else statement.
Operator | Description |
---|---|
== | is equal to |
!= | is not equal to |
< | greater than |
> | less than |
<= | less than or equal to |
>= | greater than or equal to |
To verify your results, ./mmf0verify
Remember to only use stdout to print the days of the week [EX: fprintf(stdout, “Monday\n”)]. If stdout is used for anything other than the days of the week, the mmf0verify will say you have a mismatch for every year, even if you get the correct day when you run your compiled code. If you want to print something other than the days of the week, use stderr.