Read everything before doing anything! You are strongly encouraged to write a flowchart or pseudocode before you start writing this program, especially part 2!
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.
lastname_firstname_stats.pl
push
operator to
add new values to the array.print
statements in it,
and it must return both the median and average
as its result. (Added 8 Mar 07)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
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.)
lastname_firstname_day.pl
is_leap_year
which takes a year number as its parameter. The subroutine
will return 1
if the year is a leap year, and
0
otherwise.is_valid_date
which takes the month, day, and year as named paramters
and returns 1
if the combination of month, day,
and year are valid, and returns 0
otherwise. This
subroutine will print error messages, not the
main program.is_valid_date
subroutine may print as many error
messages as possible, or it may exit as soon as it finds any error
in the data.See the output from running the program.
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))
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.
Upload both programs. If you wish, you may put the files into a .zip file or .tgz file.