Forum > General

problem with leap year to count.

**mikex**:

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**:

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: --- for i := 1 to 12 do

begin

..

if i = 2 then {unor ma 28 dni}

v[i] := 28; //<------

end;

--- End code ---

When you use it later in this part:

--- Code: --- for i := m1 to 12 do

dni := dni + v[i];

--- End code ---

You should consider r1.

While for next part:

--- Code: --- for i := m2 - 1 downto 1 do

dni := dni + v[i];

--- End code ---

You should consider r2.

This calculation is an approximation, but is not correct for your purpose.

--- Code: --- x := (r2 - r1) div 4; {jednou za 4 roky pridame jeden den prestupniho roku}

dni := dni + (365 * (r2 - r1)) + x;

--- End code ---

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**:

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**:

--- Quote from: Windsurfer on November 22, 2014, 10:15:37 am ---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.

--- End quote ---

It is 400, not 1000. I know you won't take my word for it :P, so here is Wikipedia:

--- Quote ---if (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)

--- End quote ---

**marcov**:

Dateutils has

--- Code: ---Function IsInLeapYear(const AValue: TDateTime): Boolean;

--- End code ---

Navigation

[0] Message Index

[#] Next page