Hi i have an assignment to do in C language but i have no idea where to start with....could anyone give me a headstart where i should start?? i dont have much experience in C programming.

i was given these information:
(a) It was Monday on the 1st of January 1900,
(b) April, Jun, September and November have 30 days,
(c) January, March, May, July, August, October and December have 31 days, and
(d) Feburary has 28 days normally, but in a leap year, it has 29 days.

Based on the general information about a calendar above, write a program that reads two dates and a day, and returns how many months within the given period start with the given day. (Note
that the first date could be before or after the second date.)
For example:
(Prompt) Period 1 (YYYYMMDD)
(User) 20130101
(Prompt) Period 2 (YYYYMMDD)
(User) 20131201
(Prompt) Day (0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thur, 5=Fri, 6=Sat)
(User) 2
(Output) 2

I've started by making the main method with the printf and scanf which reads the input of the user but i have no idea where to go next could anyone help me get started?

## All 4 Replies

should i first start off by writing the codes for a calendar?

I'd start by assuming that the day in the range is always 01 (the first day of the month). This isn't an actual requirement, but it simplifies things initially. One question I'd ask is if the day is beyond 01, does that month count, or do you skip to the next month?

From there you can loop through each month of each year in the range and calculate the first weekday of the month. This calculation can actually be done with minimal effort and no loops.

As a huge help for you, I'll offer something from the internals of my standard C library that finds the first weekday given a year and month:

``````#define LEAP_YEAR(year)  (((year) > 0) && !((year) % 4) && (((year) % 100) || !((year) % 400)))
#define LEAP_COUNT(year) ((((year) - 1) / 4) - (((year) - 1) / 100) + (((year) - 1) / 400))

const int yeardays[2][13] = {
{-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
{-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
};

int first_weekday(int year, int month)
{
int ydays, base_dow;

/* Correct out of range months by shifting them into range (in the same year) */
month = (month < 1) ? 1 : month;
month = (month > 12) ? 12 : month;

/* Find the number of days up to the first day of the month */
ydays = 1 + yeardays[LEAP_YEAR(year)][month - 1];

/* Find the day of the week for January 1st */
base_dow = (year * 365 + LEAP_COUNT(year)) % 7;

/* Shift to month 1st and cycle to the correct weekday */
return (base_dow + ydays) % 7;
}
``````

Remaining parts include parsing the YYYYMMDD format into component integers, and then looping over the range to check each month's first weekday. Don't forget to validate both the input and the resulting date to ensure that it's valid.

i also thought about if the day beyond 01 really matters because infact i'm only looking for the first day of each month and i think that it doesnt matter what the user inputs. thanks for your help but what is the difference between LEAP_YEAR and LEAP_COUNT?

what is the difference between LEAP_YEAR and LEAP_COUNT?

`LEAP_YEAR` tells you if the given year is a leap year. `LEAP_COUNT` tells you how many leap years there have been up to the current year. Play with them to see what the results are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.