CPE 101 - Spring 2011 - Lab #3

Due:  End of lab Friday (4/15/11) 

The problem for this lab is figuring out what day of the year is it.  The lab has two parts to it, in order to understand that the same program can be written using the different techniques that you’ve learned so far in lecture:  if statements, switch statements. The question is nearly the same as that in chapter 4 problem 7 of your textbook.  Feel free to collaborate with a partner on this lab, but each person must turn in their own lab.

Write a program that determines the day number (1 to 366) in a year for a date that is provided as input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31, 1996, is day 366 since 1996 is a leap year. A year is a leap year if it is divisible by four, except that any year divisible by 100 is a leap year only if it divisible by 400. Your program should accept the month, day, and year as integers. The program will output the day number to the user.

Error Checking

Writing this program requires you to think in some detail about error checking. Inputs for months and days are very limited. The year is not necessarily as important because technically there can always be that year sometime before or down the line.  Look closely at the sample runs below, and take into account all the different cases that are required to error (range) check.  Pay particular attention to these considerations:

●	1. Month between 1 and 12
●	2. Day: depending on the month anywhere from 28 – 31 as the max
●	3. Day: account for leap year when error checking February

You don’t need to worry about incorrect character input, such as entering “xyz” for the number of a month.  You should view the behavior of your program if you enter incorrect input characters, but you don’t have to correct for that in this lab.  If you finish early, and fully understand what’s going on in this lab, you can think about how you might implement checking for incorrect inputs, such as entering alphabetic strings when the program expects numbers.

The Two Parts of the Lab 
 
Part A: Name this part of the lab: lab3A.c   Solve this part using only if-else statements in the program.

Part B: Name this part of the lab: lab3B.c   Now solve this part using a switch statement rather than the if-else statements.

NOTE: For Part B, some if-else statements will still be needed for the error checking, but use the switch statement to do the main calculations. 

Testing your lab 

Here are some sample runs of the program:

Sample Run 1:
Enter a month number:  2
Enter a day of that month:  28
Enter the year:  1999
That is day 59 of that year! 

Sample Run 2:
Enter a month number:  12
Enter a day of that month:  31
Enter the year:  1995
That is day 365 of that year! 

Sample Run 3:
Enter a month number:  12
Enter a day of that month:  31
Enter the year:  1996
That is day 366 of that year! 

Sample Run 4:
Enter a month number:  1
Enter a day of that month:  1
Enter the year:  2000
That is day 1 of that year! 

Sample Run 5:
Enter a month number:  19
Enter a day of that month:  23
Enter the year:  2000
Error there are only 12 months in any year 

Sample Run 6:
Enter a month number:  10
Enter a day of that month:  32
Enter the year:  1000
Error: no month has more than 31 days 

Sample Run 7:
Enter a month number:  4
Enter a day of that month:  31
Enter the year:  2009
Error: that month only has 30 days 

Sample Run 8:
Enter a month number:  2
Enter a day of that month:  30
Enter the year:  2000
Error: it’s a leap year, so that month has 29 days
 
Devise your own tests and record them in your comments: "TEST PLAN and RESULTS" 
 
The sample run above  gives you a lot of information about what your program must do, but it does not fully test the input-output relationship for the entire domain of possible inputs.   If you could test every possible input, you would need a way to check the given output to make sure it was correct (this is often called an "oracle" because it represents some method of obtaining the correct output given the input.... not always easy to do with complex programs.)	Devise a few of your own simple tests for some relatively suspicious cases that the sample runs have not covered.  Design these tests to find odd problems or to exhibit the boundary cases where problems often occur.  We will discuss such aspects of testing in class.  

EXTRA CREDIT -

If you've finished the Lab and it works well and you are able to review the text and go ahead of the lectures to see a "While" statement and its use to loop a program until user input indicates to exit, go ahead and add a big "While" loop to give the user the option to continue playing until they want to stop, then make the program exit.

Handin Instructions 

Hand in two files:  lab3A.c and lab3B.c, using the following command on vogon:

      handin instructor 101_lab3 lab3A.c lab3B.c 

where instructor is the UNIX ID of your 101 instructor this quarter:

Gene Fisher	gfisher	
John Seng	jseng	
Clark Turner	csturner	
David Janzen	djanzen