T1 - Programming Practical 5
Analysis
A program must be developed to take an input of a year from the Gregorian calendar. This must then print the day of the week that the 1st of January will be or has been in that year. The user should be able to enter a series of years until a sentinel value is entered, or to enter a range of years and print a response for each year.
Design
The year’s affected by user input must also be analysed for leap years and leap year exceptions in order to output the correct day of the week. The valid expected year range is from 2000 onwards.
In order to check if the year is a leap year it must be divisible by 4 and, if it is a century year, also 400. i.e. 1900 is not a leap year as it does not divide by 400. So it must be divisible by 400 and not 100 to be a leap year.
The day must be calculated by finding the difference between the year that the day is being found out for and 1900 then this casted as integer (rounded down) when divided by 4. Then this number being added to the difference and being modulo 7. Unless the year is a leap year then the value added must be decremented so that the application occurs after the leap day itself.
The output should be in the format:
YEAR DAY
Warning!
I feel that the way I implemented my solution to this problem was rather messy and that this could be avoided.
I intend to sort this in a future program along with the Alpha Sting for this practical:
I am hoping to find a way of increasing the width of my main column to show a kinder program layout.
Code
Code
Syntax highlighting thanks to pastie.org
A program must be developed to take an input of a year from the Gregorian calendar. This must then print the day of the week that the 1st of January will be or has been in that year. The user should be able to enter a series of years until a sentinel value is entered, or to enter a range of years and print a response for each year.
Design
The year’s affected by user input must also be analysed for leap years and leap year exceptions in order to output the correct day of the week. The valid expected year range is from 2000 onwards.
In order to check if the year is a leap year it must be divisible by 4 and, if it is a century year, also 400. i.e. 1900 is not a leap year as it does not divide by 400. So it must be divisible by 400 and not 100 to be a leap year.
The day must be calculated by finding the difference between the year that the day is being found out for and 1900 then this casted as integer (rounded down) when divided by 4. Then this number being added to the difference and being modulo 7. Unless the year is a leap year then the value added must be decremented so that the application occurs after the leap day itself.
The output should be in the format:
YEAR DAY
Warning!
I feel that the way I implemented my solution to this problem was rather messy and that this could be avoided.
I intend to sort this in a future program along with the Alpha Sting for this practical:
Extend your program so that it can calculate the day of the week for any date between 1st January 1900 and 1st January 2010.In the future I may alter this to use functions (as it would be better practice).
I am hoping to find a way of increasing the width of my main column to show a kinder program layout.
Code
Code
Syntax highlighting thanks to pastie.org