# Chapter 9: Arrays

In this chapter, we'll be examining arrays. An **array** is a collection of objects or variables of the same type. Each object or variable in an array is an **element** of that array. Each element of the array is assigned an **index**, or a numerical value starting from zero. Elements of arrays are accessed with the **array indexing operator**, two square brackets [].

**Note**: The plural form of index is indices or indexes. Both are grammatically correct. You may see these words used interchangeably throughout various math, science, and programming fields.

## Creating Arrays

### Declaring Arrays

Arrays, like any variable, must be declared before you can use them. In Java, you declare an array by placing square brackets next to the variable type.

Declaring Array`int[] myArray;`

In plain English, this line is saying "create a new reference variable for an array of ints and call it myArray."

### Instantiating Arrays

When we initialize variables, we are allocating memory for them. We must do the same for arrays. Arrays, however, are considered to be collections. Collections are objects, not variables. Recall that when we allocate memory for an object, we are **instantiating** it. When we allocate memory for a variable, we're initializing it. You may see the terms instantiate and initialize used interchangeably in your studies.

When you instantiate an array, you must tell the JVM how much memory to allocate for that array. Does this array need 5 spaces in memory? Ten? Twenty? To instantiate the array after declaring it, we need to say it's a new array with a certain size. Once the size or length of the array is set, it cannot change. That would look like this:

Instantiate Array`myArray = new int[5];`

The above line is saying "create instance myArray as a new array of integers with 5 elements. Set aside 5 locations in memory for ints."

It is important that you do not create arrays with more indices than you need to use. If you do this, you're wasting space in the computer's memory.

We can now set and access values from any index in the array. Indexing starts from zero, so if the array has 5 elements, the indices go from 0 to 4, not 1 to 5.

In the sample code below, we set index 0 of the array to 43 and print it to the screen.

Assign, Instantiate, Use Array```
int[] myArray;
myArray = new int[5];
myArray[0] = 43;
System.out.println(myArray[0]);
```

43

We can set any element in the array to any value permitted by the int type. This will become especially useful later, when we combine arrays with loops. We can loop through the indicies and do all sorts of fun things.

Setting Values For More Elements```
int[] myArray;
myArray = new int[5];
myArray[0] = 43;
myArray[1] = 24;
myArray[2] = 69;
System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
```

43 24 69

We can also declare and initialize our arrays in one line.

Declare and Instantiate Array`int[] myArray = new int[5];`

### IndexOutOfBounds Exception

If you run out of space in your array, or attempt to access an index beyond the length you originally set, you will get an index out of bounds exception. You will likely become well acquainted with this error. When you see it, you need to review your logic and array size. Usually you'll only be off by one number in an index, or a loop will go one step too far.

```
int[] myArray;
myArray = new int[5];
myArray[5] = 43;
```

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5

### Array Creation Shortcut

If you already know the values you need in your array, you can declare, instantiate, and assign values to it all in one line.

Here's an example that adds five values to an int array.

`int[] myArray = { 5, 10, 15, 20, 25};`

You declare the array as you normally would, then add a comma separated list of values in curly braces. There is no need to say how many elements are in the array, it will automatically figure that part out.

## Array Length

The number of elements an array can hold is the **length** of the array. You can find the length of an array by accessing the .length variable.

```
int myArray[] = {5, 10, 15, 20, 25};
System.out.printf("The length of the array is: %d", myArray.length);
```

The length of the array is: 5

Finding the length of an array is helpful if you're creating an algorithm that might deal with arrays of unknown or different lengths.

## Calculating The Average - Example

### The Problem

Let's take a look at a simple example. Say you want to create a program that calculates the average of five different values entered in by the user. The program would look like this when run:

Enter 5 numbers to find the average. 5 7 3 6 5 Calcululating Average... The average of the terms you entered is: 5.2

