## CS107 - Lab 2

Due Tuesday 09/21 at the beginning of class.

In this lab you will cover:

• Come up with an algorithm for the (sequential) search problem discussed in class.
• Start accessing the course software and play with the sequential search simulator.
• Design new algorithms (textbook problems chapter 2).
The reading for this lab is Chapter 2 in the textbook.

### An algorithm for sequential search

Write an algorithm that, given a list of numbers and a target value, searches for the target value in the list and outputs whether it has found it or not. In case it finds it, it also output the position in the list where it finds it. For instance, if the user inputs
```2 5 3 8 6 10
6
```

```Target value 6 found at position 5 in the list.
```

If the user inputs the same list, but target vale 4, your algorithm should print

```Target value 4 not found  in the list.
```

Before reading the notes or the book, try and come up with a solution on your own and show it to me. It is important to try to do this on your own before before you look at the notes/book.

What is the key idea of your algorithm? Why do you think the algorithm is called sequential search? You do not need to write down the answers, only to think about them.

Now look at the search algorithm in Figure 2.9 in the book. The problem is slightly different, searching for a target name in a list of 10000 input names. However the algorithm is conceptually the same as the one for searching a list of numbers. Work through a couple of examples and understand how the algorithm works.

### Accessing the course software

1. Select the menu item Go -> Connect to Server and type afp://collaboration.bowdoin.edu
3. A new icon called Computer Science will appear on the desktop.
4. Double-click the folder Computer Science -> csci107

The following folders are particularly useful for completing this and other assignments in this course.

csci107 -> Users -><your username> is a folder in which you can save work from week to week during the semester.

csci107 -> Invitation contains the lab software for many exercises in this course.

csci107 -> Drop Box is the place where you can submit an electronic copy of your completed work when you finish an assignment.

Drag a copy of the folder csci107 -> Invitation to your desktop, and then close the folder csci107.

### Using the Search animator

Select the button Search Animator. A window will appear that allows you to simulate the step-by-step behavior of the sequential search algorithms. Take a few minutes and look at the algorithm and check how it is different than the one you came up with.

In this window, select Algorithm -> Search for Value, and then select the Run button. Watch each step execute and the Target value change. You may Reset and Rerun this exercise several times to get a feeling for the relationship between it and the date values.

As you work through the algorithm, think what happens if the target occurs multiple times in the list. Which occurence would the algorithm find? How would you modify the algorithm to find, say, the last, or first, occurence? How would you modify the algorithm to find all occurences? You do not need to write down the answers, only to think about them.

### Designing Algorithms

Complete exercises 7, 8, 9, 13, 15, 16, 17 at the end of Chapter 2 of your textbook.

### What to turn in:

• The Designing Algorithms section.

Be sure to justify your answers! You may do this work either with a word processor or by hand. If you type your solutions, bring a hard copy of your assignment and hand it in class. If you write by hand, do your best at writing legibly and leave space between problems.

You may choose to do this assignment either by yourself or in a group. However, solutions should be written up individually and handed in on Tuesday, at the beginning of class.

Once you are finished in the lab, if you saved any documents in yout collaboration personal directory, be sure to drag the csci107 icon to the Trash (this step disconnects you from the server and prevents someone else, who may use this iMac later in the day, from accidentally accessing files in your personal folder) and log out!

#### Accessing the Course Software on the Web

If you have a computer in your room, and wish to download the course software to that computer, use Explorer or Netscape and follow these steps:

1.  Go to the Web site http://www.brookscole.com/cgi-brookscole/course_products_bc.pl?fid=M18&discipline_number=6, here.

2.  Scroll down this page and select our textbook (An Invitation to Computer Science - 2nd edition), and then select Lab Files.