Monday, May 24, 2010

Why this c program is going infinite!!!!!!?

Program is to find no. of days between 2 dates.Logic is to add 1 day to 1 date till it reaches other.


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int a,b,c,d,e,f,flag=0;


printf("Enter first date");


scanf("%d%d%d",%26amp;a,%26amp;b,%26amp;c);


printf("Enter second date");


scanf("%d%d%d",%26amp;d,%26amp;e,%26amp;f);





while(c!=f||b!=e||a!=d) //If I comment c!=f this program works otherwise it goes infinite!





{


a++;


if((b==4)||(b==6)||(b==9)||(b==11))


f=30;


else if(b==2)


{


if(d%4==0)


f=29;


else


f=28;


}


else


f=31;


if(a%26gt;f)


{


b++;


a=1;


}


if(b==13)


{


c++;


b=1;


}


flag++;





}


printf("%d",flag);


getch();


}

Why this c program is going infinite!!!!!!?
Why don't you first calculate the julian value and then just subtract the two dates?





For example, if they enter 2007 01 31 for the first date and


2007 02 10 for the second, the first date would calculate to 2007031 and the second is 2007041. Subtracting, you then get 10 days.





The following hasn't been compiled or checked for complete accuracy, but should give an idea.








int main() {


int date1, date2, daysDiff=0;


/* blah blah blah - get your dates... */





date1 = jetJulian (a,b,c);


date2 = jetJulian (d,e,f);





if (date1%26lt;date2)


daysDiff = date2-date1;


else


daysDiff = date1-date2;





}





int getJulian(int year, int mo, int day) {


int jValue=0;


int moDays[11]={0,31,59,90,120,150,181,212,2...


/* Double-check the values above :) */





/* Add some verifications prior to the following */


jValue = moDays[mo-1]+day;





/* Add one if the mo%26gt;2 and leap year */


if ((year%4 %26amp;%26amp; !year%100) || year%400)


jValue++;





jValue+= year*1000;


return jValue;


}
Reply:i did this program


its to find persons age


%26amp; hw many days he lived


its very simple


first u substracted the old year from recent year


than answer*365


other line


subtract large month to smaller 1


than answer*30


than just add the days


ok that was mine


i wont do ur prob


i might get answer 2moro


when i go to my class


No comments:

Post a Comment