If you wanted to make a program like this using only variables, it wouldn't be too hard. You could use an input scanner and set five different variables equal to the numbers the user enters, then add them up and find the average. This would involve a bit of repetitive code. One of our goals when designing things is to make methods as generic or reusable as possible. If we code a program like this, it will only be able to ever find the average of 5 values.

There is a way to find the mean of multiple terms using loops without arrays, but we will ignore that option for demonstration.

Example Program (No Arrays)```
Scanner input = new Scanner(System.in);
System.out.println("Enter 5 numbers to find the average.");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
input.close();
System.out.println("Calculating average...");
double average = (a+b+c+d+e)/5.0;
System.out.println("The average of the terms you entered is: " + average);
```

If we modified it to support 10 values, we would have to create 5 additional variables and manually code each term into calculating the average. That's not too hard. But what if we wanted to modify it to support 20, 30, or even 100 different entries? It would quickly become a tedious nightmare. This is where arrays come in to save the day.

Arrays allow us to use a single variable identifier to store multiple elements. Soon, we'll learn how to make a program that calculates the average of any number of terms using an array.

### The Solution (With Arrays)

Now, we're going to use an array and loops to calculate the average from a given set of terms.

### Logic

- The program will ask how many numbers we are finding the average from. This will be the length of our array.
- We will loop through the indices of the array, assigning each index a value the user enters.
- Loop through all the indices to find the sum of all the values.
- Calculate the average.

### Code

Start by importing the Scanner class and creating a new scanner. Then, ask the user how many numbers we're finding the average of.

```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("How many numbers are you entering?");
Scanner input = new Scanner(System.in);
int terms = input.nextInt();
}
}
```

Next, we need to create an array to hold the values. I have decided to use doubles as my variable type. Declare and instantiate the array with the number the user entered. Then, ask the user to enter the numbers.

```
double[] values = new double[terms];
System.out.println("Enter " + terms + " numbers to find the average.");
```

Create a loop that iterates through each index of the array, starting from zero. In this case, we're going from zero to the number of elements in the array (our terms variable).

In the body of the loop, record the number the user enters for that index of the array.

I have decided to use a for loop for this.

```
for(int i = 0; i < terms; i++){
values[i] = input.nextDouble();
}
```

Once this loop is finished, we have assigned values to each index of the array. We can close the scanner.

`input.close();`

Now, we need to find the sum of all the values that have been entered. Create a variable called sum that will store the total sum of all the values entered. Iterate through the loop again, and add each value to the sum.

```
//find the average
System.out.println("Calculating average...");
double sum = 0;
for(int i = 0; i < terms; i++){
sum = sum + values[i];
}
```

We now have the sum and the number of terms. We can find the average.

```
double average = sum / terms;
System.out.println("The average of the terms you entered is: " + average);
```

Click here to view the full example, if needed.

```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Ask how many terms we're using
System.out.println("How many numbers are you entering?");
Scanner input = new Scanner(System.in);
int terms = input.nextInt();
//create the array of length the user entered
double[] values = new double[terms];
System.out.println("Enter " + terms + " numbers to find the average.");
//record values into the array
for (int i = 0; i < terms; i++) {
values[i] = input.nextDouble();
}
//close scanner
input.close();
//find the average
System.out.println("Calculating average...");
double sum = 0;
for (int i = 0; i < terms; i++) {
sum = sum + values[i];
}
double average = sum / terms;
System.out.println("The average of the terms you entered is: " + average);
}
}
```

Now, when you run the program, you should be able to figure out the average of any number of items.

How many numbers are you entering? 3 Enter 3 numbers to find the average. 5 9 2 Calculating average... The average of the terms you entered is: 5.333333333333333

### Extended Calculations

Now, let's use the same program above, but calculate some additional information from the values. Let's find the maximum value entered.

To find the maximum value, we will set a variable to zero. Assume the user only enters positive values. We can loop through each index of the array and check the current value against the recorded maximum. If the value at this index is greater than what's currently recorded, it becomes the new maximum.

```
//calculate the max
double max = 0;
//iterate through all values
for (int i = 0; i < terms; i++){
//if this value is greater than the recorded max, it's the new max
if (values[i]>max){
max=values[i];
}
}
System.out.println("The largest number you entered (maximum) is:" + max);
```

How many numbers are you entering? 3 Enter 3 numbers to find the average. 43 53 1 Calculating average... The average of the terms you entered is: 32.333333333333336 The largest number you entered (maximum) is:53.0

The program now finds the average and the maximum value entered.

**Try Yourself: **Can you create another loop that finds the minimum value?

## Basic Algorithm - Sorting

Now that we have a basic foundation for using loops and arrays together, consider the following question. How could we make an algorithm that sorts the elements of an array of integers in order from smallest number to greatest?

First, let's consider the logic. If you were given a list of five numbers: 5, 9, 7, 2, and 1, you could easily sort them at a glance, likely with little thought required. The order from smallest to greatest is 1, 2, 5, 7, then 9. How can we get a computer to figure this out? We need to break the problem down into instructions a computer can understand. There are multiple ways we could achieve this with a loop.

### Sorting An Array From Smallest to Greatest

One way we could rearrange this list of numbers is to iterate through each number. If this number is larger than the next number in the sequence, we will swap the number with the next number. Each time the loop executes, the number is moved to the next index if it's greater than that number.

- Given Values: 5, 9, 7, 2, 1
- Check if the first value is greater than the second.
- 5 is not greater than 9, leave it as is.
- Check if the second value is greater than the third.
- 9 is greater than 7. Swap 9 and 7.
- Values: 5, 7, 9, 2, 1
- 9 has moved one slot to the right.
- Check if the third value is greater than the fourth.
- 9 is greater than 2, so swap 9 and 2.
- Values: 5, 7, 2, 9, 1
- Check if the fourth value is greater than the fifth.
- 9 is greater than 1, so swap 9 and 1.
- Values: 5, 7, 2, 1, 9

Now, we have 9 in the proper position. It has moved to the end of the array. We repeat this process three more times. Each time, the next highest number is moved as far to the right as possible.

Given: 5, 9, 7, 2, 1 Outer Loop 1 Finishes: 5, 7, 2, 1,9Outer Loop 2 Finishes: 5, 2, 1,7, 9 Outer Loop 3 Finishes: 2, 1,5, 7, 9 Outer Loop 4 Finishes: 1,2, 5, 7, 9

Here is a visual representation of the steps involved, if that helps with understanding.

Examine the code for the completed program below. It takes an array of the 5 values above and sorts them from smallest to greatest, then prints out the values.

```
public class Main {
public static void main(String[] args) {
int sortMe[] = new int[5];
sortMe[0] = 5;
sortMe[1] = 9;
sortMe[2] = 7;
sortMe[3] = 2;
sortMe[4] = 1;
//outer loop four times
for(int a = 0; a < 4; a++){
//inner loop moves largest to the right
for(int i = 0; i < 4; i++){
//if this value is greater than the next value
if (sortMe[i] > sortMe[i+1]){
//create a temporary value to hold the current value, so we don't lose it
int tmp = sortMe[i];
//swap the values
sortMe[i] = sortMe[i+1];
sortMe[i+1] = tmp;
}
}
}
//print out the sorted array
for(int i = 0; i < 5; i++){
System.out.println(sortMe[i]);
}
}
}
```

The inner loop checks each value in the array and moves the largest number found to the end. The outer loop executes 3 more times to move the largest number to the right each time. Both loops run 4 times and not 5, because there are 4 swaps that can occur the way we have this set up. In fact, if we ran the inner loop 5 times, on the fifth time, we'd be checking the 4th index value against the 5th index value. There is no 5th index value so we'd get an IndexOutOfBounds Exception.

