Function Help


Joined
Feb 19, 2022
Messages
3
Reaction score
0
I am working on a function that prints a calendar. I have another function that tells me if a year is a leap year. I have another function that tells me what day of the week that a month starts on. In this function I am having trouble getting the dates to change based on the start date. I can get either to work but not both. Small side note, if an array[] is empty it cant show 0 it has to be blank. Not sure how to do that part either.

Any help would be appreciated.


void print_calendar(int month, int year)
{
int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char *months[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
int first_day = day_of_the_week(1, month, year);
int array[35] = {0};

if(is_leapyear(year))
{
days_in_month[1] = 29;
}

array[first_day] = 1;

printf("%s, %i\n", months[month - 1], year);
printf("Su Mo Tu We Th Fr Sa\n");
printf("---------------------\n");
printf("%2d%3d%3d%3d%3d%3d%3d\n",array[0],array[1],array[2],array[3],array[4]
, array[5], array[6]);

}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
Alrighty, let's see what we've got here. If it has to show a blank instead of a zero, you know right there that a simple printf isn't going to cut it. I'd recommend nixing the array and using a simple loop. Keep track of the current day you're printing and the current day of the week. Print the current day you're on (or a blank space). Next, if the day of the week is Sunday, print a newline and reset the day of the week. The tricky part is starting on the right day of the week. I'm throwing stuff in spoilers, as I'd recommend trying to go with just the above. I just threw in some placeholders for the functions that weren't given.

Instead of starting at day 1, use the day of the week you calculated and go backwards, into the negatives. If the current day is zero or less, print a blank space; otherwise, print the integer day.

C:
#include <stdbool.h>
#include <stdio.h>

int day_of_the_week(int day, int month, int year);
bool is_leapyear(int year);
void print_calendar(int month, int year);

int main(){
        print_calendar(2, 2022);
        return 0;
}

int day_of_the_week(int day, int month, int year){
        return 2;
}
bool is_leapyear(int year){
        return false;
}
void print_calendar(int month, int year){
        int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        char *months[] = {"January", "February", "March", "April",
                "May", "June", "July", "August",
                "September", "October", "November", "December"};

        if(is_leapyear(year))
        {
                days_in_month[1] = 29;
        }

        int day = -day_of_the_week(1, month, year) + 1;

        printf("%s, %i\n", months[month - 1], year);
        printf("Su Mo Tu We Th Fr Sa\n");
        printf("---------------------\n");
        const int last_day = days_in_month[month-1];
        for (int dow=1;day <= last_day; day++, dow++){
                if (day > 0){
                        printf("%3d", day);
                } else {
                        printf("   ");
                }
                if (dow == 7){
                        printf("\n");
                        dow = 0;
                }
        }
        printf("\n");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top