Assignment 8

due at midnight on   +40

The goal of this assignment is to help you understand defining and calling functions in C/C++. What we will do is revisit the assignment 6 (calendar), and break that long program into functions. You can refer to my assignment 6 solution from Oct 21st, as well as the a6-with-functions program we started on Nov 4th.

Your program should include the following fragment exactly as shown. It declares the prototypes for a series of functions, and then it defines the main function you should use. Your job is to define all the functions prototyped here. You can add them to your file below the main function.

The end result should work just like my calendar solution for assignment 6.

// ==================== FUNCTION PROTOTYPES ====================

int ask_user_for_year();
/* Prompt the user to enter the year as an integer (1900-), and repeat
 * until they provide a valid year. Return that year.
 */

int ask_user_for_month();
/* Prompt the user to enter the month as an integer (1-12), and repeat
 * until they provide a valid month. Return that month.
 */

void print_month_name(int m);
/* Print just the name of the given month ‘m’ in English, such as
 * “January” or “May”.
 */

void print_header(int m, int y);
/* Print the calendar title (month in English and year, such as
 * “January 1998”) using the given ‘m’ and ‘y’ values. (It should call
 * ‘print_month_name’.) Then print a blank line and the day-of-week
 * headings using three-character abbreviations (“Sun Mon Tue”).
 */

int compute_month_start(int m, int y);
/* Implement the formula to compute the day of the week on which month
 * ‘m’ starts in year ‘y’. Return an integer in the range 0=Sun,
 * 1=Mon, ..., 6=Sat.
 */

void print_chars(int k, char c);
/* Print out the character given by parameter ‘c’, repeated ‘k’ times.
 * Do NOT go on to the next line.
 */

int lookup_days_in_month(int m, int y);
/* Return the number of days in month ‘m’ for year ‘y’. You are
 * provided the year to help account for leap years in February.
 */

void print_calendar_grid(int dow, int max);
/* Print the calendar grid for a month starting on ‘dow’ (0=Sun,
 * 6=Sat), and up to ‘max’ days.
 */

//==================== MAIN FUNCTION ====================
// Your main program should be EXACTLY as shown here.
int main()
{
    int year = ask_user_for_year();
    int month = ask_user_for_month();
    int dow = compute_month_start(month, year);
    int last_day = lookup_days_in_month(month, year);
    print_header(month, year);
    print_calendar_grid(dow, last_day);
    return 0;
}