Sign in to follow this  

Newb Help

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im completely lost one how to make a program in C for this problem Write a program that determines the day number ( 1 to 36) in a year for a date that is provided as input data. As an example, January 1 , 1994, is day 1---December 31,1993 is day 365. December 31, 1996, is day 366, since 1996 is a leap year. A year is divisble by four except that any year divisble by 100 is a leap year only if it is divisble by 400. Yor program should accept the month, day and year as intergers. Include function leap that returns 1 if called with a leap year, 0 otherwise if anyone could help me it would be greatly apperciated.

Share this post


Link to post
Share on other sites
sounds awfully like class-work to to me...

What part has made you lost - how far did you get before giving up?

- Parsing a string-based date into numeric day/month/year?
- Determining if it is a leap year or not using the numeric year?
- Determining how many days are in each month?

Share this post


Link to post
Share on other sites
I have coded up an EXAMPLE program for you to examine. I have included comments to help you understand what I did. IF YOU ACTUALLY LIKE PROGRAMMING AND WANT TO LEARN THEN YOU WILL NOT SIMPLY TAKE THIS CODE AND HAND IT IN. Also, it may have errors as I have not actually compiled it. So, that being the disclaimer, here you go:


int leap (int year) {
if (year % 400 == 0) {
return 1;
else if (year % 4 == 0) {
return 1;
else
return 0;
}

int dayInYear (int month, int day, int year) {
int totalDays = 0;
int monthsWithThirtyDays [4] = {4, 6, 9, 11};

// Test for days above 28 or 29 (depending if leap year) in February.
if (month == 2 && day > 28 + leap (year)) {
return -1;
}

// If the month is past February, then make sure we subtract from the
// assumed 31
// days in a month so that we have 28 or 29 days instead.
if (month > 2)
totalDays = (totalDays - 3) + leap (year);

// Loop through the months with thirty days.
for (int n = 0; n < 4; n++) {
// If the month matches one of them and the day is above 30,
// exit.
if (month == monthsWithThirtyDays[n] && day > 30)
return -1;

// If the month is greater than one of the exceptions,
// then subtract one from the total.
if (month > monthsWithThirtyDays[n])
totalDays--;
}

// Assume all months have 31 days and calculate totalDays. The
// algorithms above should
// make the totalDays number negative because of the correction on the
// assumption that every month has 31 days.
totalDays = totalDays + (month - 1) * 31 + day;

return totalDays;
}

Share this post


Link to post
Share on other sites

This topic is 3720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this