2

We are given Birthdate and Currentdate as input and we have to find age in years, months and days, ignoring leap years.

Here is my current code (its incomplete and some things are missing) :

#include <stdio.h>

void main() {
    int cy, cm, cd, by, bm, bd, ay, am, ad;
    int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    printf("\nEnter Current Date (dd/mm/yyyy) ->  ");
    scanf("%d/%d/%d", &cd, &cm, &cy);
    printf("\nEnter Birth Date (dd/mm/yyyy) ->  ");
    scanf("%d/%d/%d", &bd, &bm, &by);
    if (cm > bm) {
        ay = cy - by;
        //bm = month[bm - 1];
    } else
    if (cm < bm) {
        ay = cy - by - 1;
    } else {
        if (bd >= cd) {
            ay = cy - by;
            ad = bd - cd;
        } else {
            ay = cy - by - 1;
            ad = cd - bd;
        }
    }
}

How should I go about calculating days and months? I don't want to use libraries like <time.h> etc. and want to do it manually first.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 2
    Just on the side, did your teacher tell you to write `void main()`? – DeiDei Oct 23 '16 at 14:40
  • 1
    This is much harder than it looks. Why don't you want to use the relevant standard library functions (mktime, strptime, difftime)? [This book will show you just how hard it is.](http://www.powells.com/book/calendrical-calculations-9780521702386/61-1) – zwol Oct 23 '16 at 14:43
  • @DeiDei Our teacher told us if we use int main() we'll have to return an integer value like 0. So I use void mostly. –  Oct 23 '16 at 14:43
  • Arguable duplicate of https://stackoverflow.com/questions/1381832/how-to-calculate-the-number-of-days-between-two-given-dates-leap-year-obstacle?rq=1 but I do not feel confident enough to apply the dupehammer. – zwol Oct 23 '16 at 14:44
  • @zwol We were told not to use any library functions in class. –  Oct 23 '16 at 14:44
  • what should be displayed for `1/1/2000 - 1/2/2000` and for `1/2/2000 - 1/3/2000`? are both 1 month? – Iłya Bursov Oct 23 '16 at 14:45
  • 5
    @M.SIngh You _must_ use `int main` and you _must_ end by returning 0 (specifically 0, until you get to the point where you care about indicating whether the program succeeded or failed). `void main` is accepted as an extension by some C compilers but is Wrong. – zwol Oct 23 '16 at 14:45
  • @M.SIngh OK, your teacher wants you to learn just how hard it is. See if your school's library has a copy of the book I linked to. You probably don't want to spend US$50 just for this assignment. – zwol Oct 23 '16 at 14:46
  • @Lashane 0 Years 1 Month 0 Days –  Oct 23 '16 at 14:49
  • 1
    So this is not about the language C, but an algorithm. I'm sure a simple search will show you a long list of matches. – too honest for this site Oct 23 '16 at 14:54
  • 1
    I thought that DOB (Date of Birth) _is_ "Birthdate", so you are done. – Paul Ogilvie Oct 23 '16 at 15:04
  • @PaulOgilvie thanks for pointing that out. Fixed the typo –  Oct 23 '16 at 15:07
  • @Zwol Using standard library functions (mktime, strptime, difftime) do help concerning leap year and text/data conversion, but otherwise do not help here given the asymmetric difference sought in Y,M,D. Your proposed dupe sought a difference in days – chux - Reinstate Monica Oct 23 '16 at 17:16

3 Answers3

2

taking @Jacob answer as starting point, you can implement this in following way:

ay = cy - by;
am = cm - bm;
ad = cd - bd;

if (ad < 0) {
    am--; // one month is not full month
    ad = month[bm - 1] - bd + cd; // can be simplified to ad += month[bm - 1];
}

if (am < 0) {
    ay--; // one year is not full year
    am = 12 - bm + cm; // am += 12; will work too
}

explanation of ad = month[bm - 1] - bd + cd;

  1. imagine two dates: 10/1/2000 - 5/2/2000, ad will be (5-10)=-5
  2. so, instead of wrong answer you should calculate days in january (31-10) and days in february 5 = 26

logic behind am = 12 - bd + cm is the same as for days, we need to calculate how many month was in previous year and add current month

