problem with leap year to count. lazarus / 62-General ---------------------------------------------------------------------- mikex · 2014-11-21 22:58 Hi there I have written program for school work 2 times but i cant figure out the problem. First time my program didn't counted dates like 1.1.2005 - 1.1.2007 Then I fixed that by deleting  small part and rewriting it but now it dont count the leap year .. i need it exactly from calendar .. 2012 .. 2016.. but its impossible task for me now. I have no time left to try it and fail gain.  Commentery and counters are in my language but it does not rly matter ... Any kind of help with that would be lovely. Thanks. var d1,d2,m1,m2,r1,r2,i,x,r1i,r2i: integer;     mesic31, mesic30: set of byte;     dni,dni2: longint;     v:array[1..12] of integer;   begin write('den1='); readln(d1); write('mesic1=');readln(m1); write('rok1=');readln(r1); write('den2=');readln(d2); write('mesic2=');readln(m2); write('rok2=');readln(r2); mesic31:=[1,3,5,7,8,10,12]; {mesice ktere maji 31 dni} mesic30:=[4,6,9,11];          {mesice ktere maji 30 dni}   r1i:=r1;r2i:=r2;       for i:=1 to 12 do begin     if i in mesic31 then     v:=31;     if i in mesic30 then     v:=30;     if i=2 then      {unor ma 28 dni}     v:=28; end;                     {rozdelime program do dvou casti: dni a dni2 , dni v rozmezi jednoho roku nebo vice let} dni:=0; for i:=m1 to 12 do         dni:=dni+v;         dni:=dni-d1; for i:=m2-1 downto 1 do         dni:=dni+v;         dni:=dni+d2; r2:=r2-1;   x:=(r2-r1) div 4;               {jednou za 4 roky pridame jeden den prestupniho roku} dni:=dni+(365*(r2-r1))+x;   dni2:=0;   for i:=m1 to m2-1 do          dni2:=dni2+v;          dni2:=dni2-d1+d2; if r1i=r2i then writeln('pocet dni je ',dni2) else writeln('pocet dni je ',dni);   readln; end. ---------------------------------------------------------------------- engkin · 2014-11-22 01:14 Number of days in the 2nd month of any year is a function to the year itself. It seems to me that your code assumes a fixed number: 28 days. Code: [Select]  for i := 1 to 12 do   begin ..     if i = 2 then {unor ma 28 dni}       v[i] := 28; //<------   end; When you use it later in this part: Code: [Select]  for i := m1 to 12 do     dni := dni + v[i]; You should consider r1. While for next part: Code: [Select]  for i := m2 - 1 downto 1 do     dni := dni + v[i]; You should consider r2. This calculation is an approximation, but is not correct for your purpose. Code: [Select]  x := (r2 - r1) div 4; {jednou za 4 roky pridame jeden den prestupniho roku}   dni := dni + (365 * (r2 - r1)) + x; For instance, when r1 is a leap year consider the effect of having d1, m1 prior to the leap day of that year vis. after that day. On the other hand, not every four years include a leap year. ---------------------------------------------------------------------- Windsurfer · 2014-11-22 09:15 I have not read your code, but a year is a leap year if it is divisible by 4 unless it is divisible by 100 unless it is divisible by 1000. Note the second 'unless'. This means that all years divisible by 1000 are NOT leap years. This is a common mistake. ---------------------------------------------------------------------- engkin · 2014-11-22 09:42 Quote from: Windsurfer on November 22, 2014, 10:15:37 amI have not read your code, but a year is a leap year if it is divisible by 4 unless it is divisible by 100 unless it is divisible by 1000. Note the second 'unless'. This means that all years divisible by 1000 are NOT leap years. This is a common mistake. It is 400, not 1000. I know you won't take my word for it  , so here is Wikipedia: Quoteif (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year) ---------------------------------------------------------------------- marcov · 2014-11-22 16:58 Dateutils has Code: [Select]Function IsInLeapYear(const AValue: TDateTime): Boolean; ---------------------------------------------------------------------- engkin · 2014-11-22 18:45 Quote from: marcov on November 22, 2014, 05:58:19 pmDateutils has Code: [Select]Function IsInLeapYear(const AValue: TDateTime): Boolean; Can't be used in school assignment. ---------------------------------------------------------------------- Bart · 2014-11-22 22:13 Quote from: engkin on November 22, 2014, 07:45:12 pmQuote from: marcov on November 22, 2014, 05:58:19 pmDateutils has Code: [Select]Function IsInLeapYear(const AValue: TDateTime): Boolean; Can't be used in school assignment. But function can be copied to impress teacher. Bart ---------------------------------------------------------------------- BrunoK · 2014-11-23 11:05 Or a different one. Code: [Select]  function Fun_IsLeapYear(aYear: integer): boolean; begin   Result := (aYear and 3) = 0;   // Test simple case   if Result then begin     aYear:=aYear shr 2;          // Reduce (Divide by 4)     Result:=(aYear mod 25)<>0;   // Eliminate those that cannot     if not Result then           // ..be divided by 100, they are always leaps       Result:=(aYear and 3)=0;   // Those that can be divided by 4(00) are leaps   end; end; ---------------------------------------------------------------------- Windsurfer · 2014-11-23 16:42 Engkin,  thanks for the correction.  Thankfully I did know it correctly in 1999! The lesson for me is to check before posting. ---------------------------------------------------------------------- engkin · 2014-11-23 23:09 Quote from: Bart on November 22, 2014, 11:13:09 pmBut function can be copied to impress teacher. @Bart, your reply cheered me up, thanks. Impressing teachers would result in harder assignments! Quote from: Windsurfer on November 23, 2014, 05:42:22 pmThankfully I did know it correctly in 1999! @Windsurfer, retaining anything related to this for so long is nearly impossible for me.