d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Struggling On Exercise
Add Reply New Topic New Poll
Member
Posts: 3,659
Joined: Jul 6 2008
Gold: 153.00
Nov 1 2014 08:59am
So, in this exercise I need to put the day, month and year and I need to receive what is the number of that day in that year.

Example:

Sample Input
7 02 2008
Sample Output
38

I started with:

int order_of_year(int day, int month, int year)

{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31;

else if (month == 4 || month == 6 || month == 9 || month == 11 )
return 30;

else // month == 2

if( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return 29;

else
return 28;

?
? ( maybe a "for cicle?") ??
?

}

int main(void)
{
int day, month, year;
scanf("%d %d %d", &day, &month, &year);
printf("%d\n", order_of_year(???, probably i ?);
return 0;
}

That's all I could do! Now I'm really struggling because I think I need to use a for cicle, but I don't know how to make it right!



Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 1 2014 11:40am
you have a few options. i'll look at two of them.

1) store the number of days in each month. then loop over all previous months and add their number of days together, then add the current day of month.

this could work, but you're looping for every month just to add a fixed quantity you know before hand. so how about we change it a little bit? instead of storing the number of days in each month, how about we store the sum of days of the previous month? then you can simply add the current day of the month.

example, jan is 0 since there are no months before it. if user chooses jan 5, then the day of year is aggregates[jan] + 5 = 0 + 5 = 5.

aggregates[feb] = 31 since there are 31 days before it. if user chooses feb 7, then the day of year is aggregates[feb] + 7 = 31 + 7 = 38.

aggregates[march] = 59

and so forth.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll