Section 11.11 Worked Example: Arrays - Average
Subgoals for Evaluating Arrays.
-
Declaring and initialization of array
- Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
- Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
- Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
-
Accessing array element
- Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
returns value stored at that index- index must be between 0 and
arrayName.length
- 1, inclusive otherwiseIndexOutOfBounds
exception occurs
-
Changing value of an array element
- Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
will now contain the value on the RHS of assignment statement- (remember the assignment subgoals for verifying data types and evaluating expressions)
- (remember rules for index values)
-
Whole array actions
- Pass as argument - a copy of the reference to the instantiated array is passed to the method. This means that any changes made to the array elements inside the method are persistent. The one exception to this is if you assign the argument to reference a different array in memory.
- Assignment - changes the reference to point to the array on the RHS of the assignment operator.
Subsection 11.11.1
Problem: Assume that the integer array
alpha
has been properly declared and initialized with non-default values. What does this code accomplish?int sum = 0;
double avg = 0.0;
for (int i = 0; i < alpha.length; i++)
sum = sum + alpha[i];
if (alpha.length != 0)
avg = (sum * 1.0) / alpha.length;
Subsection 11.11.2 SG1: Declaring and initialization of array
There is no explicit declaration or initialization of an array within the code. However, within the code there are [ ], so we know we are accessing an array.
- alpha is an array of ints and has values, but we don’t know what those values are
- however, we can still diagram a representation of this array
- notice that the largest index is
size
- 1 - notice that
alpha.length
- 1 is the same value assize
- 1
Subsection 11.11.3 SG2: Determine access or action
Within the loop, we are accessing array elements.
Subsection 11.11.4 Evaluating code
int sum = 0;
double avg = 0.0;
The first two lines are declaring two variables, an integer to hold the sum of all the values in the array and a double to hold the average of the values.
References to the array are in the loop and selection statements:
for (int i = 0; i < alpha.length; i++)
sum = sum + alpha[i];
- This loop has index
i
go from 0 tosize
- 1 (<length
) by increments of 1. - Then the value at
alpha[i]
is added tosum
, an int.
This is the same loop from the Section 11.9 example.
if (alpha.length != 0)
avg = (sum * 1.0) / alpha.length;
- If the size of the array is not 0, then it is divided by the number of values in alpha.
- All indexes into the array are valid, and all assignments are valid.
Let us trace with a sample array.
First,
sum
and avg
are both initialized to zero values, and then a for-loop is used to traverse the array to calculate the sum. The chart below uses one line to represent the memory and calculations during each iteration of the loop, starting when i
has a value of zero.Next we look at the selection statement:
if (alpha.length != 0)
avg = (sum * 1.0) / alpha.length;
In our sample,
alpha.length
is 5, and 60 divided by 5 is 12, for a final value of 12 in avg
.Why have the selection statement? What if the array has been declared, but has no values? Then its size is 0 – and we would be dividing by 0! An exception! So we guard against this by checking the length.
Why do we need to multiply sum by 1.0? Remember, an int divided by an int is always an int! Our sample did not have any remainder or decimal value, but that might not always be the case. So we need to make sure that either the divisor or dividend is a double; and one way to do this without affecting the value is to multiply it by 1.0. Another way would be to add 0.0 to the value. Still another way would be to cast the value as a double.
So an alternate equivalent code might be:
if (alpha.length != 0)
avg = (double) sum / alpha.length;
What does thiscode accomplish?
Answer.
avg
contains the average of the values in the array alpha
or 0 if alpha
is empty.Subsection 11.11.5 Practice Pages
You have attempted of activities on this page.