Page 417 - Introduction to Programming with Java: A Problem Solving Approach
P. 417

                10.6 Problem Solving with Array Case Studies 383
There are several things to note about this code fragment. It’s OK to use an expression inside the [ ]’s—we use hours[d+1] to access the element after the hours[d] element. Notice how we shift elements at the low-index end first. What would happen if you started the shifting at the high-index end? You’d over- write the next element you wanted to move and end up filling the entire array with the value that was origi- nally in the highest element. Not good.
Calculating a Moving Average
Let’s now borrow code from the above example and apply it to another problem. Suppose
you need to present a four-day moving average of the Dow Jones Industrial Average (DJIA)
at the end of each business day. Assume you already have a four-element array holding the
values of the DJIA at the end of the day on each of the past four days, with four-days-ago’s value at index 0, three-days-ago’s value at index 1, two-days-ago’s value at index 2, and yesterday’s value at index 3. For today’s four-day moving average, you’ll want the sum of the values for the last three days plus the value for today. This means you’ll need to shift everything in the array to lower-index positions and insert today’s value at the high-index end. Then you’ll need to sum up everything in the array and divide by the length of the array. Presumably, you’ll save the shifted array somewhere and then do the same thing again at the end of each day in the future. You could do the shifting and summing in separate loops, but it’s easier to do both in the same loop as shown in Figure 10.7.
To allow for different lengths of time, it’s best not to hard code the array length. Instead, you should always use <array-name>.length. Think carefully about each boundary. Notice that the index [d+1]on the right side of the first statement in the inside for loop is one greater than the count variable value d.
  Apago PDF Enhancer
Remember that the highest index value in an array is always one less than the array’s length. So the highest value of the count variable should be the array’s length minus two. That’s why the loop-continuation con- dition is d<days.length-1. Also notice that we insert the new final value for the array after the loop terminates, and then we include this final value in the sum before computing the average. Here’s an example of what the program does:
Sample session:
  Enter number of days to evaluate: 4
Enter next day's value: 9800
Moving average = 9650
Enter next day's value: 9800
Moving average = 9725
Enter next day's value: 9700
Moving average = 9750
Enter next day's value: 9600
Moving average = 9725
A moving average is smoother than an instantaneous plot, but notice that its values lag behind.
There’s a simpler way to do shifting. Do you remember the API arraycopy method mentioned in the
previous section? You can use it to implement shifts to lower-index positions with this code fragment:
System.arraycopy(days, 1, days, 0, days.length-1);
System.out.print("Enter next day's value: ");
days[days.length-1] = stdIn.nextInt();
Borrow code and modify it.








































































   415   416   417   418   419