Question: FindMax ( ( a 0 , a 1 , dots, a n - 1 ) ) : largest = a 0 for i = 1

FindMax ((a0,a1,dots,an-1)) :
largest =a0
for i=1,dots,n-1:
if largest ai:
largest =ai
return largest
Consider the loop invariant:
After t iterations, largest is equal to the maximum of (a0,a1,dots,at)
Fill in the blanks of the proof that the loop invariant is correct:
The proof will be by using: induction
Base Case: After 0 iterations of the for loop (before the loop begins:)
Inductive Hypothesis: Assume that for some k, with k>0, that after k-1 iterations, largest is equal to the maximum of Inductive Step: in the k th iteration, there are 2 cases:
Case 1: largest ak By the induction hypothesis largest is the maximum of
and since ,ak largest, ak is the max of (a0,dots,ak). largest =ak,
Case 2: largest >ak By the induction hypothesis largest is the maximum of
and since ak largest, largest is the max of (a0,dots,ak)
 FindMax ((a0,a1,dots,an-1)) : largest =a0 for i=1,dots,n-1: if largest ai: largest

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!