CSC 123 FALL 2019



Lesson 4 Arrays and Lists Review CSC 123 Fall 2019Notes: 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@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.javaThe 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 mineAll answers will be posted on web site, and most will be reviewed in class.Short Answers1. In an array declaration, this indicates the number of elements that the array will have. ______a. subscriptb. size declaratorc. element sumd. reference variable2. Each element of an array is accessed by a number known as a(n) __________. a. subscriptb. size declaratorc. addressd. specifier3. The first subscript in an array is always ___________. a. 1b. 0c. 21d. 1 less than the number of elements4. The last subscript in an array is always __________. a. 100b. 0c. 21d. 1 less than the number of elements5. This array field holds the number of elements that the array has ________a. sizeb. elementsc. lengthd. width6. This search algorithm steps through an array, comparing each item with the search value. ___a. binary searchb. sequential searchc. selection search d. iterative search7. This search algorithm repeatedly divides the portion of an array being searched in half. ____a. binary searchb. sequential searchc. selection search d. iterative search8. 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. 2Nb. Nc. N2 d. N/29. When initializing a two-dimensional array, you enclose each row’s initialization list in ___________.a. bracesb. parenthesesc. bracketsd. quotation marks10. 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.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]--; 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 ProgramsZeroSum (10)Write a program that accepts two lists of integers, each terminated by the sentinel -999, and reports whether or not there are two values, one from each list, withsum equal to zero. Your program should utilize two methods: one that reads a list of integers into an array (see Exercise 1a) and another that returns true if and only if there are two values, one from each array, with sum equal to zero. For example, the two arrays [2, -3, 1, 7, 9] and [6, 7, -5, -3, 4, -2] satisfy the “zero sum” criterion because the first array contains 2 and the second -2.Enter up to 100 integers, terminating the list with -999:8 7 -2 -6 -9 -14 8 2 -999Enter up to 100 integers, terminating the list with -999:-8 0 6 6 6 6 -999It is true that there is a zero sum pair. Enter up to 100 integers, terminating the list with -999:8 2 4 5 -3 -1 -8 -7 -4 3 6 -999Enter up to 100 integers, terminating the list with -999:1 2 3 -999It is true that there is a zero sum pair.Enter up to 100 integers, terminating the list with -999:8 7 6 5 -4 -3 -2 -1 -999Enter up to 100 integers, terminating the list with -999:18 16 -4 -3 7 -999It is false that there is a zero sum pair.DiceRoll - 100000 rolls look for table of results (10)Write a program that simulates rolling two dice 100,000 times and displays the number of occurrences of each sum from 2 to 12.RollCount 2 2767 3 5603 4 8619 510970 613895 716469 814044 911093 10 8239 11 5597 12 2704MergedArrays (20)In the main program:Read in the lengths of two arrays. The arrays don’t need to be of the same lengthCreate 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 greatestCall mergeArrays with references to each of the two arraysPrint out the two original arrays after they have been sorted and the merged array whose reference is returned by mergeArraysrandomFill: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 methodsmergeArrays:Accepts the references from two int arraysCreates 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.Please enter the number of cells in each of two integer arrays: 4 6The sorted first array is: 13 47 75 96 The sorted second array is: 4 14 22 64 68 78 The sorted merged array is: 4 13 14 22 47 64 68 75 78 96 Please enter the number of cells in each of two integer arrays: 6 4The sorted first array is: 9 22 36 70 80 98 The sorted second array is: 69 81 89 92 The sorted merged array is: 9 22 36 69 70 80 81 89 92 98 Please enter the number of cells in each of two integer arrays: 9 9The sorted first array is: 1 4 40 57 65 77 84 88 88 The sorted second array is: 3 26 53 55 58 58 67 73 80 The sorted merged array is: 3 4 26 40 53 55 57 58 58 65 67 73 77 80 84 88 88CheckingAccount (20)Write a program that processes checking account transactions. A positive entry signifies a deposit, a negative number denotes a withdrawal, and zero signals the end of data. Your program should display a checkbook ledger. Assume an initial balance of zero. For example, if input to the program is 10.52, 1900.78, -234.78, 0, then the program displays:Transaction BalanceDeposit 10.52 10.52Deposit 1900.78 1911.30Withdrawal 234.781676.52Enter the list of up to 100 checking transactions, with a - meaning a withdrawal. Enter a 0 to terminate the list:88.99 -76.65 76.99 1237.80 -321.99 0Transaction BalanceDeposit 88.99 88.99Withdrawal 76.65 12.34Deposit 76.99 89.33Deposit 1237.80 1327.13Withdrawal 321.99 1005.14ArrayOperations2D (20)Write a program that creates a two-dimensional array initialized with test data. Use anyprimitive data type that you wish. The program should have the following methods:fillRandom. Accepts a reference to a two-dimensional array and fills it with random integers from 0 to 99formatPrint. This method should accept a two-dimensional array and print it out row by rowgetTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array. Calls getTotal and getElementCount.getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the sub- script of a row in the array. The method should return the total of the values in the specified row.getColumnTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The method should return the total of the values in the specified column.getHighestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the highest value in the specified row of the array.getLowestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the sub- script of a row in the array. The method should return the lowest value in the specified row of the array.getElementCount. This method should accept a two-dimensional array and returns the total number of elements in the array.Demonstrate each of the methods in this program. Each (except for getElementCount, are called from main.The main program will request the number of rows and columns as input, creates the two-dimensional array, and first calls fillRandom. A sample output is:Please enter the number of rows and columns in a two dimensional array: 4 5 78 65 72 30 95 60 71 88 41 73 32 74 47 70 27 59 91 80 81 87Processing the int array.Total : 1321Average : 66.05Total of row 0 : 340Highest in row 0 : 95Lowest in row 0 : 30Total of row 1 : 333Highest in row 1 : 88Lowest in row 1 : 41Total of row 2 : 250Highest in row 2 : 74Lowest in row 2 : 27Total of row 3 : 398Highest in row 3 : 91Lowest in row 3 : 59Bonus: LoShuMagicSquare (20 points)The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in the figure below. The Lo Shu Magic Square has the following properties:The grid contains the numbers 1 through 9 (each number only once)The sum of each row, each column and each diagonal are the same (see figure)In a program you can simulate a magic square using a two-dimensional array. Write a method that accepts a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. Test the function in a program.Detailed Description:In main create two arrays:// Create a magic two-dimensional array. int[][] magicArray = { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }; // Create a normal two-dimensional array. int[][] normalArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 8} };For the normalArray call showArray and then showResult. Then do the same for magicArrayshowArray accepts a two-dimensional array and prints it row by rowshowResult call isMagicSquare with the two-dimensional array reference as the parameter. It prints a message based on the return value which is Boolean.The isMagicSquare method accepts a two-dimensional int array as an argument, and returns true if the array meets all the requirements of a magic square. Otherwise it returns false. You can create separate methods to checkRange (all must be from 1-9), checkUnique (see if all values are unique), checkRowSum, checkColSum, checkDiagSum. If each of these 3 return true then it is a Magic Square.1 2 3 4 5 6 7 8 8This is not a Lo Shu magic square. 4 9 2 3 5 7 8 1 6This is a Lo Shu magic square. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download