teaching machines

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.

Checkpoint 1

Person A types.

Solve one of the following problems:

  1. Suppose you have an array of 10 String names. You want to select 5 of them for a drug test. Repeat subjects are not allowed. Write a method pick5 that accepts an array containing the 10 names as a parameter and prints 5 unique names drawn at random.
  2. You have a list of people running for election stored in a String array. Write a method enumeratePairs that 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
  3. 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 cell
    Write a method selectionSort that receives an array of ints as a parameter and sorts it using this algorithm.

Checkpoint 2

Person B types.

Complete a second problem from the list above.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *