Thread: Day of year?

1. Day of year?

Hey guys...Happy Thanksgiving!!

Well as for me I am at home doing homework, and I ran into a bit of a snag. The problem is I have to return an integer value ( 1 to 366) when entering the month, day, and year.

I am lost on the algorithm I would use!!!

I thought about entering the number of days for each month in an array and add those with the amount of days. Would that work? Or is there a better way to do this problem?

CJ

2. Re: Day of year?

Originally posted by CrackerJack
I thought about entering the number of days for each month in an array and add those with the amount of days. Would that work?
Try it and find out. Run through the logic in your head or on paper. Then turn it into code.

Quzah.

3. Well it seems like it would work. Trying to work on the code now. Except then I really would not need the year except for the leap years right?

Which would add a day if I am not mistaken.

CJ

4. Yes. You'll need to figure out some way to ask if the year is a leap year or what not. Otherwise everything past Feb 28th will be 1 day off.

Quzah.

5. It seems as though it is working, so I am hapy there. However, where would I start to through in the leap year if?

CJ

Code:
```#include <stdio.h>

int day_of_year(int a, int b, int c);

main()
{
int day=0, month=0, year=0;

printf("Enter the month in number form (ex. January being '1') : ");
scanf("%d", &month);
printf("Enter the date : ");
scanf("%d", &day);
printf("Enter the year : ");
scanf("%d", &year);

printf("The day of the year you selected was %d \n", day_of_year(month, day, year) );

}

int day_of_year(int a, int b, int c)
{
int i=0, daymon=0, dayday=0;
int mth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

dayday = b;

for (i = 0; i < a; i++)
{
daymon += mth[i];
}

return (daymon + dayday);
}```

6. Well since you're actually asking the year, you can simply determine if it's leap year from that number.

Quzah.

7. Several points:

Learning calendrics is nice, but it is already a done deal.
Plus, most people do not understand the Gregorian calendar shift\
and the interesting and varying state of dates after the 1580's in Europe.

More important - why does your prof have you doing basic stuff that standard C handles? - see struct tm in time.h

Most important point: you guys should NOT be doing time and date operations with algorithms you create, you should use proven code for that. In the real world, messing with money and dates - and screwing either one up - gets you fired. Fast.

8. Originally posted by jim mcnamara
Most important point: you guys should NOT be doing time and date operations with algorithms you create, you should use proven code for that. In the real world, messing with money and dates - and screwing either one up - gets you fired. Fast.
Get real. The lesson is to teach how to think a bit for yourself, and how to do various basic C lessons all rolled into one.

It's nice that you're a teacher and all, except for the fact that you're a moron.

By your logic, people should never learn the standard "Hello World" application, because "it's been done".

How the hell are people supposed to learn to program if people like you tell them not to cover the basics?

"Just jump right into kernel programming for your first assignent. All that stuff on linked lists, dynamic memory allocation, arrays, basic data types, basic IO, all that's been done. Just skip it."

Yeah, that makes lots of sense.

Quzah.

9. Jim I understand where your coming from, however I am a newbie to all this stuff. So, I think for me to understand all the logic is the best thing to accomplish first. I can always go back and learn functions later.

OK I have done this piece of code after searching. And from what I read the extra day is added on to the end of year total correct? So when entering 12/31/1996 it should give off a 366 day total.

CJ

Code:
```#include <stdio.h>

int day_of_year(int a, int b, int c);

main()
{
int mth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day=0, month=0, year=0;

printf("Enter the month in number form (ex. January being '1') : ");
scanf("%d", &month);

printf("Enter the date : ");

while ( scanf("%d", &day) != (day <= mth[month] + 1) || (day >= mth[month] + 1) )
printf("Not a valid date inside selected month! Try again! :");

printf("Enter the year : ");
scanf("%d", &year);

printf("The day of the year you selected was %d \n", day_of_year(month, day, year) );

}

int day_of_year(int a, int b, int c)
{
int i=0, daymon=0, dayday=0;
int mth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if ( (c % 4) || (c % 100) && (c % 400) )
mth[3] = mth[3] + 1;

for (i = 0; i < a; i++)
{
daymon += mth[i];
}

dayday = b;

return (daymon + dayday);
}```

10. Code:
```if ( (c % 4) || (c % 100) && (c % 400) )
mth[3] = mth[3] + 1;```
You'll want to fix this check. Otherwise what happens is c%4 is checked first, and the rest is ignored. Thus, those special cases dealing with centuries and leap year won't be accurate.

Also, don't you mean:
Code:
`if( ((c % 4 ) == 0) ...`
Otherwise, three out of four years will be a leap year.

And finally, the line:
Code:
`mth[3] = mth[3] + 1;`
Can be simplified as:
Code:
`daymon = 1;`
Just start your total at 1 instead of zero and you don't have to modify your array.

Quzah.

11. > int mth[13] = &#123;0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31&#125;;
You've written this array out twice

> printf("Not a valid date inside selected month! Try again! :");
You cannot enter Feb 29th, even if it's a leap year.
You need to input the year before you can properly validate the days within a month.

> mth[3] = mth[3] + 1;
I thought Feb was month 2

12. Originally posted by quzah
And finally, the line:
Code:
`mth[3] = mth[3] + 1;`
Can be simplified as:
Code:
`daymon = 1;`
Just start your total at 1 instead of zero and you don't have to modify your array.

Quzah.
1) Not so. This will make all of January and February one day off. But you don't want March to have 32 days, so you want to use either: