Monday, December 13, 2010

Test Question 1 Review 3 - The processing within the loop

As most people realised, the "meat in the sandwich" of the question was based on some class work, it's just that it had a loop around it to find many leap years.

The specification says:

The current calendar, called the Gregorian calendar, was introduced in 1582.
Every year divisible by four was declared to be a leap year, with the exception
of the years ending in 00 (that is, those divisible by 100) and not divisible by
400. For instance, the years 1600 and 2000 are leap years, but 1700, 1800, and
1900 are not.
So this should be enough to calculate
  • which years are leap years
  • which years are leap years that are also century years (eg: 1600, 2000)
  • which years are century years that are NOT leap years.

The test question asks for all three to be displayed, although the leap century years are part of the leap year display.

The test question also asks for two different sections to be displayed, one after the other: first the list of leap years, then the non-leap century years. Since the processing of each year will find each jumbled up next to the other it either means running the loop twice (first for leap years, then for non-leap century years) or displaying the leap years to the screen while noting and saving the non-leap century years for display after that (but more of this later in another blog post).

The tricky part of the processing is not the leap year part (which is easy: most are divisible by 4). It is whether it is a century year that is also a leap year (or a century year that isn't a leap year).

I decided (in the 10 minutes I allocated myself to answer the question: I actually didn't have it work out when I modified the class work question) to find the century years first:

For thisYear = startYear to finishYear
if (thisYear divided by 100 has no remainder) then
' Century year

else
' all non-century years

end if
Next

This gave me the difference between century years (both leap and non-leap) and all the other years (also both leap and non-leap).

Keeping with the century years (until it was worked out), the next step was to find out which century years were leap years and which weren't:


if (thisYear divided by 100 has no remainder) then
' Century year
if (thisYear divided by 400 has no remainder) then
' leap year Century year

else
' Century year but not Leap

end if
else

With the century years all taken care of, it was time to move on to all non-century years and work out which were leap years and which ones were not:


else
' all non-century years
if (divided by 4 has no remainder) then
' non-century leap years

end if
end if

so, the resulting logic for the three types of years I was looking for (leap years, century leap years and century non-leap years) ends up as

For thisYear = startYear to finishYear
if (thisYear divided by 100 has no remainder) then
' Century year
if (thisYear divided by 400 has no remainder) then
' leap year Century year

else
' Century but not Leap

end if
else
' all non-century years
if (divided by 4 has no remainder) then
' non-century leap years

end if
end if
Next

The next blog post will deal with the two different sections that need to be displayed