Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have given a task in which I am given data dates in integer format e.g
39083 (= 1/1/2007)

I need to check for dates of same month to find monthly average. I have to only solve it using
cmath.h
functions. other built in functions related to date/time are not allowed. i am using visual studio.

What I have tried:

I am unable to think a logic for it.
Posted
Updated 16-Oct-18 3:11am
Comments
KarstenK 16-Oct-18 12:28pm    
Pay attention to leap years, search for the complete formula, because there are some excemptions!!!

Assuming that the integer value is "days since 1/1/1900", then you have to convert that to a date.
So each leap year has 366 days, and each non-leap year has 365. Leap years are all those years divisible by 4 in the range 1900 to 2099, so I'd create an array of "year counts"
0, 365, 365+365, 366+365+365, 365+365+365+366 ...
(Because 1900 was not a leap year - century end correction - but 2000 was - millenium end correction).
I'd then use that to check on the year number but subtracting each value in the array from the date code: it's the last year with a positive or zero subtraction.
The value you get as the subtraction is the day number in the year, so just do the same with the months (allowing for leap years, two "cumulative months" arrays is the easiest way)
The value that leaves is the day of the month.
 
Share this answer
 
Comments
Faizan ul Hassan 16-Oct-18 8:25am    
I am very thankful for response. Your solution is good. But it would be a favour if you can provide an algorithm or math formula to get at least year from integer.
OriginalGriff 16-Oct-18 8:32am    
I did ...
Faizan ul Hassan 16-Oct-18 8:36am    
You have done with array. But i am requesting for a formula to get value at once
Quote:
How to extract day, month and year from integer (following 1900 date system)

You don't extract, you calculate or convert.
39083 (= 1/1/2007)

This is looking like the encoding used by Excel.
This kind of date coding is simply counting the number of days between a reference date and the date you want.
Quote:
I am unable to think a logic for it.

The number of days in a year is known, the rules for leap year are known, the number of days in a month are known.
you just have apply the rules.
Train yourself with a sheet of paper and a pencil, solve sample dates by hand, the method you use is your algorithm.

HomeWork problems are simplified versions of the kind of problems you will have to solve in real life, their purpose is learning and practicing.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.
 
Share this answer
 
Comments
Faizan ul Hassan 16-Oct-18 15:53pm    
Dear! I know the option of counting and I have applied it. but I was thinking there would be some mathematical formula. However thanks for your response.
Patrice T 16-Oct-18 17:17pm    
"I am unable to think a logic for it."
Then show your code, ask for optimizing.
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900