## _dragonwolf_

I have tried different things to modify the following code and have been unsuccessful. The following code is to get the julian date number. I need to alter it to make it return the day number (between 1 - 366) within a given year. Can anybody help?
(fyi - this code was previously the julianDayNumber which I acquired from a previous assignment)

``````int a, yr, mnth;
int dayOfYear;

// calculate Julian day for a date
a = (14 - month) / 12;
yr = year + 4800 - a;
mnth = month + 12 * a - 3;

dayOfYear = day + (153 * mnth + 2) / 5 + 365 * yr + yr/4 - yr / 100 + yr / 400 - 32045;

return dayOfYear;``````

## VernonDozier 2,218

I have tried different things to modify the following code and have been unsuccessful. The following code is to get the julian date number. I need to alter it to make it return the day number (between 1 - 366) within a given year. Can anybody help?
(fyi - this code was previously the julianDayNumber which I acquired from a previous assignment)

``````int a, yr, mnth;
int dayOfYear;

// calculate Julian day for a date
a = (14 - month) / 12;
yr = year + 4800 - a;
mnth = month + 12 * a - 3;

dayOfYear = day + (153 * mnth + 2) / 5 + 365 * yr + yr/4 - yr / 100 + yr / 400 - 32045;

return dayOfYear;``````

You have a bunch of variables that you use that are obviously defined elsewhere, but we don't know what they are. What are you given as input and what exactly are you expecting to calculate. I see no variable called julianDayNumber, which I guess is the number of days elapsed since January 1, 4713 BC or something.

Seems like you have some old function that involves Julian Dating that you are using as some kind of skeleton, but it's unclear to me at least what you're looking for NOW and what the code you posted does. I think you need to be a little more descriptive and repost.

## _dragonwolf_

my program requires that i use input from a file via fstream. there is no variable named "julianDayNumber" because I changed it to "dayOfYear". What I am trying to do is alter the julianDayNumber calculations to get it to properly display the day number for a given day within a given year. For example in a NON-leap year January 1, "year" is going to be dayNumber 1 and December 31 "year" is going to be day number 365. However in a leap year, December 31 "year" is day number 366. That is what I'm trying to calculate. The day number of a given day within a given year. If this code cannot be altered to do that and I need a whole new equation, please let me know.

## VernonDozier 2,218

I think I am more confused now than I was before. You have a variable named "year" that you are assigning values from 1 to 366? Seems like that should be named "day".

Does this function still have anything to do with Julian dating at all? If not, I would imagine that you should ditch this line:

``dayOfYear = day + (153 * mnth + 2) / 5 + 365 * yr + yr/4 - yr / 100 + yr / 400 - 32045;``

So, what are your input variables, let's say for today: July 19, 2009? 2009 is a non-leap year. Are you supposed to take "July 19" and return this?

31 + 28 + 31 + 30 + 31 + 30 + 19 = 200?

## _dragonwolf_

the word year that is in quotation marks in my last post ("year" ) is just stating that whatever year you put there 2000, 2001, 2002, 1900 .... whatever year you put there.... I don't know what is so confusing about it....look I just need a code that will allow me to determine what the day number is of a given day within a given year. for example (again) January 1, 1983 is day 1. December 31, 1983 is day 365. However December 31, 1984 is day 366 because it is in a leap year. does that help what I am trying to say?

## VernonDozier 2,218

Got it. Throw out the Julian coding since it is irrelevant. Have an array of the number of days in the 12 months:

``int numDaysInMonth[12] = {31, 28, 31, 30, ....};``

If you have month, day and year as input variables:

``````int month = 7;
int day = 19;
int year = 2009;``````

You need to add up the number of days in the first 6 months ( `month-1 = 7 - 1 = 6` ). Do that by summing the first 6 elements of `numDaysInMonth` . That's 181. Then add `day` , which is 19, and get 200. Then figure out if `year` is a leap year. If it is and `month` is 3 or greater, add 1.

I forget the exact rules for leap year. It looks like you have them though. Every 4th year is a leap year unless it's a factor of 100. Something like that.

## _dragonwolf_

Okay. So I got that to work and it gives me the number of days within a given year (i.e. Leap year 366, and Non-Leap year 365). However, I am unsure as to how to get it to pinpoint the day number of a particular day. I just can't figure that math portion. I'm still workin with it though.

## VernonDozier 2,218

Okay. So I got that to work and it gives me the number of days within a given year (i.e. Leap year 366, and Non-Leap year 365). However, I am unsure as to how to get it to pinpoint the day number of a particular day. I just can't figure that math portion. I'm still workin with it though.

``````int month = 7;
int day = 19;
int year = 2009;

int numDaysInMonth[12] = {31, 28, 31, 30, ....};
int dayNumber = 0;

for (int i = 1; i < month; i++)
dayNumber += numDaysInMonth[i-1];

dayNumber += day;

// check if leap year.  If so, and month >= 3, add 1``````

## _dragonwolf_

Ok, now it works....kind of. For the first 2 months it will add up correctly to give me the correct day number. However, when It reads in "March 1, 1900" or whichever year that isn't a leap year that you give it, it starts to add more than is supposed to.

