Programming Assignment
Arrays

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

This program will read a group of positive numbers, and then calculate the average and median values. The program will then print all the scores in the order that they were input, showing each number and what percentage it is above or below the average. Note: Finding the average doesn’t require an array. Finding the median and the percentages does require an array.

Finding the Median

To find the median of a group of n numbers, sort them into order. If n is odd, the median is the middle entry. In an array named @data with elements starting at index zero, this will be element $data[(n-1)/2]. (Of course, you will have to put a scalar variable in place of n)

If n is even, the median is the average of the numbers “surrounding” the middle. For an array named @data with elements starting at index zero, this is ($data[(n/2)]+$data[(n/2)-1])/2.

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
Value   % above/below average
4       50% below
6       25% below
14      75% above

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
Value   % above/below average
20      0% exactly average
10      50% below average
30      50% above average

In this example, the percentages all happened to come out to be integers. For an arbitrary set of numbers, this won’t be the case. Do not truncate your results to integers.

Sorting an Array Numerically

When you sort an array, the default is to sort in alphabetical (ASCII) order. This isn’t what you want for an array containing numbers. Otherwise, the number "47" (as a string) will sort before the number "5" (as a string).

In order to sort an array numerically, you must use code like this, where @arr is the name of your original array and @new is the name of your sorted array:

@new = sort {$a <=> $b} (@arr);

You must use the variable names $a and $b for this to work! Don’t worry if you are already using those variables for something else. This is truly magic, and their values won’t be affected by the sort.

If you are stuck

If you have no idea how to approach the problem, here is the pseudocode. You will have to translate a lot of the English into Perl. For example, if pseudocode says "set age in days to zero", you can’t write this:

    $age in days = 0; 

Instead, you have to make it correct in Perl:

    $age_in_days = 0;

When You Finish

Name the file in the form lastname_firstname_arrays.pl and upload it. So, if your name is Federico Blittner, your filename would be blittner_federico_arrays.pl. The filename must be all lowercase, and may not contain blanks.