In this lab you will cover:
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.
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.
repeat until (i >n)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 ...In each of the following cases, explain exactly what would happen if this instruction were changed to the indicated instruction:
1 + 22 + 32 + 42 +...+n2
1+2+....+nUsing 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.
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.