Solved

Project question

Posted on 2004-10-21
161 Views
Last Modified: 2010-04-15
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.

Any help you can give would be useful.
0
Question by:robukaps7
    2 Comments
     
    LVL 55

    Accepted Solution

    by:
    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.

    Then it is your turn.
    Go ahead!

    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    > 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    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 opening and reading files in the C programming language.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now