User Tools

Site Tools


notes:cprog:fall2024:projects:mmf0

This is an old revision of the document!


MMF0

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);

Obtain the last two digits of this input value

Quarter the value

Add quartered value back to two digit year

Subtract best fitting multiple of seven

If it is a leap year subtract 1 more

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

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

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.1727039008.txt.gz · Last modified: 2024/09/22 21:03 by tkastne1