Monday, March 7, 2016

From October 2, 2015

I apparently had this written back in October of last year when I was working on a calendar program in C, but I never posted it because it was titled "To be written tomorrow..." and I obviously got fucking distracted. Anyway, here's my (very likely hackish) calendar program.

After mulling over this ALL day, here is the answer I've come up with for this programming project.

/* Write a program that prints a calendar. */

#include <stdio.h>

int main (int argc, char* argv[]) {

    int days_month, week_begin, var1, var2;

    printf("Enter the number of days in the month: ");
    scanf("%d", &days_month);
    printf("Enter the day of the week the first day falls upon (1=Sun, 7=Sat): ");
    scanf("%d", &week_begin);

    var2 = 1;
    while (week_begin > var2) {
        printf("   ");
        var2++;
    }

    for (var1 = 1; days_month >= var1; var1++, ++week_begin) {
        printf("%3d", var1);
        if (week_begin >= 7) {
            printf("\n");
            week_begin -= 7;
        }
    }
    printf("\n");

return 0;
}

I looked up the answer in the book answer key and 50% of my program is almost identical to the author's program. Woohoo!
Unfortunately, that last gnarly bit with the if statement is vastly different from his. He used all sorts of maths like remainder and variables - 1 * variables to get his answer. I can't see the relationships of numbers quite that easily... yet. :(

No comments:

Post a Comment