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?
Ok, let us not be very complex:
Simple, day 1 is represeted as 01/00/00. It easily follows that day 2 would be 02/00/00.
that converts how many days into dd/mm/yy
See, iffin you break down, very easy. btw, what date you have?
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.
what exactly are you asking?
#1. Convert a number of seconds into the number of days, hours, minutes and seconds since the beginning of the year.
Already done this
#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.
I've got this only in hardcopy.
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
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......
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.
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.
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
int calcdaysthisyear(int, int);
int calcmonthsthisyear(int, int);
int dayofthemonth(int, int);
const int secperday=60*60*24;
const int dayperyear=365;
int nday=static_cast<int>(static_cast<double& gt;(nsec)/secperday);
int nyear=static_cast<int>(static_cast<double >(nday)/dayperyear);
int daysthisyear=calcdaysthisyear(nday, yr4);
int monthnow=calcmonthsthisyear(daysthisyear, yr4);
int daythismonth=dayofthemonth(daysthisyear, yr4);
//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)
//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)
//+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)
//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)
if(year%100==0 && year%400==0)
[This message has been edited by deob197 (edited 08-22-2001).]
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 > 1)
Month = 12;
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!
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!
assuming you are given
...if (num_of days>366)
...if (num_of days>365)
for(int i = 0; num_of_days >= month_list[i]; i=i+1)
If you need to check for leap year ...
These can easily be tranlsted into Pascal, java, Basic, etc...
~~EDIT added '.' since the whitespace is deleted
[This message has been edited by Banti (edited 08-23-2001).]
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
Well, I'll probably join you here too!
I'm taking Data Structures using C++ and Assembly!
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
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke