Arrays

Imagine the following problem: you are given a list of 10 numbers and you must compute how many are greater than the average. For example, for 1, 2, 5, 20, 32, 2, 3, 4, 0, 10 the average is 7.9 meaning that 3 numbers are above the average.

A simple (but very long) solution would be the following:

Scanner input = new Scanner(in);
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
int e = input.nextInt();
int f = input.nextInt();
int g = input.nextInt();
int h = input.nextInt();
int i = input.nextInt();
int j = input.nextInt();


double average = (a + b + c + d + e + f + g + h + i + j) / 10.0;

int aboveAverage = 0;

if (a > average) aboveAverage++;
if (b > average) aboveAverage++;
if (c > average) aboveAverage++;
if (d > average) aboveAverage++;
if (e > average) aboveAverage++;
if (f > average) aboveAverage++;
if (g > average) aboveAverage++;
if (h > average) aboveAverage++;
if (i > average) aboveAverage++;
if (j > average) aboveAverage++;

out.println(aboveAverage);

This solution works, but it is very badly coded. Much of the code is duplicated, therefore it is harder to find and fix bugs, if there are any. Also, it doesn't scale well. What if there were 100 numbers in the list? What if the size of the list changed from test case to test case? A much better solution to this problem would use arrays. Arrays are variables that hold a list of data. They can be created as follows.

int [] numArray = new int[10]; // creates an array called numArray that holds 10 ints 
String [] asdf = new String[231]; // creates an array called asdf that holds 231 Strings
double [] a = new double[3]; // creates an array called a that holds 3 doubles

To access an individual variable (or "element") within an array, use something similar to the following.

int [] numArray = new int[10];
numArray[3] = 10; // set the 3rd element of numArray equal to 10
numArray[1] = 32; // set the 1st element of numArray equal to 32
numArray[0] = numArray[1] + numArray[3]; // set the 0th element equal to the sum of the 1st and 3rd elements
numArray[1] += 3; // add three to the first element
out.println(numArray[0] + " " + numArray[1]); // prints "42 35"

Notice how there is an element at numArray[0]. The indices of an array go 0, 1, 2, ..., size-1. That means an array with, say, 10 elements doesn't have a 10th element, but it does have a 0th element and a 9th element.

int [] numArray = new int[4];
numArray[0] = 1; // okay: arrays have 0th elements
numArray[1] = 1; // okay
numArray[2] = 1; // okay
numArray[3] = 1; // okay
numArray[4] = 1; // not okay: arrays do not have elements at their size
numArray[-1] = 1; // not okay: arrays do not have elements at negative indices
numArray[32] = 1; // not okay: arrays do not have elements at indices greater than their size

Note that arrays can't change their size after they have been created. However, the size of an array doesn't have to be determined at compile time. For example:

Scanner input = new Scanner(in);
int n = input.nextInt();
int [] numArray = new int[n]; // the input decides how many elements are in this array
String [] strArray = new String[n*2]; // this array has twice as many elements as numArray

Finally, array.length is a int that denotes how many elements are in array.

To put it all together, here is a solution to the original problem that uses arrays:

Scanner input = new Scanner(in);
int [] nums = new int[10]; 
int nums[0] = input.nextInt();
int nums[1] = input.nextInt();
int nums[2] = input.nextInt();
int nums[3] = input.nextInt();
int nums[4] = input.nextInt();
int nums[5] = input.nextInt();
int nums[6] = input.nextInt();
int nums[7] = input.nextInt();
int nums[8] = input.nextInt();
int nums[9] = input.nextInt();
int nums[0 = input.nextInt();


double average = (nums[0] + nums[1] + nums[2] + nums[3] + nums[4]
                + nums[5] + nums[6] + nums[7] + nums[8] + nums[9]) / 10.0;

int aboveAverage = 0;

if (nums[0] > average) aboveAverage++;
if (nums[1] > average) aboveAverage++;
if (nums[2] > average) aboveAverage++;
if (nums[3] > average) aboveAverage++;
if (nums[4] > average) aboveAverage++;
if (nums[5] > average) aboveAverage++;
if (nums[6] > average) aboveAverage++;
if (nums[7] > average) aboveAverage++;
if (nums[8] > average) aboveAverage++;
if (nums[9] > average) aboveAverage++;

out.println(aboveAverage);

At this point, you may be thinking that arrays made the code longer, if anything. Well, notice how most of the code is duplicated, but with a single number changed. If we had a variable that iterated from 0 to 9 and executed the code, the solution would be much shorter. Enter the for loop.

Scanner input = new Scanner(in);
int n = 10; // this line determines how many numbers are in the list
int [] numArray = new int[n];

for (int i = 0; i < numArray.length; i++) {
    numArray[i] = input.nextInt();
}
int sum = 0;
for (int i = 0; i < numArray.length; i++) {
    sum += numArray[i];
}
double average = 1.0*sum / numArray.length;
int aboveAverage = 0;

for (int i = 0; i < numArray.length; i++) {
    if (numArray[i] > average) aboveAverage++;
}
out.println(aboveAverage);

Not only is this solution shorter that the original, but a lot easier to scale. Changing int n = 10; to int n = 100; makes the solution work for lists of 100 numbers, and int n = input.nextInt(); makes it work when the size of the list is given in the input.

results matching ""

    No results matching ""