created 05/31/03
Complete the following program so that it
computes the sum of all the elements of the array,
the sum of the even elements,
and the sum of the odd elements.
Assume that all the numbers are zero or positive.
Even integers are those for which N%2
is zero.
import java.io.* ; class ThreeSums { public static void main ( String[] args ) throws IOException { int[] data = {3, 2, 5, 7, 9, 12, 97, 24, 54}; // declare and initialize three sums // compute the sums for ( int index=0; index < data.length; index++) { } // write out the three sums System.out.println( ); } }
Click here to go back to the main menu.
Complete the following program so that it computes and writes out the two largest elements in the array.
import java.io.* ; class TwoLargest { public static void main ( String[] args ) throws IOException { int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2}; // declare and initialize variables for the two largest // compute the two largest for ( int index= ; index < data.length; index++) { } // write out the two largest System.out.println( ); } }
Click here to go back to the main menu.
Complete the following program so that it
reverses the order of the values in data
,
then prints it out.
In the first version of the program there is only one array and its values are reversed with some fairly tricky programming.
import java.io.* ; class ReverserVersion1 { public static void main ( String[] args ) throws IOException { int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14}; // reverse the data for ( int j=0; j < be careful here; j++) { } // write out the new data for ( int j=0; j < data.length; j++) { } } }
Now write another program that uses two arrays.
The first array data
is not changed.
The second array result
gets the elements
of data
in reversed order.
import java.io.* ; class ReverserVersion2 { public static void main ( String[] args ) throws IOException { int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14}; int[] result = // copy the data in reversed order to result for ( int j=0; j < be careful here; j++) { } // write out the result for ( int j=0; j < result.length; j++) { } } }
Click here to go back to the main menu.
An audio signal is sometimes stored as a list
of int
values.
The values represent the intensity of the signal
at successive time intervals.
Of course, in a program the signal is represented
with an array.
Often a small amount of noise is included in the signal. Noise is usually small, momentary changes in the signal level. An example is the "static" that is heard in addition to the signal in AM radio.
Smoothing a signal removes some of the noise and improves the perceptual quality of the signal. This exercise is to smooth the values in an integer array.
Say that the original values are in the array "signal".
Compute the smoothed array by doing this:
Each value smooth[N]
is the average
of three values: signal[N-1]
,
signal[N]
, and signal[N+1]
.
For the first element of smooth
,
average the first two elements of signal.
For the last element of smooth
,
average the last two elements of signal.
Use integer arithmetic for this so that the
values in smooth
are integers.
import java.io.* ; class Smooth { public static void main ( String[] args ) throws IOException { int[] signal = {5, 5, 4, 5, 6, 6, 7, 6, 5, 4, 1, 4}; int[] smooth // compute the smoothed value for each // cell of the array smooth smooth[0] = smooth[ signal.length-1 ] = for ( ) { } // write out the input for ( int j=0; j < smooth.length; j++) { } // write out the result for ( int j=0; j < smooth.length; j++) { } } }
In interpretting the results, remember that integer division discards the remainder. It does not compute a rounded value. Here is a sample run of the program:
C:\>java Smooth signal: 1 5 4 5 7 6 8 6 5 4 5 4 smooth: 3 3 4 5 6 7 6 6 5 4 4 4 C:\>
Click here to go back to the main menu.
Say that you are interested in computing the average acid level of coffee as served by coffee shops in your home town. You visit many coffee shops and dip your pH meter into samples of coffee. You record your results in a text file such as the following. The first line of the file gives the number of values that follow.
13 5.6 6.2 6.0 5.5 5.7 6.1 7.4 5.5 5.5 6.3 6.4 4.0 6.9
Unfortunately, your pH meter is known to sometimes produce false readings. So you decide to disregard the reading that is most distant from the average.
Create a text file containing the above or similar data. Now write a program that reads the data into an array (use input redirection as explained in Chapter 22). Compute the average of all the data. Now scan through the array to find the value that is farthest (in either direction) from the average. Set this value to -1 to show that it is not to be included. Compute and print the new average.
Here is a run of the program:
C:\>java CoffeeAverage < CoffeeData.txt data[ 0 ] = 5.6 data[ 1 ] = 6.2 data[ 2 ] = 6.0 data[ 3 ] = 5.5 data[ 4 ] = 5.7 data[ 5 ] = 6.1 data[ 6 ] = 7.4 data[ 7 ] = 5.5 data[ 8 ] = 5.5 data[ 9 ] = 6.3 data[ 10 ] = 6.4 data[ 11 ] = 4.0 data[ 12 ] = 6.9 average: 5.930769230769231 most distant value: 4.0 new average: 5.6230769230769235
Click here to go back to the main menu.