Notice that when I swapped the values I created a temporary variable to store one of the values. This was necessary to swap the values.

Consider what would've happened if I just swapped the values using the indexes.

```
//wrong way to swap
sortMe[i]=sortMe[i+1];
sortMe[i+1]=sortMe[i];
```

When the program runs, it does something like this. Let's say we're at the beginning where sortMe[1] = 9 and sortMe[2] = 7.

```
sortMe[1]=9;
sortMe[2] = 7;
...
//wrong way to swap
sortMe[1]=sortMe[2]; //sortMe[1] now equals 7
sortMe[2]=sortMe[1]; //sortMe[1] was turned into 7 in the last step, so sortMe[2] now equals 7.
//the value 9 has disappeared
```

When I first set sortMe[i] to sortMe[i+1], I am overwriting the value of sortMe[i]. If I do this, then when I set sortMe[i+1] to sortMe[i] after setting sortMe[i] to sortMe[i+1], it would be setting both indexes to the same value. The temporary variable stores one of the values, so I can use it again immediately. We want to SWAP the values, not set them equal to the same number.

When we use the temporary value, this is what happens:

```
sortMe[1]=9;
sortMe[2]=7;
...
//right way to swap
int tmp = sortMe[1]; //tmp is set to 9
sortMe[1] = sortMe[2]; //sortMe[1] is set to 7
sortMe[2] = tmp; //sortMe[2] is set to 9
```

### Code Efficiency Note

The way we created the inner loop in the above section to sort values works, but is it as efficient as possible?

Consider the fact that the inner loop is set to run a set number of times, every time. The first time it runs through, it moves the largest number to the right. The outer loop then moves onto the second execution of the inner loop, and the inner loop is run again, moving the second largest number to the second space from the end.

When the inner loop runs its second time, it still checks if the second last value is higher than the last value. We already know this will be false, since we moved the highest number to the end the first time the inner loop ran. Therefore, there is no need to check if this last swap needs to be performed.

If we're dealing with an array of 5 elements, then the first time the outer loop runs, we need to check all 4 swaps with the inner loop. The second time, we only need to check the first 3 swaps. The third time, the first two swaps, and so on.

We can prevent these extra unnecessary checks from occurring by simply adding -a to the condition for the inner loop. So the first time it runs, it runs all 4 times. The second time it runs, it runs 4-1 times, the third time, 4-2 times, and so on.

More Efficient Inner Loop Condition`for(int i = 0; i < 4-a; i++){`

Add -a to the inner loop condition, and test the code again. It should still sort just like before, but now the code is more efficient. It's not wasting as much CPU resources.

The key takeaway here is that just because it works, doesn't mean it's the most efficient way of working. Carefully consider the logic behind the algorithms you write. On a small scale, it won't make a huge difference. But if we were sorting a thousand numbers, or a million numbers, these inefficiencies could result in significant amounts of wasted CPU time.

## Passing Arrays To Methods

Arrays, like variables, can be passed to methods as arguments. This is important for making reusable code. The previous sorting example is hard coded to only sort an array with five elements. Let's modify the algorithm that sorts numbers from smallest to greatest so it can work with any array. We'll do this by making a sortLeastGreatest method, which sorts any array of ints from smallest to largest.

The method definition will look like this:

```
public static void sortLeastGreatest(int[] sortMe){
...
}
```

### Overflow Exceptions

If you get an overflow exception when looping, check the end condition! In many cases, you may only need to decrease the conditional variable by one index.

The method is public, it can be accessed anywhere in the program. The static keyword is required so we can call it from the main method. The method is of type void. This method does not return anything, it only modifies an existing array. The parameter is an int array called sortMe, which is the array that will be sorted.

Like all objects in Java, arrays are passed by value. In the case of arrays, the value being passed is a reference variable corresponding to the original array. So when we modify the reference variable in the method, it actually updates the values of the original array. No new array is being created in the method.

