CS107 - Lab 2
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
your algorithm should print
Target value 6 found at position 5 in the list.
If the user inputs the same list, but target value 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 (or ask
me for a handout). 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.
Designing Algorithms
- Write an algrithm that uses a loop to read in 10 pais of numbers,
where each pair represents the score of a football game with Bowdoin
College score listed first, and determine, for each pair of numbers,
whether Bowdoin won or lost (use the same loop). After reading the 10
pairs of values, print out the won/lost/tie record of Bowdoin. In
addition, if the record is a perfect 10-0, print out the message
"Congrattulations on your undefeated season!!!".
- Write an algorithm that inputs 15 test scores, 14 regular tests and
an examination.Your algorithm should compute and display the average
of all 15 tests, weighting the final exam twice as heavily as a
regular test.
- Write an algorithm that inputs the length and width of a carpet
(in feet), as well as the price in dollars per square feet. The
algorithm prints out the price of the carpet, including a 6% sales
tax. After it finished the computation of one carpet, it starts the
computation of the next carpet. This iterative process is repeated until we
have located a carpet whose total cost is less than 1000$.
- Modify the sequential search algorithm so that it works
correctly even if the desired name target more than once. Your
algorithm should find every occurrence of target in the directory and
print out the location of each match, and the total number of
occurences found. It should still print out "Sorry, this number is
not in the list" if the name is not found at all.
- Consider the following algorithm for finding the largest number
in a list.
Algorithm FindLargest:
Variables: n, list a of n values
get n, a_1, a_2, ..., a_n
set i to 2
set largest to a_1
set location to 1
repeat until (i>n)
if largest < a_i then
set largest to a_i
set location to i
set i to i+1
print "largest value is " largest "at location " location
If the numbers in our list were not unique and therefore the largest
could occur more than once, would the algorithm find the first
occurence? The last occurence? Every occurence? Explain why.
- In the algorithm FindLargest above there is an instruction
that reads:
repeat until (i >n)
In each of the following cases, explain exactly what would happen if
this instruction were changed to the indicated instruction:
- repeat until (i = n)
- repeat until (i = n+1)
- repeat until (i < n)
- In the algorithm FindLargest above consider the instruction
if largest < a_i then ...
In each of the following cases, explain exactly what would happen if
this instruction were changed to the indicated instruction:
- if largest <= a_i then ...
- if largest > a_i then ...
- Write an algorithm that asks the user for a positive integer
n and computes and prints out the sum of the squares of all
numbers smaller than or equal to n, that is:
1 + 22 + 32 + 42 +...+n2
- Recall Gauss's idea for computing the sum
1+2+....+n
Using a similar reasoning, find a formula for the following sum:
2+4+6+...+{the last even number smaller or equal to n}
Your formula should work for both odd and even values of n.
What to turn in:
- The 9 problems 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 to
write 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 the due date, at the beginning of class.
Once you are finished in the lab, if you want to save any
documents, you'll have to drag them to your network folder (mounted to
your desktop).
Files left on the desktop and in your local folder (on the local
machine) will be erased.