Question: 1-D Arrays Introduction to Computer Programming We will be working with arrays - filling them, passing them to and from methods, and implementing algorithms. Objectives
1-D Arrays
Introduction to Computer Programming
We will be working with arrays - filling them, passing them to and from methods, and implementing algorithms.
Objectives
1. Familiarlization with arrays 2. Pass arrays to methods 3. Return an array from a method 4. Implement common array algorithms
Problem: 1-D arrays and passing arrays to methods
For this program, you will be finishing the code for a program that will:
1. Randomly generate the values of an array of 1000 values
2. Determine the average of the values in the array
3. Find the minimum value of the values in the array
4. Find the maximum value of the values in the array
5. Reverse the contents of an array
Complete the program in the file
The five tasks described above will be accomplished in five separate static methods. Two of the methods have had their method headers written for you (i.e., you need to fill in the method). Three of the methods need to be written from scratch.
Be aware that you may need to add additional code to the file in order to make the program work.
ArrayMethods.java
/* File ArrayMethods.java Fills array with random numbers between 100 and 999 inclusive and finds average, largest, smallest, and reverses array
*/
public class ArrayMethods { /** * main method invoked when program starts * @param args an array of Strings containing possible parameters to main program */ public static void main (String [] args) { int min; /** stores position of minimum value found in array */ int max; /** stores position of maximum value found in array */ double avg; /** stores average of values in array */
/* The code below can be used to test your min(), max(), average(), and reverse methods. Use this section of code and it will use the pre-filled testArr[] array. The largest value should be 5.6, the smallest value should be 1.0, and the average should be 3.151. Reversed values should be: 1.1, 2.1, 3.0, 5.1, 4.01, 5.6, 4.1, 3.3, 2.2, 1.0 When finished, comment out this section of code.
*/
double [] testArr = {1.0, 2.2, 3.3, 4.1, 5.6, 4.01, 5.1, 3.0, 2.1, 1.1}; double [] result; System.out.println("Testing small and large on sample array"); min = smallest(testArr); max = largest(testArr); avg = average(testArr); result = reverse(testArr); System.out.printf("Smallest in Test is %.3f at position %d.%n", testArr[min], min System.out.printf("Largest in Test is %.3f at position %d.%n", testArr[max], max) System.out.printf("The average in Test array is %.3f%n", avg); System.out.println("Here is the reversed array: "); for (int i = 0; i < result.length; i++) { System.out.println(result[i]); }
System.out.println();
/* The code below will create a 1000 element array and then call all four of the methods on that array.
*/
/* final int SIZE = 1000; double [] array = new double[SIZE]; double [] result2; System.out.println("This program is now filling the array."); array = fillArray(SIZE); System.out.println("Average value in array is " + average(array)); System.out.println(); System.out.println("Testing small and large on real array"); min = smallest(array); max = largest(array); avg = average(array); System.out.printf("Smallest in array is %.3f at position %d.%n", array[min], min System.out.printf("Largest in array is %.3f at position %d.%n", array[max], max) System.out.printf("The average in array is %.3f%n", avg); System.out.println("Testing reverse on real array"); result2 = reverse(array); System.out.println("Here is the reversed array: "); for (int i = 0; i < result.length; i++) { System.out.print(result[i] + " "); if (i % 10 == 0)
System.out.println(); }
System.out.println();
*/
}
/** fillArray * fills the array with random whole numbers between 100 and 999 * @param size size of array to be allocated and filled with values * @return array of doubles that will be filled with values */
public static double [] fillArray ( int size) { } /** average * calculates the average value of the array * @param arr array of double values * @return the average of all the values in arr */
public static double average ( double [] arr) { return 0.0; }
/** smallest * smallest will return the position of the smallest value * @param arr the array of doubles that contain values to process * @return an int as the position of the smallest value in the array */
/** largest * largest will return the position of the smallest value * @param arr the array of doubles that contain values to process * @return an int as the position of the largest value in the array */
/** reverse * reverse will return a new array with all of the elements reversed * @param arr the array of doubles that contain values to process * @return an array of doubles, same size as arr, with all the values * reversed from arr */
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
