Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Project question

Posted on 2004-10-21
Medium Priority
163 Views
I am trying to begin a program for one of my classes, but I am just a blank.  I was wondering if you could give me some pointers to start me off.  Here is what it consists of:

Leap year function:  write a function that checks the year and returns 0 for a non leap year and 1 if it is.

Valid date function:  checks the year month and day, for a valid date return 1, and 0 for a non valid date.

day in year function:  returns the day of the year ex. feb 14 would be 45 depending on  a leap year.

first date function:  checks two dates entered, if any are in valid print it out.  Write out the date that comes first.

elapsed days function: enter two dates, make sure both dates are valid. ex. 1/30/2002 and 2/2/2002 four days have elapsed.

0
Question by:robukaps7
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 55

Accepted Solution

Jaime Olivares earned 2000 total points
ID: 12375663
first of all, you have to extract the date from an char array, and store in separate day/month/year to evaluate.
You can use strtok as a first approach:

char *datestr = "2004-07-25";   // test date
int day, month, year;
char *token;

token = strtok(datestr, "-");
year = atoi(token);
token = strtok(NULL, "-");
month = atoi(token);
token = strtok(NULL, "-");
day = atoi(token);

This is your true stating point.

To test leap year, just have to detect year every 4 years, if year is multiple of 4, the it is leaf, your can use de modulus (%) operator to do this.
To valid date, just have to make multiple ifs: if (day>=1 && day<=31), etc.
To obtain day in year, and to refine your date validation, you will need an array containing days per month. Something like:
int dpm[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31,30, 31 };
Then you have to acumulate days for every month until you reach actual month, and then sum actual day.
For date lapse, you will need the dpm array again.

0

LVL 9

Expert Comment

ID: 12378285
> To test leap year, just have to detect year every 4 years, if year is multiple of 4, the it is leaf, your can use de modulus (%) operator to do this.
not always. years in multiples of 100 have to be multiples of 400 not just 4 to be leap yrs. eg. 2000 was leap yr but 1900 wasn't.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month10 days, 14 hours left to enroll