Exercise 06
Exercise 06
Exercise 06
PART I: Test
Exercise 1 : Collections
Which of the following is an advantage of using an array? Simplicity Efficiency Type safety All of the above
1/8
array of a class type yields what? of instances of that class of object references of references of the class type
4. True or false: it is considered good style to explicitly invoke the garbage collector whenever an object allocated on the heap goes out of scope. True False 5. Determining if two references refer to the same object should be done using: The == operator The Equals method The Object.ReferenceEquals method
2/8
3/8
2.
Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week, plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salespersons salary is truncated to an integer amount): a) b) c) d) e) f) g) h) i) $200$299 $300$399 $400$499 $500$599 $600$699 $700$799 $800$899 $900$999 $1000 and over
3.
Use a single-subscripted array to solve the following problem: Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the worst case (in which all 20 numbers are different). Use the smallest possible array to solve this problem.
4/8
2.
3.
(Binary Search) Modify the program in Fig. 7.12 to use a recursive method Binary- Search to perform the binary search of the array. The method should receive an integer array and the starting and ending subscript as arguments. If the search key is found, return the array subscript; otherwise, return 1.
4.
(Quicksort) In this chapter, we discussed the sorting technique bubble sort. We now present the recursive sorting technique called Quicksort. The basic algorithm for a single-subscripted array of values is as follows: a) Partitioning Step. Take the first element of the unsorted array and determine its final location in the sorted array (i.e., all values to the left of the element in the array are less than the element, and all values to the right of the element in the array are greater than the element). We now have one element in its proper location and two unsorted subarrays. b) Recursive Step. Perform step 1 on each unsorted subarray. Each time Step 1 is performed on a subarray, another element is placed in its final location of the sorted array, and two unsorted subarrays are created. When a subarray consists of one element, it must be sorted; therefore, that element is in its final location. The basic algorithm seems simple, but how do we determine the final position of the first element of each subarray? Consider the following set of values (partitioning element in boldit will be placed in its final location in the sorted array): 37 2 6 4 89 8 10 12 68 45 a) Starting from the rightmost element of the array, compare each element to 37 until an element less than 37 is found, then swap 37 and that element. The first element less than 37 is 12, so 37 and 12 are swapped. The new array is 12 2 6 4 89 8 10 37 68 45 Element 12 is italicized to indicate that it was just swapped with 37. b) Starting from the left of the array, but beginning with the element after 12, compare each element to 37 until an element greater than 37 is found, then swap 37 and that element. The first element greater than 37 is 89, so 37 and 89 are swapped. The new array is 12 2 6 4 37 8 10 89 68 45
5/8
c) Starting from the right, but beginning with the element before 89, compare each element to 37 until an element less than 37 is found, then swap 37 and that element. The first element less than 37 is 10, so 37 and 10 are swapped. The new array is 12 2 6 4 10 8 37 89 68 45 d) Starting from the left, but beginning with the element after 10, compare each element to 37 until an element greater than 37 is found, then swap 37 and that element. There are no more elements greater than 37, so when we compare 37 to itself, we know that 37 has been placed in its final location of the sorted array. Once the partition has been applied to the previous array, there are two unsorted subarrays. The subarray with values less than 37 contains 12, 2, 6, 4, 10 and 8. The subarray with values greater than 37 contains 89, 68 and 45. The sort continues with both subarrays being partitioned in the same manner as the original array. Using the preceding discussion, write recursive method QuickSort to sort a singlesubscripted integer array. The method should receive as arguments an integer array, a starting subscript and an ending subscript. Method Partition should be called by QuickSort to perform the partitioning step.
5.
(Maze Traversal) The following grid of #s and dots (.) is a doublesubscripted array representation of a maze: # # . # # # # # # # # # # . . # . # . # . # . # # . # # . # . . . # . # # . . . . # # # . # . # # # # # . . . . . # . # # . . . # # # # . # . # # . # . # . . . . . . # # . # . # # # # . # # # # . # . . . . . . # . # # . # # # # # # # # . # # . . . . . . . . . . # # # # # . # # # # # # #
The #s represent the walls of the maze, and the dots represent squares in the possible paths through the maze. Moves can be made only to a location in the array that contains a dot. There is a simple algorithm for walking through a maze that guarantees finding the exit (assuming there is an exit). If there is not an exit, you will arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from the wall, eventually you will arrive at the exit of the maze. There may be a shorter path than the one you have taken, but you are guaranteed to get out of the maze if you follow the algorithm. Write recursive method MazeTraverse to walk through the maze. The method should receive as arguments a 12-by-12 character array representing the maze and the starting location of the maze. As MazeTraverse attempts to locate the exit from the maze, it should place the character X in each square in the path. The method should display the maze after each move so the user can watch as the maze is solved.
6/8
1. 2.
Set the TypeChecking project active Examine the Base, Level1 and TypeChecking classes and contained methods closely. 3. Execute the project and observe the output.
2.
Based on the inspiration from the accompanying example, you are in this exercise supposed to experiment with some simple C# arrays. First, consult the documentation of the class System.Array. Please notice the properties and methods that are available on arrays in C#. Declare, initialize, and print an array of names (e.g. array of strings) of all members of your group. Sort the array, and search for a given name using System.Array.BinarySearch method. Reverse the array, and make sure that the reversing works.
3.
Consult the documentation of type type System.Enum, and get a general overview of the methods in this struct. Be sure that you are able to find the documentation of System.Enum. See also the exercise about the type char. Test drive the example EnumTest, which is part of MicroSoft's documentation. Be sure to understand the program relative to its output. Write your own program with a simple enumeration type. Use the Enum.CompareTo method to compare two of the values of your enumeration type.
7/8
2.Declares, instantiates, initializes, and prints the contents of a twodimensional array. In this example, a for loop is used to initialize the elements of the array.
8.Add to the ArrayList using the Add( ) method, and the list takes care
of its own internal bookkeeping.
10. Sorting an array by employees' IDs. 11. Demonstrates adding items to a Hashtable and then retrieving them
with the Item property.
8/8