### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: problem with leap year to count.  (Read 5371 times)

#### mikex

• New member
• Posts: 7
##### problem with leap year to count.
« on: November 21, 2014, 11:58:15 pm »
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

• Hero Member
• Posts: 2513
##### Re: problem with leap year to count.
« Reply #1 on: November 22, 2014, 02:14:58 am »
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

• Sr. Member
• Posts: 364
##### Re: problem with leap year to count.
« Reply #2 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.

#### engkin

• Hero Member
• Posts: 2513
##### Re: problem with leap year to count.
« Reply #3 on: November 22, 2014, 10:42:11 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.
It is 400, not 1000. I know you won't take my word for it  , 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)

#### marcov

• Global Moderator
• Hero Member
• Posts: 8803
• FPC developer.
##### Re: problem with leap year to count.
« Reply #4 on: November 22, 2014, 05:58:19 pm »
Dateutils has

Code: [Select]
`Function IsInLeapYear(const AValue: TDateTime): Boolean;`

#### engkin

• Hero Member
• Posts: 2513
##### Re: problem with leap year to count.
« Reply #5 on: November 22, 2014, 07:45:12 pm »
Dateutils has

Code: [Select]
`Function IsInLeapYear(const AValue: TDateTime): Boolean;`
Can't be used in school assignment.

#### Bart

• Hero Member
• Posts: 3955
##### Re: problem with leap year to count.
« Reply #6 on: November 22, 2014, 11:13:09 pm »
Dateutils 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

• Sr. Member
• Posts: 267
• Retired programmer
##### Re: problem with leap year to count.
« Reply #7 on: November 23, 2014, 12:05:51 pm »
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

• Sr. Member
• Posts: 364
##### Re: problem with leap year to count.
« Reply #8 on: November 23, 2014, 05:42:22 pm »
Engkin,  thanks for the correction.  Thankfully I did know it correctly in 1999!
The lesson for me is to check before posting.

#### engkin

• Hero Member
• Posts: 2513
##### Re: problem with leap year to count.
« Reply #9 on: November 24, 2014, 12:09:33 am »
But function can be copied to impress teacher.
@Bart, your reply cheered me up, thanks. Impressing teachers would result in harder assignments!

Thankfully I did know it correctly in 1999!
@Windsurfer, retaining anything related to this for so long is nearly impossible for me.