CS 145 Lab 9 – Arrays
Welcome to lab 9!
If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.
In this lab you will work with data that has been collected into arrays. Arrays let us number our data, making it easy to loop through the collection or associate data with the natural numbers.
Person A types.
Solve one of the following problems:
- Suppose you have an array of 10
Stringnames. You want to select 5 of them for a drug test. Repeat subjects are not allowed. Write a method
pick5that accepts an array containing the 10 names as a parameter and prints 5 unique names drawn at random.
- You have a list of people running for election stored in a
Stringarray. Write a method
enumeratePairsthat receives this array as a parameter and enumerates all possible president/vice president pairs. For example, if the list contains Alice, Bob, and Carol, then the possible tickets are:
P: Alice, VP: Bob P: Alice, VP: Carol P: Bob, VP: Alice P: Bob, VP: Carol P: Carol, VP: Alice P: Carol, VP: Bob
- A lot of data is impossible to use if it’s not sorted. (Find me an unsorted phonebook!) There are many algorithms for sorting data in an array. One simple one is called the selection sort. It can be described in pseudocode:
for each cell in the list find the smallest value in the sublist starting at the current cell swap that smallest value with the value in the current cellWrite a method
selectionSortthat receives an array of
ints as a parameter and sorts it using this algorithm.
Person B types.
Complete a second problem from the list above.