Review required for program of date difference

This is a discussion on Review required for program of date difference within the C Programming forums, part of the General Programming Boards category; hello, i created a simple program to calculate date difference. Please review it and suggest optimisations, if any. Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Review required for program of date difference

    hello,
    i created a simple program to calculate date difference. Please review it and suggest optimisations, if any.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void cal_date_diff(int day1,int month1,int year1,int day2,int month2,int year2)
    {
     int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //respective days in month
     int d,i=month1+1; //initialising date difference and i
     if(month1==2 && ((year1 &#37; 4==0 || year1 % 100==0) && year1 % 400!=0))
    	d=29-day1;
     else
    	d=m[month1]-day1;
    
     if(year1==year2 && month1==month2)
     {
    	printf("The Difference is %d",abs(day1-day2));
    	return;
     }
     if(i==13)
    	i=1;
     while(1)
     {
    	while(i<=12)
    	{
    		if(year1==year2 && i==month2) //calculation over since both years same
    										//and months too
    		{
    			d+=day2;
    			printf("The Difference is %d",d);
    			return;
    		}
    		if(i==2 && ((year1 % 4==0 || year1 % 100==0) && year1 % 400!=0))
    						//leap year so count 29 days
    			d+=29;
    		else
    			d+=m[i];
    		i++;
    	}
    	i=1;
    	year1++;
     }
     over:
    
    }
    
    int main(void)
    {
     int day1,day2,month1,month2,year1,year2;
     day1=30; month1=7; year1=1988;
     day2=22; month2=8; year2=1988;
     if(year1>year2)
    	cal_date_diff(day2,month2,year2,day1,month1,year1);
     else if(year2>year1)
    	cal_date_diff(day1,month1,year1,day2,month2,year2);
     else
     {
    	if(month1>month2)
    		cal_date_diff(day2,month2,year2,day1,month1,year1);
    	else
    		cal_date_diff(day1,month1,year1,day2,month2,year2);
     }
     return 0;
    }
    Last edited by chottachatri; 10-31-2008 at 09:09 AM.

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    First, there is absolutely no need for a "goto". Instead, why not do this:

    Code:
    		if(year1==year2 && i==month2)                               
                    {
      	            d+=day2;
    	            printf("The Difference is %d",d);
                    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, perhaps, replace "goto over" with "break" which is less obviously a goto.

    But the real solution is to change the conditions on the while-loop in such a way that you exit when the conditions are true. E.g add the "month2 == i" on the inner while-loop.

    Code:
    d=m[month1]-day1
    I think this can go wrong if you are in a leap-year...

    Code:
    		int d=abs(day1-day2); //only difference in days so directly print
    Should that not be part of the date-difference function instead?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    I cannot write "month2 == i in the inner while-loop" because example if i enter 1st date as 22 6 1988 and 2nd date as 22 7 1989 then the inner loop won't get executed at all and it will increment the year without counting the days of july..december of 1988 and hence wrong answer.

    Any more suggestions? and please check it whether it produces correct answer or not
    Last edited by chottachatri; 10-30-2008 at 09:51 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    I cannot write "month2 == i in the inner while-loop" because example if i enter 1st date as 22 6 1988 and 2nd date as 22 7 1989 then the inner loop won't get executed at all and it will increment the year without counting the days of july..december of 1988 and hence wrong answer.
    So add the "year1 == year2" to the condition? Goto is NOT the right answer here.

    Any more suggestions? and please check it whether it produces correct answer or not
    So, you are suggesting that I re-implement your code in a different form, validate it, and check that your code produces the correct result, or do you have any other suggestion for validating your code - isn't that YOUR task?

    Notes on your edited post:
    Code:
     if(month1==2)
    	d=29-day1;
     else
    	d=m[month1]-day1;
    You need to check if it's a leapyear or not for february calculation - your current code assumes it is ALWAYS a leap-year. I personally would write a small function that produces "days in month" based on year and month.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Oops...! i wrote it by mistake in hurry to correct it. I'll modify it

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not use time.h to perform these comparisons?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing Days
    By nhubred in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 07:22 PM
  2. Advancing day by day until it matches a second date
    By nhubred in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2009, 09:55 AM
  3. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 08:24 AM
  4. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  5. CDate Class - handle date manipulation simply
    By LuckY in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21