Problem 1: Given is the following algorithm to determine the maximal value of an array A of size N. MaxValue (A[1..N]): max = A[1] i = 2 for (i < N): { if A[i]> max: max = A[i] i=i+1 }| return max A loop invariant is stated as: Variable max holds the maximal value of all values in A[1..i-1] (that is: values at indices 1 to i-1). Using this loop invariant, prove the correct of algorithm MaxValue. Follow the standard three steps... Initialization: Prior to the first iteration, where i = 2, show that the invariant holds. Maintenance: Based on the initialization, the loop invariant is known to holds prior to the some i-th iteration. Show that the loop invariant will also hold prior to the i+1-the iteration. Termination: Show that after the Nth (last) iteration (prior to an imaginary N+1st iteration), the loop invariant still holds. Argue that this state means that the solution to the problem (the largest value in the array) has been found.

icon
Related questions
Question

I need help the question

Problem 1: Given is the following algorithm to determine the maximal value of an array A of
size N.
MaxValue (A[1..N]):
max = A[1]
i = 2
for (i < N) :
{
if A[i]> max:
max = A[i]
i=i+1
}|
return max
A loop invariant is stated as: Variable max holds the maximal value of all values in A[1..i-1] (that
is: values at indices 1 to i-1).
Using this loop invariant, prove the correct of algorithm MaxValue. Follow the standard three
steps...
Initialization: Prior to the first iteration, where i = 2, show that the invariant holds.
Maintenance: Based on the initialization, the loop invariant is known to holds prior to
the some i-th iteration. Show that the loop invariant will also hold prior to the i+1-the
iteration.
Termination: Show that after the Nth (last) iteration (prior to an imaginary N+1st
iteration), the loop invariant still holds. Argue that this state means that the solution to
the problem (the largest value in the array) has been found.
Transcribed Image Text:Problem 1: Given is the following algorithm to determine the maximal value of an array A of size N. MaxValue (A[1..N]): max = A[1] i = 2 for (i < N) : { if A[i]> max: max = A[i] i=i+1 }| return max A loop invariant is stated as: Variable max holds the maximal value of all values in A[1..i-1] (that is: values at indices 1 to i-1). Using this loop invariant, prove the correct of algorithm MaxValue. Follow the standard three steps... Initialization: Prior to the first iteration, where i = 2, show that the invariant holds. Maintenance: Based on the initialization, the loop invariant is known to holds prior to the some i-th iteration. Show that the loop invariant will also hold prior to the i+1-the iteration. Termination: Show that after the Nth (last) iteration (prior to an imaginary N+1st iteration), the loop invariant still holds. Argue that this state means that the solution to the problem (the largest value in the array) has been found.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps

Blurred answer