Code:
#include <iostream>
using namespace std;
//-- const unsigned REFORM_YEAR = 1582;
bool is_leap(const int);
void print_leap_excl_range(int, int);
//-- int main()
{
int start, end;
//Boht are leap years, but wont be printed.
start = 800;
end = 2008;
//Printing *closed* range. So (excl)usive start/end.
print_leap_excl_range(start, end);
system("pause");
return 0;
}
//------------------------------------------------ bool is_leap(const int yr)
{
if( yr % 4 ) return false;
if( yr < REFORM_YEAR ) return true;
if( yr % 100 ) return true;
if( yr % 400 ) return false;
return true;
}
//------------------------------------------------ void print_leap_excl_range(int start, int end)
{
//Test for "between". So do *not* include passed end year.
--end;
do
{
//Test for "between". So do *not* include passed start year.
++start;
if( is_leap(start) )
{
cout<<"Leap Year Found: "<<start<<endl;
}
} while(start < end);
} You could do it this way too.
QUESTIONS:
1 ) Is it faster or slower or doesn't it matter?
2) Is it better for some reason?
3) Why?
hint: read my signature below. Code:
void print_leap_excl_range(int start, int end)
{
//Test for "between". So do *not* include passed end year.
--end;
while(++start < end)
{
if( is_leap(start) )
{
cout<<"Leap Year Found: "<<start<<endl;
}
}
}