User Tools

Site Tools


notes:cprog:fall2024:projects:mmf0

MMF0

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.

Process

Input value as short int (all four digits)

mmf0verify uses stdin to write the year

  • fscanf can be used to read from stdin
  • The following line uses fscanf to read the stdin file, and assign the result to a variable named 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.

Obtain the last two digits of this input value

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.

Quarter the value

Add quartered value back to two digit year

Subtract best fitting multiple of seven

To subtract the best fitting multiple of seven the % operator can be used

  • Do note that % is the remainder operator and not a modulo operator
    • Modulo and remainder are primarily the same but they handle negative numbers differently

If it is a leap year subtract 1 more

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.

Look up day in table

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

Edge case: leap years

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.

  • Adding 7 to the number when it is outside of the expected range
  • When the result would be outside of the expected add 6 instead of subtracting 1
  • etc

SELECTION

if statements

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.

  • The first if statement's boolean condition is a == 5, which evaluates to true.
  • Because it evaluates to true the block of code after it is ran. A block of code is surrounded by { }.
  • The second if statement's boolean condition is b != 6, which evaluates to true.
  • Because it evaluated to true the block of code after it is ran; However, the code after it has no { }, thus the only code that is controlled by the if statement is fprintf function directly after it
  • The final fprintf in this code will run unconditionally because it is not within a block of code that is controlled by an if statement

else if

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)

else

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.

RELATIONAL OPERATORS

Operator Description
== is equal to
!= is not equal to
< greater than
> less than
<= less than or equal to
>= greater than or equal to

VERIFY RESULTS

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.

notes/cprog/fall2024/projects/mmf0.txt · Last modified: 2024/09/29 03:52 by mwinter4