Mel Moore
Mel Moore

Reputation: 89

Print a 12 month calendar with only input being the year. Using C

I'm stuck on a program that takes the user's desired year and prints out a 12 month calendar for the same year. This is all I have so far and I'm pretty sure I have the right way to figure out whether the year is a leap year (in the code) and how to find out when the 1st of Jan is (underneath first with just variables then filled in). Also I am trying to print this in normal Calendar format with the month on top then the days of the week underneath followed by the day numbers. Any help would be appreciated.

Find first day:

h = (1 + [(13(m + 1))/5] + K + [K/4] + [J/4] - 2J) mod 7

h = (1 + [(13(13 + 1))/5] + (year % 100) + [(year % 100)/4] + [(year/100)/4] - 2(year/100) % 7

H is the starting day, M is the month, K is yr % 100, J is yr / 100.

/* Calendar.c */

#include <stdio.h>

int main(void){

    int year, month, date;
    int startingDay; /* initfrom user input*/

    printf("Enter the year of your desired calendar: ");
    scanf("%d\n", &year);

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        int months[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    else 
        int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    for (month = 0; month < 12; month++) {
        const int daysInMonth = ; /* set # of days */
        int dayOfWeek;

        printf(…); //month name
        printf(…); //days of week

        for (dayOfWeek = 0; dayOfWeek<startingDay; dayOfWeek++)
            printf(/*blanks*/);

        for (int date = 1; date <= daysInMonth; date++) {
            printf("…", date);

            if (++dayOfWeek>6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek !=0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month
}

Output:

        February 2009
Sun Mon Tue Wed Thu Fri Sat
  1   2   3   4   5   6   7
  8   9  10  11  12  13  14

Upvotes: 1

Views: 15337

Answers (1)

jev
jev

Reputation: 2013

The following compiles, but there are still opportunities for improvement:

/* calender.c */

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

#define JULIAN    1
#define GREGORIAN 2

/*
 * return the day of the week for particualr date
 * flag JULIAN or GREGORIAN
 * (the divisions are integer divisions that truncate the result)
 * Sun = 0, Mon = 1, etc.
 */
int get_week_day(int day, int month, int year, int mode) {
  int a, m, y;

  a = (14 - month) / 12;
  y = year - a;
  m = month + 12*a - 2;

  if (mode == JULIAN) {
    return (5 + day + y + y/4 + (31*m)/12) % 7;
  }

  return (day + y + y/4 - y/100 + y/400 + (31*m)/12) % 7; // GREGORIAN
}


int main(void) {

    int year, month, date;
    int startingDay;     /* of the week: init from user input*/
    char *names[] = {"January", "February", "March", "April", "May", "June",
                    "July", "August", "September", "October", "November", "December"};

    printf("Enter the year of your desired calendar: ");
    scanf("%d", &year);

    // could check whether input is valid here

    startingDay = get_week_day(1, 1, year,GREGORIAN);

    int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
       months[1] = 29;

    for (month = 0; month < 12; ++month) {
        const int daysInMonth = months[month];  /* set # of days */
        int dayOfWeek, date;

        printf("\n  ------------%s-------------\n", names[month]);   // month name
        printf("  Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");         // days of week

        for (dayOfWeek = 0; dayOfWeek < startingDay; ++dayOfWeek)
            printf("     ");

        for (date = 1; date <= daysInMonth; ++date) {
            printf("%5d", date);

            if (++dayOfWeek > 6) {
                printf("\n");
                dayOfWeek = 0;
            }
        } // for date

        if (dayOfWeek != 0)
            printf("\n");

        startingDay = dayOfWeek;
    } // for month

    return EXIT_SUCCESS;
}

Upvotes: 4

Related Questions