Classwork 20: Calendar
- To practice implementing a program designed with the top-down design process.
- To practice implementing a large-ish program as a group, with different people implementing different parts of the program.
In our previous class, the students in the class chose a design for a program that prints out an annual calendar. The output of the program should look something like:
What to do
In class, you will be given one of the functions to implement.
You must download calendar.h, BUT DO NOT MODIFY THIS FILE. Make sure that you save it as a .h file.
Create two separate files, one for your function and one for your main program that will test your function. Both programs MUST include calendar.h at the top:
This will ensure that the function you implement is compatible with the function prototype specified in calendar.h.
For example, if you are implementing isLeapYear(), you would have two files: leapyear.c and main.c. The first file leapyear.c has the implementation of isLeapYear(). It is very important that this file NOT have a main function. You should have a main function in main.c to test your isLeapYear() function. That way you can check that your isLeapYear() function really works before you submit it to be combined with the other students' programs.
To compile you would type:
gcc -Wall leapyear.c main.c
The functions, one by one
/* GetYear() Prompt the user for the calendar year. Make sure that the year is not before FirstYear, otherwise keep bugging the user. */ int GetYear() ;
This should just be a simple while loop.
/* WeekdayJan1(int year) Return the day of the week for January 1 for the given year. */ int WeekdayJan1(int year) ;
Here you can start with the fact that January 1, 1900 was a Monday and figure out what day of the week each subsequent year starts with until you get to the given year. You need to consider leap years. SarahsFunction() could come in handy.
There are faster algorithms for this, but first implement the slow straightforward one. If that works, you can attempt a faster algorithm.
PrintLabel(int month, int year)
/* PrintLabel(int month, int year) Prints a nice label like "August 2012" centered over the monthly calendar. */ void PrintLabel(int month, int year) ;
A simple function. You just need to print out the month name (e.g., "August") given a numeric designation for the month (e.g., 8). The only complication is centering the label over the monthly calendar.
int NumberDays(int month, int year)
/* int NumberDays(int month, int year) Return the number of days in the given month. The year parameter is needed for leap years. */ int NumberDays(int month, int year) ;
This should just be a big switch statement. You can use nested if statements too. You should use the isLeapYear() function provided instead of writing your own.
PrintMonth(int days, int weekday)
/* PrintMonth(int days, int weekday) Print a monthly calendar for a month that has 'days' days and begins on the given weekday. */ void PrintMonth(int days, int weekday) ;
This might look like the "main" part of the program, but all you really need to do is print out
Su Mo Tu We Th Fr Sa
and the numbers 1 through 'days'. You just have to figure out when to print out the newline characters.
int SarahsFunction(int days, int weekday)
/* int SarahsFunction(int days, int weekday) Return the weekday of the day that is 'days' days after the given weekday. For example, if August starts on a Wednesday, then SarahsFunction(31, 4) will tell you that September starts on Saturday. */ int SarahsFunction(int days, int weekday) ;
A very useful function. This one requires more thinking than coding. Consider what the modulus operator % can do for you.
int isLeapYear(int year)
/* int isLeapYear(int year) Return 1 if the given year is a leap year. Otherwise returns 0. */ int isLeapYear(int year) ;
Pretty straightforward. Recall that the rule for which years are leap years were given in Homework 6. Code quickly so others can use your function. Then, help someone else out.
As soon as you are done, submit both of your files to cw20. Then shout out that you are done so your program can be incorporated into the whole program.
You can continue testing and submitting improved versions of your program. Again, shout out when you submit a new version.