78.7k views
3 votes
Find largest algorithm get a value for n, the size of the list get values for a1, a2, ... an, the list to be searched set the value of largest_so_far to a1 set the value of location to 1 set the value to i to 2 while ( i <= n ) do if a1 > largest_so_far then set largest_so_far to a1 set location to i add 1 to the value of i end of the loop print out the values of the largest_so_far and location stop question: if the numbers in our list were not unique and therefore the largest number could occur more than once, would the algorithm find the first occurrence? (select: true for "yes";false for "no")

1 Answer

3 votes

Answer:

The algorithm as written here will do nothing useful, as a1 is always being compared to a1. It will only print a1 and location 1.

Explanation:

As intended to be written, the comparison is "greater than", so there will only be a change in "largest_so_far" when a greater value is found. A value that is the same will not result in any new output. The algorithm as intended will find the first copy of the largest value.

User Lakston
by
8.6k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.