Programming Assignment
Subroutines

Read everything before doing anything! You are strongly encouraged to write a flowchart or pseudocode before you start writing this program, especially part 2!

Part 1

This program will read a group of positive numbers into an array, and will then call a subroutine that calculates and returns the average and median values to the main program. The main program will then print those values.

See the arrays assignment for information on how to calculate the median of a set of numbers.

Here is sample output from three separate runs of the program.

Enter positive number or -1 to quit: 4
Enter next positive number or -1 to quit: 0
0 is not a positive number.
Enter next positive number or -1 to quit: book
book is not a positive number.
Enter next positive number or -1 to quit: -2
-2 is not a positive number.
Enter next positive number or -1 to quit: 6
Enter next positive number or -1 to quit: 14
Enter next positive number or -1 to quit: -1

The average is 8
The median value is 6

Enter a positive number, or -1 to quit: -1
No data entered. No calculations performed.

Enter a positive number, or -1 to quit: 20
Enter next positive number or -1 to quit: 10
Enter next positive number or -1 to quit: 30
Enter next positive number or -1 to quit: -1
The average is 20
The median value is 20

Part 2

Write a program that prompts for a month (by number), day, and year. The program will then print out which day of the year you have specified. (e.g., Jan 1, 2005 is day 1; Feb. 1, 2005 is day 32, etc.)

See the output from running the program.

Leap Year Calculation

A year is a leap year if it’s divisible by four, except that any year divisible by 100 is a leap year only if it’s also divisible by 400. The following expression is true if $year is a leap year and is false otherwise.

(($year % 4 == 0 && $year % 100 !=0) || ($year % 400 == 0))

Hints:

Create an array named @months to make it easy to convert a month number to a month name.

@months = qw( no_month_zero Jan Feb Mar Apr May Jun
   Jul Aug Sep Oct Nov Dec );

Create an array that tells how many days there are per month:

@days_per_month = ( 0, 31, 28, 31, ... );

This will not only help you figure out which day of the year something is, but also if a date is valid. To handle February, I suggest that you not modify the array on the fly. That can be problematic. Instead, for example, when calculating day of year, do something like this pseudocode:

for ($i = 0 up to month in question)
{
   $total_days += $days_per_month[$i];
}
add in the days for the month not completed to $total_days
if (month is > february and it's a leap year)
{
   add 1 to $total_days
}

Do something similar (use a temporary variable) to hold the number of days in the month in question when validating a date.

When You Finish

Upload both programs. If you wish, you may put the files into a .zip file or .tgz file.