Now we can largely use the same code as before, we just need to modify the loops to work with an array of any length, since we don't know the length of the arrays we're passing to this method. The condition of the loops will make them stop at the array length minus one.

If you do this correctly, it will look like this:

`/**`

* You can put this after the the main method within the same class
* @param sortMe the array to sort
*/
public static void sortLeastGreatest(int[] sortMe){
//outer loop
for(int a = 0; a < sortMe.length; a++){
//inner loop moves largest to the right
for(int i = 0; i < sortMe.length-1-a; i++){
//if this value is greater than the next value
if (sortMe[i] > sortMe[i+1]){
//create a temporary value to hold the current value, so we don't lose it
int tmp = sortMe[i];
//swap the values
sortMe[i] = sortMe[i+1];
sortMe[i+1] = tmp;
}
}
}
}

Once it's done sorting, it must return the sorted array sortMe.

Now, let's test out the method we created. I make a test array, fill it with values, and use the method to sort it. I print this sorted array to the console.

```
//replace your main method with this
public static void main(String[] args) {
int[] arr = {5, 10, 9, 7, 2, 1, 4};
sortLeastGreatest(arr);
//print sorted array
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + ", ");
}
}
```

1, 2, 4, 5, 7, 9, 10,

It clearly works for this array. But never test something just once, create some other test arrays with random values and make sure they're all properly sorted.

```
int[] testArrayB = {5, 6, 23, 43, 24, 54, 63, 632, 132, 4234, 54, 0, 92, 1, 4, 5, 4, 2, 55, 234};
sortLeastGreatest(testArrayB);
//print sorted array
for(int i = 0; i < testArrayB.length; i++){
System.out.print(testArrayB[i] + ", ");
}
```

0, 1, 2, 4, 4, 5, 5, 6, 23, 24, 43, 54, 54, 55, 63, 92, 132, 234, 632, 4234,

Now I know it works with longer arrays, and it can properly sort a variety of numbers. The logic behind the algorithm works and no unexpected behavior occurred, even with duplicate values.

### Returning New Arrays/Copying

Suppose instead of sorting an existing array from least to greatest, we want to take the values from an original array, and return a new sorted array. That way, the original array doesn't change. To do this, we need to create a copy of the original array, modify sort copy, and return the reference to the sorted copy.

The example code below copies the values of the original array into a new array, then sorts and returns it.

```
public static int[] sortLeastGreatest(int[] sortMe){
//create a new array to store the copy
int copy[] = new int[sortMe.length];
//copy sortMe into a new array
for(int i = 0; i < sortMe.length; i++){
copy[i] = sortMe[i];
}
//sort array
for(int a = 0; a < copy.length; a++){
for(int i = 0; i < copy.length-1-a; i++){
if (copy[i] > copy[i+1]){
int tmp = copy[i];
copy[i] = copy[i+1];
copy[i+1] = tmp;
}
}
}
return copy;
}
```

Now, when we want to sort arrays, calling the sortLeastGreatest method will return a new array of ints.

```
public static void main(String[] args) {
int[] unsorted = {5, 9, 3, 5, 1};
int[] sorted = sortLeastGreatest(unsorted);
//print out the arrays
System.out.println("Original array: ");
for(int i = 0; i < unsorted.length; i++){
System.out.print(unsorted[i] + ", ");
}
System.out.println("\nSorted array: ");
for(int i = 0; i < sorted.length; i++){
System.out.print(sorted[i] + ", ");
}
}
```

Original array: 5, 9, 3, 5, 1, Sorted array: 1, 3, 5, 5, 9,

Now if we need the original array for some reason later, we still have it to work with.

## For-each Loops

For-each loops are a useful shortcut to quickly examine the contents of an array. They allow you to loop through every value in an array using a variable that matches the data type of the array. The variable declared in the for-each loop becomes each value of the array in the loop body. There's no need to use indexes with for-each loops. They just go through the array from index zero to the end one step at a time. Each time the loop body is executed the variable declared takes the place of whatever value is at that index.