In a non leap year (i.e. 1900), March 1, 1900 should be day number 60. However, it displays 61. March 2, 1900 displays 63. December 31, 1900 displays 675.

Here is what I have for my class implementation function:

``````int Date::dayNumber()
{
int numOfDaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayNum = 0;
for(int i = 1; i < month; i++)
{
if((i >= 3 && i <= 12) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{
dayNum += numOfDaysInMonth[i-1];
dayNum += day;
}
else
{
dayNum += numOfDaysInMonth[i-1];
dayNum += day;
}
}
return dayNum;
}``````

I have no clue what can be wrong with it. I'm going to keep trying myself. Any insight may render useful. Thanks for all the help thus far, and to come.

## jencas 346

This code works (cut 'n paste it and try it on www.codepad.org):

``````#include <iostream>

using namespace std;

int dayNumber(int year, int month, int day)
{
int numOfDaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayNum = 0;
for(int i = 1; i < month; ++i)
{
dayNum += numOfDaysInMonth[i-1];
}
dayNum += day;
if((month >= 3 && month <= 12) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{
++dayNum;
}
return dayNum;
}

int main()
{
cout << dayNumber(2008, 1, 1) << endl;
cout << dayNumber(2008, 2, 28) << endl;
cout << dayNumber(2008, 3, 1) << endl;
cout << dayNumber(2008, 12, 31) << endl;
cout << dayNumber(2009, 1, 1) << endl;
cout << dayNumber(2009, 2, 28) << endl;
cout << dayNumber(2009, 3, 1) << endl;
cout << dayNumber(2009, 12, 31) << endl;
}``````

## Alibeg 1

ok...this guy gave you the answer and here's the explanation

you have made it a bit too complicated, and that's where you've made a mistake

``````if((i >= 3 && i <= 12) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{
dayNum += numOfDaysInMonth[i-1];
dayNum += day;
}
else
{
dayNum += numOfDaysInMonth[i-1];
dayNum += day;
}``````

what happens with non-leap year? In if statement else always occurs and you always add both current day and number of days in month to dayNumber. And with leap year? Again, both current day and number of days in month add to dayNumber.

So if statement does nothing. It acts the same for leap and non leap year.
Also, what you need to do is to remove that if statement completely out of the for loop because if statement should be done only once.
Also (main thing) you should use if statement to determine whether you: need one more day to add to dayNum (because of leap year) or not.

For loop is just used to do this:

``````for(int i = 1; i < month; ++i)
{
dayNum += numOfDaysInMonth[i-1];
}``````

And keep this in mind: `dayNum += day;` is done only once.

This is the Jencas's code, but just a bit explained.

## _dragonwolf_

This was the code that I had....

Ok, now it works....kind of. For the first 2 months it will add up correctly to give me the correct day number. However, when It reads in "March 1, 1900" or whichever year that isn't a leap year that you give it, it starts to add more than is supposed to.

Here is what I have for my class implementation function:

``````int Date::dayNumber()
{
int numOfDaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayNum = 0;
for(int i = 1; i < month; i++)
{
if((i >= 3 && i <= 12) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{
dayNum += numOfDaysInMonth[i-1];
dayNum += day;
}
else
{
dayNum += numOfDaysInMonth[i-1];
dayNum += day;
}
}
return dayNum;
}``````

Here is what I changed it to....

``````int Date::dayNumber()
{
int numOfDaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayNum = 0;
for(int i = 1; i < month; i++)
{
if((i >= 3 && i <= 12) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{
dayNum += numOfDaysInMonth[i-1];
//			dayNum += day;    //had to erase this one
}
else
{
dayNum += numOfDaysInMonth[i-1];
//			dayNum += day;    //had to erase this one
}
}
return dayNum += day;    // put the '+= day' on the return portion.  Who knew.
}``````

As I learn with every new assignment, its all in the placement of the code.

## Alibeg 1

And it works?
I mean, there should be a part where you add one day to the dayNum when you have a leap year, and `dayNum += numOfDaysInMonth[i-1];` should be executed regardless of the leap year (so it can be removed from the if statement)

also if statement shouldnt be in for loop because that way if you have a leap year you will increase it by one every time loop repeats...

VernonDozier had explained it but it seems that you misunderstood him.
Jencas's code is working one (there are also other methods):

``````#include <iostream>

using namespace std;

int main (void)
{
int day, month, year;
int numOfDaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayNum = 0;

day = 14;
month = 1;
year = 1999;

for(int i = 1; i < month; ++i)
{
dayNum += numOfDaysInMonth[i-1];
}
dayNum += day;
if((month >= 3 && month <= 12) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
{
++dayNum;
}

printf ("Number of day in year is: %d\n", dayNum);
}``````

## _dragonwolf_

Yes the code works. Don't sound so surprised. I'm working with user defined classes. I can write a simple function that determines the 'dayOfYear' number and then use my function that I have that determines whether or not it is a leap year or not. If it is I can take the 'dayOfYear' function and add 1 to it. If it is not a leap year then just display what the class function produces.

I know I'm a beginner, but you have to give me more credit than what you do. I do know what the hell I'm doing for the some part. Trust me, if it works, it works. So please, don't insult or degrade me.