From: marwk@levels.unisa.edu.au
Newsgroups: unisa.cis.prog.c.05286
Subject: PRACTICAL 5: Answers
Date: 15 Oct 92 14:47:34 +0930
Organization: University of South Australia

PRACTICAL 5: Solution

Programming Exercise 3.7

Mark scheme (guidelines)

Completed program with correct and neat output that DOES match the program: 20

Runs as above but produces 2 separate sides of output: 12

Runs as above except not very neat: 16

Runs as above except not correct starting dates for subsequent months: 14

Runs as per the original question: 8


/* CALENDAR.C
 *
 * prints the calendar for a given year.
 *
 * The user enters the year for the calendar is required.
 *
 * The calendar appears on one A4 page with 2 columns of months.
 *
 * The calandar is written to the file CALENDAR.REP
 */

#include <stdio.h>

int main(void)
{
    int year;
    int leap_year;

    int day_code_left;   /* for the month on the left-hand side */
    int day_code_right;  /* for the month on the left-hand side */

    int month;

    int day;
    int days_left;
    int days_right;

    int line;

    /* request the year of the calendar to be produced */

    do
    {
        fprintf(stderr, "Enter the YEAR of the calendar to be produced: ");
        scanf("%d", &year);
    } while (year < 1900 || year > 2400);

    printf("CALENDAR for %d\n\n", year);

    /* determine the day_code of January 1st */

    day_code_left = (year + (year - 1)/4 - (year - 1)/100 + (year - 1)/400) % 7;

    /* determine whether or not this is a leap year */

    leap_year = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);

    for (month = 1; month <= 12; month += 2)
    {
        switch (month)  /* print the name and set the days in the month */
        {
        case 1:
            printf("\n\n%-30s", "January");
            days_left = 31;

            printf("%-30s", "February");
            days_right = leap_year ? 29 : 28;
            break;
        case 3:
            printf("\n\n%-30s", "March");
            days_left = 31;

            printf("%-30s", "April");
            days_right = 30;
            break;
        case 5:
            printf("\n\n%-30s", "May");
            days_left = 31;

            printf("%-30s", "June");
            days_right = 30;
            break;
        case 7:
            printf("\n\n%-30s", "July");
            days_left = 31;

            printf("%-30s", "August");
            days_right = 31;
            break;
        case 9:
            printf("\n\n%-30s", "September");
            days_left = 30;

            printf("%-30s", "October");
            days_right = 31;
            break;
        case 11:
            printf("\n\n%-30s", "November");
            days_left = 30;

            printf("%-30s", "December");
            days_right = 31;
            break;
        }

        /* display the headings for 2 months */

        printf("\n\n%-30s", "Sun Mon Tue Wed Thu Fri Sat");
        printf("%-30s\n", "Sun Mon Tue Wed Thu Fri Sat");

        /* determine the starting day code for the right-hand month */

        day_code_right = (day_code_left + days_left) % 7;

        /* output 6 lines for each month */

        for (line = 0; line < 6; ++line)
        {
            /* output a line for the first column */

            for (day = line * 7 + 1 - day_code_left;
                 day < (line + 1) * 7 + 1 - day_code_left; ++day)
            {
                if (day <= 0) /* not the start day yet */
                {
                    printf("%4s", " ");
                }
                else if (day > days_left) /* after last day */
                {
                    printf("%4s", " ");
                }
                else
                {
                    printf("%3d ", day);
                }
            }

            printf("%2s", " ");  /* skip over to the second column */

            /* output a line for the second column */

            for (day = line * 7 + 1 - day_code_right;
                 day < (line + 1) * 7 + 1 - day_code_right; ++day)
            {
                if (day <= 0) /* not the start day yet */
                {
                    printf("%4s", " ");
                }
                else if (day > days_right) /* after last day */
                {
                    printf("%4s", " ");
                }
                else
                {
                    printf("%3d ", day);
                }
            }

            printf("\n");
        }

        /* calculate the start day for the next left-hand month */

        day_code_left = (day_code_right + days_right) % 7;
    }

return 0;
}

--
Raymond Kennington
LECTURER
University of South Australia   | Act in haste and repent at leisure
P.O. Box 1                      | Code too soon and debug forever
Ingle Farm                      | Knobs, knobs everywhere,
South Australia                 |              just vary a knob to think!
#! rnews 3449