The structure of a for each loop is as follows. Notice that we use a colon instead of a semicolon.

```
for (type variable : array){
//do something with variable
}
```

Functionally, the above code does almost the same thing as:

```
for (int i = 0; i < array.length; i++){
//do something with array[i]
}
```

For-each loops use the for keyword just like regular for loops.

Here is an example of a for-each loop in action. This for-each loop simply traverses an array of ints and prints each one out.

```
int[] array = {5, 10, 15, 20, 25};
for(int val : array){
System.out.print(val + ", ");
}
```

This is like saying "for each int in my array, print out the value." The variable I named *value* has taken the place of each value in the array.

5, 10, 15, 20, 25,

Since for-each loops only access the values of an array without telling us the index we're at on each iteration, they don't provide a direct way to set values. You will need to use a normal for loop with indices if you want to modify the values of an array. The variable in the for-each loop containing each value in the array is not tied to the array itself. If you modify this variable, the array itself will remain the same.

Additionally, because for-each loops only start from index zero, you can only use them if you need to go through a loop from start to finish. If you want to skip indices, or go from the end of the array to the beginning, you'll need to use a different type of loop.

### Example: Find The Lowest Number

Let's find the lowest number in an array of doubles using a for-each loop. To do this, we'll declare and initialize a variable to keep track of the lowest number (called lowest). We'll set this number equal to the first element of the array to start. Then, in the for-each loop, we'll check each value. If the value found is lower than the last lowest found number, it becomes the new lowest number.

Once the for-each loop is done executing, the variable will contain the lowest number.

```
double[] randomNumbers = {54.9, 53, 213.5, 40.1, 8.89, 65, 31, 65, 76};
double lowest = randomNumbers[0];
for(double val : randomNumbers){
//if the number at this value is lower than what was set before, it's the new lowest number.
if (val < lowest){
lowest = val;
}
}
System.out.println("The lowest number is: " + lowest);
```

The lowest number is: 8.89

Here's a summary of what happens on each iteration of the for-each loop if you need help understanding what happened:

- lowest = 54.9, val = 54.9, 54.9 is not less than 54.9, continue
- lowest = 54.9, val = 53, 53 is less than 54.9, 53 becomes the new lowest number, lowest is set to 53
- lowest = 53, val=213.5, 213.5 is not less than 53, continue
- lowest = 53, val=40.1, 40.1 is less than 53, 40.1 becomes the new lowest number, lowest is set to 40.1
- lowest = 40.1, val=8.89, 8.89 is less than 40.1, 8.9 becomes the new lowest number, lowest is set to 8.9
- lowest = 8.89, val=65, 65 is not less than 8.89, continue
- lowest = 8.89, val=31, 31 is not less than 8.89, continue
- lowest = 8.89, val=65, 65 is not less than 8.89, continue
- lowest = 8.89, val=76, 76 is not less than 8.89, we have reached the end of the loop and found the lowest number

## Array Expansion

The size of an array is final and cannot be changed. That said, it is possible to "expand" an array by copying it into a new, larger array, and referencing the new, larger array.

Suppose you have an array with five elements. The original array might look like this:

```
//an int array with 5 elements
int[] arr = {10, 20, 30, 40, 50};
```

Now lets expand the array to fit ten elements. The example below shows how to do this.

```
//an int array with 5 elements
int[] myArray = {10, 20, 30, 40, 50};
//create a reference to the original array
int[] originalArray = myArray;
//have myArray reference a new array with 10 elements
myArray = new int[10];
//copy the first 5 values into the new array
for (int i = 0; i < originalArray.length; i++){
myArray[i] = originalArray[i];
}
//get rid of (dereference) the original array
originalArray = null;
```

