Monday, December 13, 2010

Test Question 1 Review 4 - Alternative displays of data

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

Now that the logic for looping through the range of years and finding century/non-century and leap/non-leap years was sorted out, it was time to work out how to display leap years before century non-leap years (since they could be all jumbled up together, depending on what year was being processed in the loop).

This means I could display leap years (normal ones and century ones) as the loop ran over each year (and worked out if it was a leap) but I needed to wait until the loop finished with all the leap years before then displaying the non-leap century years. I needed to delay those years.

The easiest thing I could think of was to create a list of these century non-leap years as a string, and, when I found one of those years, append it onto the list (concatentate that year at the end) with a simple comma "," to seperate them (eg: 1700, 1800, 1900)

add thisYear to NotCenturyLeap list
which can be written in VisualBasic.NET as

NotCenturyLeap &= thisYear & ", "
where "NotCenturyLeap" is a simple string
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
displayLine(thisYear)
else
NotCenturyLeap.StringAdd (thisYear & ", ")

end if
else
' all non-century years
if (divided by 100 has no remainder) then
' display only non-century leap years
displayLine(thisYear)
end if
end if
Next
I didn't bother in this case, but to do the job properly, I would have removed the last comma from that list before display (using something like VisualBasic's "left()" function or similar)

NOTE: there are many ways to do this: appending (adding to) lists, arrays, etc or even using the StringBuilder class (look it up if you want to learn more), but I wanted to keep it really simple.

The only thing left to do now was to display the alternative message if there were no non-leap century years within the rage of years. The easiest thing I could think of was a simple flag (boolean value) that was set to "FALSE" and if a non-leap century year was found, set it to true.


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

else
set found_a_nonCentuaryLeapYear to true
end if
else
...
end if
Next


The flag could then be used to show those years or the alternative message.

if found_a_nonCentuaryLeapYear is true then
displayLine("The following century years that fall within the date range and are NOT leap years are:")
displayLine(NotCenturyLeap)
else
displayLine("there are no century years that are not leap years within the range you have specified")
end if
so the final pseudocode ended up as:
prompt "please enter a start year for the leap year range calculator"
(get startYear)
prompt "please enter an end year for the leap year range calculator"
(get finishYear)

' display the first message to the screen
displayLine("The following years are leap years:")

' now loop through all the years looking for
' - century years that are leap years (and display them)
' - century years that are NOT leap years (and add them to a string to display later)
' - non-century years that are leap years (and display them)

set found_a_nonCentuaryLeapYear to false
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
displayLine(thisYear)
else
NotCenturyLeap.StringAdd (thisYear & ", ")
set found_a_nonCentuaryLeapYear to true
end if
else
' all non-century years
if (divided by 100 has no remainder) then
' display only non-century leap years
displayLine(thisYear)
end if
end if
Next

if found_a_nonCentuaryLeapYear is true then
displayLine("The following century years that fall within the date range and are NOT leap years are:")
displayLine(NotCenturyLeap)
else
displayLine("there are no century years that are not leap years within the range you have specified")
end if
The next step would be to turn that into VisualBasic.NET code then into C# code (see next post).