why we can simply am calculation:

  1. am = cm - bm;
  2. am += 12;

=> am = cm - bm + 12 => 12 - bm + cm

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • 1
    Suggest simplification `ad = month[bm - 1] - bd + cd;` --> `ad += month[bm - 1];` – chux - Reinstate Monica Oct 23 '16 at 17:13
  • @Lashane: Can you contact me, please — see my profile? I have some issues to discuss that don't discuss well in comments (too much code, data). The issue is 'what value do you get from entering these dates?' Following what I think is your algorithm, I got: `Date 2017-01-27; DoB 2016-02-27; Age 0 y, 11 m, 0 d` —— `Date 2017-01-27; DoB 2016-02-28; Age 0 y, 11 m, 27 d` where a difference of 1 day in the date of birth (and same current date) is yielding 27 days difference in age. I assume I have something wrong, but I'd like to check. – Jonathan Leffler Oct 24 '16 at 00:34
0

It sounds like what you need is an age calculator, and it doesn't need to deal with leap years or any other problem concepts.

What you are looking for right now is just difference. Let's start with exactly that:

date: 10/11/2016
birth date: 4/8/2012
result == 2016-2012 years, 11-8 months, 10-4 days
== 4 years, 7 months, 6 days

And so with 3 simple subtractions, 4 years, 7 months, 6 days is the result. This is the easy part, in fact we can always start with this simple subtraction, this is step 1.

Now let us consider another case:

date: 10/4/2016
birth date: 21/9/2001
== 15 years, -5 months, -11 days

What we have is still technically correct, but you probably don't want negatives in your answer. There is a really easy fix to this: a year is the next unit up from months, so we can just move one year to the months column (this should look familiar, think back to grade 6 math).

   15y,-5m -11d
== 14y, -5m+12m, -11d
== 14y, 7m, -11d

Starting to see how this works? The last and hardest step is the days. Because there is variation in number of days between month, we need to check how many days were in the current month. In your code you use month[], so I will use this array name:

   14y, 7m, -11d
== 14y, 6m, -11d+month[7-1]
== 14y, 6m, -11d+31d
== 14y, 6m, 20d

And there you have your final answer, 14 years, 6 months, 20 days. There is only one case in which you could get a negative age now, and that is when the birth date entered is after the current date; you can choose to handle that however you like. There is one more case that could give you issues using this algorithm: you could get 0s. I will let you figure that out though, because I don't want to do all of your work for you here.

So to sum everything up:

  1. Subtract the birth date from the current date
  2. If the resultant months are negative, add 12 and subtract 1 from the resultant years
  3. If the resultant days are negative, add month[resultant months-1] days to them, and subtract 1 from resultant months

Good luck, hopefully this explains things well enough.

Jacob H
  • 864
  • 1
  • 10
  • 25
0
#include<stdio.h>
#include<conio.h>
main()
{
int cd,cm,cy,bd,bm,by;
int y=0, m=0 ,d=0;
clrscr();
scanf("%d%d%d",&bd,&bm,&by);
scanf("%d%d%d",&cd,&cm,&cy);

if(cd <= bd)
{
d = cd + 30 - bd;
m--;
}
else
d = cd - bd;


if( cm <= bm )
{
m = m +(cm +12 - bm);
y--;
}
else
m = cm - bm;

y = y +(cy - by);


if(d==30 && m==11)
{
d=0;
m=0;
y++;
}


printf(" %d %d %d",d,m,y);
getch();
}
Vikram Baliga
  • 444
  • 5
  • 9
  • this program assumes a month to be 30 days. – Vikram Baliga Oct 23 '16 at 15:41
  • In the standard Gregorian calendar, some months have 30 days, some have 31, one has 28 or 29. We won't go into discussions of ancient calendars (30th February 1712 — in Sweden; 19 days in September 1752 in.Britain and its colonies; etc.). – Jonathan Leffler Oct 23 '16 at 22:35
  • if we want that to be implemented, we need to replace 30 in the program with: months[cm-1]. where months is: int monts[12] = {31,0,31,30.....}; if(cy%4 ==0) months[1]=29; else months[1]=28; – Vikram Baliga Oct 24 '16 at 05:05