# Thread: Dam algorithm got me stuck

1. ## Dam algorithm got me stuck

Gotta make an algorithm that converts how many days into dd/mm/yy . Not sure how to do it without being really complex, any brainstorms?

2. Ok, let us not be very complex:

that converts how many days into dd/mm/yy
Simple, day 1 is represeted as 01/00/00. It easily follows that day 2 would be 02/00/00.

See, iffin you break down, very easy. btw, what date you have?

3. i'm not sure that i understand the questions. it seems to me that all you would have to do is run a few calculations since days are already the lowest common denominator in all of those. I suppose there could be a bit of confusion as to how many days you put in a month... that could cause significant differences in the days/months categories if you had a large number of days.

-Z

4. #1. Convert a number of seconds into the number of days, hours, minutes and seconds since the beginning of the year.

#2. Convert the number of days to a date within the year taking into account whether this is a leap year. Do not assume the information is for this year or any specific year.

5. I've got this only in hardcopy.

6. What language are you in?
There maybe existing functions that can do just what you're asking.

maybe something like CDATE() or something?

or maybe a derivative of the format function
Format(2323, 'dd/mm/yy')

7. We're using C++ but that's irrelevant. The idea is to make the algorithm's then combine them into a program, the programming's no problem, I just not sure how they want it answered, only resolution for the problem I can think of is lot's of IF and THEN statements......

8. So you need to convert a number less than or equal to 366 to a day from January 1st to December 31st? If this is incorrect please say so!!

You could make this a smaller function using recursion, or make a moderate function with if/then's.

Banti

9. I think I understand better, but there are some assumptions.

First what is the relative date that one starts from, this is needed for leap year calculation. Let's assume whatever number is from year 2001. Change assumption from reqs.

Now do you know what a julian date is? The corrupted one used in programming.

Anyway, take number of days to be calcuulated and divide by 365. Iffin number greater than 3, we need to worry about leap years, else, not. This will give us the year offset, so add to 2001.

Now comes the tricky part. We need to determine the month offset. this can be done many different ways. I'll try to explain one. First, set up an array of months and number of days in month. How leap years are implemented, I'll leave out.

Now, take remainder of days from year offset and start subtracting days o' month, until negative, keep track of HOW many months. It follows you have gone one month too far. Go back one month and you are done.

year = 2001 + yr offset
month = month from above calc
days = days left over from month calculation

The implementation of the leap year should be easier, if you solve the problem without them at first and retrofit the logic for leap year processing later.

10. Hi Hellmund,

Look at this code and tell me if it does what you want it to (You should be able to do what you want to with it). I've made a few assumptions.

1)that you use the time(0) function
2)that you do not require the construction of a date class etc.

It's now 2:30am in England and I've had a few beers, so if you want me to refine it then re-post with further instructions and I'll restructure the code to meet your requirements.

Details: Written in MS VC++ 6 under nt4
(sp6a)

laters,

Deo

code
-----------------------------------------

#include&lt;iostream.h&gt;
#include &lt;time.h&gt;

bool leapyear(int);
int calcdaysthisyear(int, int);
int calcmonthsthisyear(int, int);
int dayofthemonth(int, int);

void main(void)
{
const int secperday=60*60*24;
const int dayperyear=365;

int nsec=time(0);

int nday=static_cast&lt;int&gt;(static_cast&lt;double& gt;(nsec)/secperday);
int nyear=static_cast&lt;int&gt;(static_cast&lt;double &gt;(nday)/dayperyear);

int yr4=nyear+1970;

int daysthisyear=calcdaysthisyear(nday, yr4);

int monthnow=calcmonthsthisyear(daysthisyear, yr4);

int daythismonth=dayofthemonth(daysthisyear, yr4);

cout&lt;&lt;daythismonth&lt;&lt;"/"&lt;&lt;monthnow&lt;&lt;"/"&lt;&lt;yr4&lt;&lt;endl;
int yr2=yr4%100;
cout&lt;&lt;daythismonth&lt;&lt;"/"&lt;&lt;monthnow&lt;&lt;"/"&lt;&lt;yr2&lt;&lt;endl;
}