After enlarging the array, we can access elements 5-9 of the new, larger array without throwing an IndexOutOfBounds exception.

```
//now we can access elements 5-9 of the new array
myArray[5] = 60;
myArray[6] = 70;
myArray[7] = 80;
myArray[8] = 90;
myArray[9] = 100;
for(int i: myArray){
System.out.print(i + ", ");
}
```

10, 20, 30, 40, 50, 60, 70, 80, 90, 100,

It worked! We essentially turned an array with 5 elements into an array with 10 elements. Remember, technically we replaced the original array with a new, larger array and got rid of the original. The original array never changed in size. The variable is referencing a completely new array with the elements of the original.

Computationally, this is an expensive task. Imagine if we had an array with a thousand elements, and needed to expand it to support 1001 elements. If we just expand the array by 1 element each time we need to add a new value, it's going to have to go through the entire original array and copy it into a new array every single time we add one new item. That becomes very inefficient.

If there's a possibility you'll need to add more items to an array after creating it, you should expand the length of the array by a multiple of the original. That way, your program will not need to perform as many expansion operations. Instead of just expanding an array's length by one element, expand it by double or triple the original length, depending on how often you think you'll need to expand it. This will use more memory, but a lot less CPU time. It's OK to reserve a little extra memory if you think you'll need it later.

In the next chapter, we will learn how to use another collection object called ArrayList which expands array-like collections for us.

## Assignment: Mean, Median, Mode calculator

### Instructions

Create a program that asks the user how many values they want to enter, then have them enter that amount of values. Place all these values into an array of doubles. Then, print out the numbers sorted from least to greatest. Calculate the mean, median, and mode of all these numbers and print the results.

The program must have four methods, in addition to the main method.

- Methods:
- sortLeastGreatest(double[] arr) - the method to sort the array
- double findMean(double[] arr) - a method that returns the mean of an array of doubles
- double findMode(double[] arr) - a method that returns the mode of an array of doubles - return -1 if no mode was found
- double findMedian(double[] arr) - a method that returns the median of an array of doubles

You may assume the array has already been sorted from least to greatest before using the findMean, findMedian, and findMode methods (no need to call sortLeastGreatest from these methods).

### Math Refresher

The **mean** is the average of a set of numbers. It is found by taking the sum of all the numbers and dividing that number by the number of terms.

\(\text{mean}=\frac{\text{sum of numbers}}{\text{amount of numbers}}\)

If you take a set of numbers that are in order, and find the number in the middle, that number is the **median.**

For example, if given this set of data: 1, 5, 9, 10, 12

The median would be 9, since it's the number in the middle.

If there's an even number of items, the median is the mean of the middle two numbers.

For example, if given this set of data: 1, 5, 9, 10, 12, 16

The median would be 9.5, because \(\frac{9+10}{2}=9.5\)

