Lesson 4 Arrays and Lists Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments. E-mail address is: csc123csudh@gmail.com Each program is to be submitted in a separate file with the file name being the class name with extension.java as shown below. I only need the source file. GeneralAverage.java The other homework file should be submitted with your name using the following format: RosenthalH_Ax.doc, where x is the assignment number. Note: use your name, not mine All answers will be posted on web site, and most will be reviewed in class. Review Chapter 7 in Bravaco Short Answers 1. In an array declaration, this indicates the number of elements that the array will have. a. subscript b. size declarator c. element sum d. reference variable 2. Each element of an array is accessed by a number known as a(n). a. subscript b. size declarator c. address d. specifier 3. The first subscript in an array is always. a. 1 b. 0 c. 21 d. 1 less than the number of elements 4. The last subscript in an array is always. a. 100 b. 0 c. 21 d. 1 less than the number of elements 1
5. This array field holds the number of elements that the array has a. size b. elements c. length d. width 6. This search algorithm steps through an array, comparing each item with the search value. a. binary search b. sequential search c. selection search d. iterative search 7. This search algorithm repeatedly divides the portion of an array being searched in half. a. binary search b. sequential search c. selection search d. iterative search 8. This is the typical number of comparisons performed by the sequential search on an array of N elements (assuming the search values are consistently found). a. 2N b. N c. N 2 d. N/2 9. When initializing a two-dimensional array, you enclose each row s initialization list in. a. braces b. parentheses c. brackets d. quotation marks 10. True or False: Java does not allow a statement to use a subscript that is outside the range of valid subscripts for an array. 11. True or False: An array s size declarator can be a negative integer expression. 12. True or False: Both of the following declarations are legal and equivalent: int[] numbers; int numbers[]; 13. True or False: The subscript of the last element in a single-dimensional array is one less than the total number of elements in the array. 14. True or False: The values in an initialization list are stored in the array in the order that they appear in the list. 2
15. True or False: When an array reference is passed to a method, the method has access to the original array. 16. True or False: The first size declarator in the declaration of a two-dimensional array represents the number of columns. The second size declarator represents the number of rows. 17. What are the values stored in the array a after the following code executes? int [] a = new int[10]; for (int i = 0; i < a.length; i++) a[i] = 2*i 1; 18. What are the values stored in the array a after the following code executes? int [] a = new int[10]; a[0] = 1; for (int i = 1; i < a.length; i++) a[i] = 2*a[i-1] 1; Find the error(s) in each of the following code segments for questions 19-24. 19. char[] a = new char[10]; a[10] = s ; a[9] = 76; 20. char[] a = new char[10]; char[9] = s ; 21. char[] a = new int[10]; a[0] = s ; a[1] = 80; 22. int[] a; a = new int[255]; a[0] = s ; a[a[0]] = 35; a[35] = 12345654321; 23. int[] a; a = new int[255]; a[0] = a.length; a[0]--; 3
a.length--; a[a[0]] = 2; 24. int[] b = new int[9]; int[][] a; a = new int[255][]; a[0] = b; a[0][3] = 9; a[3][0] = 9; Homework Programs Do 7 of the first 9. Number 10 is a bonus. 1. ReversePrint Write a program (all in main) Creates an int array of size 100 Reads in up to 100 elements, stopping when you read in -999 (which is not put into the array. Prints out the array elements read in in reverse order. 2. RandomFill Write a program that does the following: In main: Read in the size of an int array Create an array of that size Call randomfill with a reference to the array Print out the elements of the array In randomfill: Fill the array with random numbers from 2 to 12. (Note: you don t need to return anything) 3. SumEvenOddAll Write a program that does the following: In main: Creates an int array of size 100 Calls fillarray which accepts a reference to the created array Calculates the sums of the even numbers, odd numbers and all the numbers in the arrays Prints out the three sums fillarray does the following Accepts a reference to an int array Reads numbers into the array until it reads a -999 or the array is filled up. Return the number of elements read into the array 4
4. CoffeeAcid Write a program that does the following: In main: Read in the length of an array of type double. Call createarray with the length Read in the PH value as floating point number greater than 0.0 and less than 7.0 into the array created in createarray. (assume values read in are correct) Find the average of the elements in the array. Find the element in the array furthest from the average. Print out the average, and the value and index of the element furthest from the average. In createarray: Create an array of type double with the length in the formal parameter. Return the reference to that array. Notes: If more than one value is the furthest can just list the first. Remember to use absolute value when testing the difference from the average. 5. Rainfall Write a Rainfall class that stores the total rainfall for each of 12 months into an array of doubles. The main programs requests and reads in the rainfall by month. Do not accept negative numbers for monthly rainfall figures. The main method also prints out the data returned from two separate methods that return the following: the total rainfall for the year the average monthly rainfall 6. Lottery Write a Lottery class that simulates a lottery. The class should have an array of five integers in the range of 0 through 9 for each element in the array that are generated randomly in a method called generatelotterywinner. The class should have a method called getbettorpicks that is used to request the user s five picks between 0 and 9 and return a reference to that array. The class should also have a method comparenumbers that compares the corresponding elements in the two arrays and return the number of digits that match. Demonstrate the class in a program that asks the user to enter five numbers. The program should display the 5 lotter winners and the number of digits that match the randomly generated lottery numbers. If all of the digits match, display a message proclaiming the user a grand prize winner. 7. TwoDAverage In main; Read in the number of rows and columns of a regular two-dimensional int array. Read in the elements row by row. Call average2d with a reference to the array read in. 5
Print out the average returned by average2d. In average2d Calculate the average as a type double and return as a type double. 8. CountGreaterThanTen Write a class CountGreaterThanTen that does the following: The main method does the following: a. Requests from the user the dimensions for a two dimensional array. b. Calls the method createarray to create a two-dimensional integer array. c. Calls a method fullarray with a reference to the array created by createarray. d. Calls a method called greaterten with a reference to the array created by createarray. e. Prints out the final elements of the two-dimensional array row by row. f. The main method then prints out the final values in the array and a final sentence stating: It is (true/false) that there are more entries in the array greater than ten than less than or equal to ten. createarray does the following: a. Accepts the dimensions of a two dimensional int array as parameters. b. Creates a two-dimensional int array using the input parameters. c. Returns a reference to the array that was created. fullarray does the following: a. Has a two-dimensional int array as its formal parameter. b. Fills a two-dimensional int array with random integers from 1 to 20. greaterten does the following: a. Has a two-dimensional int array as its formal parameter. It then counts the number of entries greater than ten and returns true if there are more numbers greater than ten in the array. Otherwise it returns false. 9. RaggedArrays - Note: Typo in book r is supposed to be i The following code defines a ragged two-dimensional array. The term ragged indicates that the rows are not all the same length. int[][] triangle = new int[5][]; // allocate array of rows for (int i = 0; i < triangle.length; i++) triangle[i] = new int[i+1]; a. Write a method int [][] buildragged(int n) that returns a (possibly) ragged array with n rows. The method reads n + 1 lines of data from the console. The first line contains the number of rows of a ragged array. Each succeeding line specifies one row of a ragged array. The first entry of each line gives the number of items in that row. For example, the input 3 3 1 5 7 6 5 6 8 9 3 2 6
2 5 8 indicates that there are 3 rows and that the first row of the ragged array has 3 entries (1, 5, and 7), the second row 6 entries (5, 6, 8, 9, 3, and 2), and the third row 2 entries (5 and 8). b. Write a method void printarray(int [][] x) that displays a (possibly) ragged array. c. Test your methods in a program using the main(...) method 10. MergedArrays (Bonus) In the main program: Read in the lengths of two arrays. The arrays don t need to be of the same length Create each of the arrays as an int array. For each array call randomfill with a reference to the array. For each array, call maxsort with a reference to the array that sorts the array from least to greatest Call mergearrays with references to each of the two arrays Print out the two original arrays after they have been sorted and the merged array whose reference is returned by mergearrays randomfill: Accepts a reference to an int array. Fills the array with random integers between 1 and 100. maxsort: Accepts a reference to an int array. Sorts the array. You may copy the appropriate code from the MaxSort.java file including the maxsort and max methods mergearrays: Accepts the references from two int arrays Creates a third array that includes all the elements of the first two sorted arrays This third array create has a length equal to the sum of the lengths of two arrays, and has the elements merged from least to greatest. Returns a reference to the merged array. 7