//function to return the dy of the month
//accepts the days this year and the year in yyyy format
int dayofthemonth(int day, int year)
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year))
month[2]=29;
int i=0;
while(day&gt;month[i])
{
day-=month[i];
i++;
}
return day;
}

//returns the month number (1-12) and requires
//number of the day this year, and year in yyyy format
int calcmonthsthisyear(int ndaysthisyr, int year)
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(leapyear(year))
month[2]=29;
int i=0;
while(ndaysthisyr&gt;month[i])
{
ndaysthisyr-=month[i];
i++;
}
return i+1;
//+1 to handle the 0 based array
}

//returns the day number - needs number of days since 01/01/1970
//and the year in yyyy format
int calcdaysthisyear(int ndays, int thisyear)
{
int yr=1970;
int daysthisyr=0;
if(leapyear(thisyear))
daysthisyr=366;
else
daysthisyr=365;

while(ndays&gt;daysthisyr)
{
if(leapyear(yr))
ndays-=366;
else
ndays-=365;
yr++;
}
return ndays+1;
//otherwise it returns the number of complete days ellapsed
}

//function to tell if leap year or not
// - requires year in yyyy format
bool leapyear(int year)
{
if(year%4==0 && year%100!=0)
return true;
if(year%100==0 && year%400==0)
return true;
else
return false;
}

--------------------------------------

[This message has been edited by deob197 (edited 08-22-2001).]

11. Actually I don't want it IN C++, thing is that I recently changed course and have to basically do the same course again in C++, I've already done it and if I use functions/recursions/strings/pointers etc it won't be accepted....part of what's making this so hard
Basically I've had a quick look over the lecture notes and they havn't even done functions yet. So basically I think I'll have to make it extremely long by the looks....
Basically the leap year isn't a problem, apparently you only have to make whether it's a leap year as user input. Then just add an IF THEN statement and it should be fine. I was just concerned that basically I'm going to end up having to do something like

temp = days - (365-31)
IF (temp &gt; 1)
{
Month = 12;
return month;
)

And so on with this sort of thing.......couldn't even use the switch command, switch with a few break statements would've made it easier!

12. Hellmund,

Forgot to say last night that I haven't tried the function for pre 1970 dates - not sure that it matters though! I could possibly convert that program to another language if you wanted, it just depends which one!

Regards,

Deo

13. assuming you are given
int num_of_days
bool leap_year

int month_list[11]={31,28....}
int month=1;

if (leap_year)
{
...month_list[1]=month_list[1]+1;
...if (num_of days&gt;366)
...{
.......num_of_days=366;
...}
}
else
{
...if (num_of days&gt;365)
...{
.......num_of_days=365;
...}
}

for(int i = 0; num_of_days &gt;= month_list[i]; i=i+1)
{
num_of_days=num_of_days-month_list[i];
month=month+1;
}

If you need to check for leap year ...

assume given
int year;

bool leap_year=FALSE;

if (year/4==0)
{
...if (year/100==0)
...{
.......if(year/400==0)
.......{
..........leap_year=TRUE;
.......}
...else
...{
.......leap_year=TRUE;
...}
}

These can easily be tranlsted into Pascal, java, Basic, etc...

Banti

~~EDIT added '.' since the whitespace is deleted

[This message has been edited by Banti (edited 08-23-2001).]

14. I'll go out of my way and find the lecturer at uni tomorrow and get this done, it's due in 4 days.....From the last assignment which was really simple and EXTREMELY long and repetitive It would seem she wants a massive If and Then program. I'll post what I find out.

I'll probaly be in this forum a lot more now

15. Hey Hellmund!
Well, I'll probably join you here too!
I'm taking Data Structures using C++ and Assembly!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q