Tutorial Week 7 - Validation


Home


Question 1

For the following problem, work through steps 1 (Understanding) through 5 (Desk Checking) of the Steps in Program Development discussed in lectures.

Write a program to display the number of days in a month. The user is to input the month number.

Example Runs

Run
Month Number (1 to 12, 0 to exit) ? 5
31 days

Month Number (1 to 12, 0 to exit) ? 2
28 days

Month Number (1 to 12, 0 to exit) ? 11
30 days

Month Number (1 to 12, 0 to exit) ? 0

Question 2

Modify your algorithm to validate the month number input. Use an appropriate loop to validate the month number before determining the number of days in the month. Assume there are 28 days in February.

Example Runs

Run
Month Number (1 to 12, 0 to exit) ? 5
31 days

Month Number (1 to 12, 0 to exit) ? 2
28 days

Month Number (1 to 12, 0 to exit) ? 11
30 days

Month Number (1 to 12, 0 to exit) ? 13
Error: month number must be between 1 and 12, or 0 to Exit

Month Number (1 to 12, 0 to exit) ? -5 
Error: month number must be between 1 and 12, or 0 to Exit
Month Number (1 to 12, 0 to exit) ? 0

Question 3

Perform a Desk Check on your new algorithm. What data should be used?

Question 4

The above problem is a simplification of the real problem. What other information needs to be input by the user to be able to determine the correct number of days in a month? Under what circumstances is it needed?

Question 5

What would the condition be for validating the month if -1 was used to exit the loop instead of 0?

Question 6

Catch up on any problems from previous tutorials that you haven't completed. Take this opportunity to discuss any queries or problems from previous tutorials with your tutor.


Written by Tim Whitfort.