Using partially-filled arrays.

Create a java program called PartialArrays.java.

We often create an array which holds the exact number of elements we will be using. When we initialize the array we already know how many items there will be. If we do not know this information when we begin to read data (for example in the case of a sentinel loop), we can create an array large enough to hold as much data as we might want, but ignore the values which don’t end up getting used. This approach is called a partially filled array.

For example, we might make space for up to 10 values in an array called data:

data = new double[10];

We then use a sentinel loop to read the data. Suppose the user enters 5 values. The resulting array would look like:

{5.2, 0.0, 2.6, -12.3, 27.1, 0.0, 0.0, 0.0, 0.0, 0.0}

Note the last five 0.0 values are not entered by the user, so they are not valid. Also note that the second item in the array is valid and is also 0.0. To manage the array we will also keep track of how many of the indices contain valid data in a variable:

final int validItems;

We can set the value of this variable once the user is done inputting values. The user should not be allowed to input more values than the array can hold.

Declare the constant MAX_ITEMS:

Create an integer constant called MAX_ITEMS (outside of main, but inside of your class). This will be the size of your array, set it to something good for testing like 10.

Add the main method:

Use the following main method with your code. You can comment out parts that aren't working yet, or write stubs for your methods to start with.

public static void main(String[] args) {
	// Find the pair of values whose sum is closest to zero
	Scanner input = new Scanner(System.in);
	
	double[] data = new double[MAX_ITEMS]; 
	final int validItems = readData(input, data);
	
	int[] pair = findClosestPair(data, validItems);
		
	System.out.println("The pair whose values are closest to each other is");
	System.out.printf("data[%d] : %f and data[%d] : %f\n", pair[0], data[pair[0]], pair[1], data[pair[1]]);
	System.out.printf("Their difference is %f\n", Math.abs(data[pair[0]] - data[pair[1]]));
	
	printArray(data, validItems);
		
}

Write the readData method:

public static int readData(Scanner in, double[] data)

Create a method called readData which fills the data array from the user input. The data array should already be initialized to contain MAX_ITEMS values. Use the hasNextDouble and nextDouble methods of the Scanner to read values until the user fills up the array or enters something other than a number. It should return the number of items the user entered.

Write the printArray method:

Write a method called printArray which prints all of the valid values in the array. The method should take the array and the number of valid values as parameters.

Write the findClosestPair method:

public static int[] findClosestPair(double[] data, int valid)

Write a method called findClosestPair which finds the indices of the two valid items in the array which are closest together (difference is nearest to 0). Since a method can only return one value, this one returns an array of length two. Each of the values is an index in the data array.

Use nested for loops to evaluate pairs of values in the array e.g. data[i] - data[j]. Do not consider the pairs where i == j.

Sample Execution 1:

Enter up to 10 numbers (non-number to end): 2.3
-6.7
6.2
5.9
13.3
q
The pair whose values are closest to each other is
data[2] : 6.200000 and data[3] : 5.900000
Their difference is 0.300000
{ 2.300000, -6.700000, 6.200000, 5.900000, 13.300000 }

Sample Execution 2:

Enter up to 10 numbers (non-number to end): 1 6 3 8 3
-1 5 7 2 9
The pair whose values are closest to each other is
data[2] : 3.000000 and data[4] : 3.000000
Their difference is 0.000000
{ 1.000000, 6.000000, 3.000000, 8.000000, 3.000000, -1.000000, 5.000000, 7.000000, 2.000000, 9.000000 }