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.

- The program will read values until the user enters -1 to quit.
- The program
*must*provide an error message for zero or other negative numbers. - Do not presume that all the numbers will be integers.
- The program
*must*use the`push`

operator to add new values to the array. - The program
*must*provide reasonable prompts and error messages to the users. - The program’s output
*must*be properly labelled. - The program
*must*handle the case of an empty array properly by producing a reasonable error message. It must*not*attempt to do any calculations on the empty array. - The program’s output does not need to be lined up perfectly.
- You
*must*use the`foreach`

construct at least once in your program. - You
*must*display the deviations from the average as positive numbers with some wording (e.g.,`50.253% below`and`25.22% above`) rather than as a simple number (e.g.,`-50.253%`and`25.22%`)

To find the median of a group of ** n** numbers,
sort them into order.
If

`@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
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.

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 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;

Name the file in the form

and upload it.
So, if your name is Federico Blittner, your filename would
be *lastname_firstname*_arrays.pl`blittner_federico_arrays.pl`

. The filename *must*
be all lowercase, and may *not* contain blanks.