Design an algorith (like the one I have started, but note this instructor\'s res
ID: 2965448 • Letter: D
Question
Design an algorith (like the one I have started, but note this instructor's response). Question: Design an algorithm whose input is a finite list of positive integers and whose output is the smallest interger in the list.
My answer is Min (a_1, a_2,...,a_n:positive integer)
Min=a1
for k = 2 to n
> {If min>a_k then min = ak}
>
> (If min < a_k then output min)
This line isn't needed (INSTRUCTOR'S REPLY), and in
fact makes the algorithm not work
correctly. The algorithm won't
work in the case where this line
is included in the for loop either.
To see that, go through the steps
with the input list a1 = 2, a2 = 1.
A correct last line could be:
output min
ALSO: You should indicate in some way which statements
are inside the for loop.
HOW DO I FIX THIS PROBLEM?
It can be done in a different form like:
step 1 set min=a1
step 2 set k=2
step 3 if k<n, then output min and stop
step 4 if min>a_k, then set min=a_k
step 5: replace k by k+1 and go to step 3. (silly by I don't have idea on how to write this code correctly. Please, can someone correct my errors?)
Explanation / Answer
Yes, the last line is not needed . Remove it and add Output min in the last line outside the for loop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.