The **mode** is the number that occurs most often in the data set. Sets of data can contain multiple modes (if there's five twos and five fours, for example).

For the sake of this assignment, assume that the data set only contains one possible mode.

- Data: 1, 4, 5, 9, 9, 10
- Mode: 9 (occurs twice)

### Hints & Methodology

Here is a template to get started, if needed. It contains only the method definitions you will need. Everything else, you should fill in yourself.

```
public class Main {
public static void main(String[] args) {
double[] values; //the array of values the user enters
int totalValues; //the total number of values the user is entering in the array
//TODO Collect the values the user enters with a scanner
//TODO Sort the array by calling sortLeastGreatest
//TODO Print the sorted values out as a comma separated list (1, 2, 3...)
//TODO Find the mean, median, and mode using the methods you made
//TODO Print the results
}
/**
* sorts an array from least to greatest number
* @param arr - the array being sorted
*/
public static void sortLeastGreatest(double[] arr){
}
public static double getMean(double[] arr){
//TODO Write a method that finds the mean of the data
}
public static double getMedian(double[] arr){
//TODO Write a method that finds the median of the data
}
public static double getMode(double[] arr){
//TODO Write a method that finds the mode of the data - return -1 if none found
}
}
```

- To get started, create a new project with a class file and a main method. Import and instantiate the scanner, then ask the user how many values they're entering.
- Create an array of doubles
`double[] values;`

. Set the length of the array to the number the user specified.`values = new double[totalValues];`

- Use a loop and fill the array with the values the user enters.
- Sort the array from least to greatest using the sortLeastGreatest method defined in this chapter. Modify it to use doubles instead of ints.
- Use a loop to print out the sorted array as a comma separated list of values
`System.out.print(values[i] + ", ");`

. - Create methods to find the mean, median and mode. Find the results and print them to the screen.

It may help to visualize how each method needs to work by writing a sample set of data on a piece of paper, then figure out each step you need to take to find each value.

#### The Mean Method

We already learned how to find the mean with an array of ints in this chapter. Use the same logic for this method, but with doubles. Then return the mean as a double.

#### The Median Method

After sorting the array, you can find the median. We will assume that the sort method has been executed before the getMedian method is executed, so there is no need to re-sort the array in the getMedian method.

You will have to first determine if you're working with an even set of numbers, or an odd set of numbers.

To find if you have an even or odd set, you can use the modulus operator. Remember, this operator returns the remainder after dividing two numbers. If \(\frac{\text{length of array}}{2}\) has a remainder of 1, then the length must be odd. If the remainder is zero, the length must be even.

`if(arr.length % 2 == 1)`

can be used to determine if there are an odd number of items in the array.

If dealing with an odd number of items, the median is the number in the middle. Divide the total length of the array by 2 to find the index of the middle item.

If dealing with an even number of items, the median is the average of the two middle numbers. Divide the length of the array by two to find the index of the greater middle item. Subtract this number by 1 to find the index of the lesser middle item. Add the values at these indices together and divide by 2.0 to find the median.

Remember, the length of the array is the total number of elements. The indices of an array start at zero. If an array has 5 elements, its length is 5, but its indices run from 0 to 4.

If the array has 5 elements, the 3rd element is the one in the middle. When we divide length/2 the answer is 2.5, but since it's an int, 5/2 will equal 2. So we can use length/2 to find the middle index. Even though it 5/2 returns 2, 2 is the proper index of the third, middle element.

If the array has 6 elements, then the 3rd and 4th terms are the two middle numbers we need to find the average of. When we divide 6/2, we get 3. Since the indices start at zero, index 3 will give us the fourth item in the sequence (the higher middle number). We must subtract the index by 1 to find the index of the lower middle number.

Return the median once you find it.

### The Mode Method

We're going to assume our data set only has one mode, or no modes. We will ignore the situation of multiple modes. If we entered the terms 5, 5, 6, 6, 7, and 8, we would have two modes - 5 and 6. We're going to ignore this possibility.

Create a variable to keep track of the mode. It should be a double. Create a variable to keep track of the number of times this number shows up in the array. It should be an int.

Now, you need to figure out how many times each number appears in the array. You will have to loop through each value in the array.

Create an inner loop that again goes through each value in the array. It should check if the current value found matches the value we're checking for. If they match, then increment the number of occurrences found by 1.

Check if the amount of times that this number appears is greater than the amount of times we have recorded for the last mode found. If we found more of this number than we did for another number, then this must be the new mode. Update the mode with the current found value and set the total amount of occurrences to the occurrences found for this number.

Return the mode you found. If the total number of occurrences found is 1, then only 1 of each number must exist. So return -1 in this case.

### Sample Output

When you run the program, it should work something like this:

How many values are you entering? 5 Please begin entering your values. 5 2.5 3 3 4 Sorted values: 2.5, 3.0, 3.0, 4.0, 5.0, The mean is: 3.5 The median is: 3.0 The mode is: 3.0