• §

    Jonas Altrock ew20b126@technikum-wien.at

    To overview.

    The whole source file u5_calendar.c.

    /* Compile with `clang -std=c99 -Wall -o u5_calendar u5_calendar.c` */
    /* Test with `../checkproject u5_calendar` */
  • §

    Include C-libraries for input/output.

    #include <stdio.h>
    #include <stdlib.h>
  • §

    Exercise 5: Loops: Calendar

  • §

    Enter the number of days in a month and the day of week on the first of the month, then print a calendar of this month.

    Details

  • §

    Read two integer numbers. The first one is the numbers of the days of a month. The next one is the day-of-week of the first of the current month. 0 here is sunday which is also the day in the first column of the calendar. Print the calendar of this month where each row represents a week and all days in a column represent days on the same day of week. Each column should be 3 characters wide (use %3d to print the days for the correct format).

    You may assume that only correct values are entered.

    Example 1

  • §

    Input:

    31 0
    

    Output:

     1  2  3  4  5  6  7
     8  9 10 11 12 13 14
    15 16 17 18 19 20 21
    22 23 24 25 26 27 28
    29 30 31
    

    Example 2

  • §

    Input:

    30 3
    

    Output:

              1  2  3  4
     5  6  7  8  9 10 11
    12 13 14 15 16 17 18
    19 20 21 22 23 24 25
    26 27 28 29 30
    26 27 28 29 30
    

    main()

  • §
    int main() {
  • §

    The user will input the number of days in the month, and the day of the week for the first day.

        int days, first;
        scanf("%d %d", &days, &first);
  • §

    Loop through 0 to days + first, to get all ‘slots’ for days on the calendar.

        for (int i = 0; i < days + first; i++) {
  • §

    Actual day in month: best understood by doing a concrete example, say i = 4, first = 3 (Wednesday), then day = 4 - 3 + 1 = 2, the second day of the month.

            int day = (i - first) + 1;
  • §

    Before first real day, print empty column/slot, three spaces,

            if (day < 1) {
                printf("   ");
            }
  • §

    else print the number with 3 characters, the prefix being the space character! 1 will be output as   1, 26 as  26.

            else {
                printf("% 3d", day);
            }
  • §

    One row has a maximum of seven days (one week). i starts with 0, so we add 1 before using the modulo % operator to get the rest after dividing by 7.

    That means i = 6 will print a line break.

            if ((i + 1) % 7 == 0) {
                printf("\n");
            }
        }
  • §

    If our rows do not end at the last column, print an extra newline. Example:

    • days = 30, first = 3
    • days - (7 - first) == 30 - (7 - 3) == 26
    • 26 % 7 == 5 The last day in the calendar will be two columns before the last one, meaning a Thursday.
        if ((days - (7 - first)) % 7 != 0) {
            printf("\n");
        }
  • §

    Return code 0 for everything OK.

        return 0;
    }