Question

Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0...

Problem Description

proving program correctness

Consider the following program specification:

Input: An integer n > 0 and an array A[0..(n - 1)] of n integers.

Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)].

For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in A is 8 and the smallest index at which 8 appears is index 1.

Consider the following implementation:

indexOfMax(n, A)
(1) in - 2
(2) mA[n - 1]
(3) sn - 1
(4) while i ≥ 0
(5)      if A[i] ≥ m then
(6)           mA[i]
(7)           si
(8)      end
(9)      ii - 1
(10) end
(11) return s

In the implementation above, line numbers are given so you can refer to specific lines in your answers and ← is used to indicate assignment.

Part A

Use induction to establish the following loop invariant right before the while test in line (4) is executed:

i. -1 ≤ i < n - 1

ii. m = A[s]

iii. m = max A[(i + 1) .. (n - 1)]   (i.e., m is the maximum value in that appears in the array A between indices i + 1 and n - 1, inclusive)

iv. s is the smallest index at which max A[(i + 1) .. (n - 1)] appears

Hints and tips:

Use only one induction proof and prove each of the four parts of the invariant in your base case and inductive step.

You may assume that i and s will always be integers (i.e., you don't have to prove this).

Part B

Prove the correctness of the implementation by arguing partial correctness and termination.

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 10. (10 points) Computational problem solving: Proving correctness: Function g (n: nonnegative integer) if n si...

    10. (10 points) Computational problem solving: Proving correctness: Function g (n: nonnegative integer) if n si then return(n) else return(5*g(n-1) - 6*g(n-2)) Prove by induction that algorithm g is correct, if it is intended to compute the function 3"-2" for all n 20. Base Case Proof: Inductive Hypothesis: Inductive Step:

  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • Given an array of integer numbers, write a linear running time complexity program in Java to...

    Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...

  • Prove procedure to compute Fibinocci(n) where F0 = 0, F1 = 1, Fn = Fn-2 +...

    Prove procedure to compute Fibinocci(n) where F0 = 0, F1 = 1, Fn = Fn-2 + Fn-1. Prove by establishing and proving loop invariant then using induction to prove soundness and termination. 1: Procedure Fib(n) 2: i←0,j←1,k←1,m←n 3: while m ≥ 3 do 4:   m←m−3 5:   i←j+k 6:   j←i+k 7:   k←i+j 8: if m = 0 then 9: return i 10: else if m = 1 then 11: return j 12: else 13.   return k

  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

  • PLEASE USE INDUCTION, NO PROGRAMS ALLOWED Consider the following program, where a and n are positive...

    PLEASE USE INDUCTION, NO PROGRAMS ALLOWED Consider the following program, where a and n are positive integers. Input: a, n x = a; m = n; y = 1; while (m > 1) {if m is even x = x*x; m = m/2; if m is odd y = x*y; x = x*x; m = (m - 1)/2;} Output x*y Show by induction that the above loop has the following invariant: a^n = x^m times y. What does the above...

  • An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence...

    An array A[1,2,... ,n is unimodal if its consists of an increasing sequence followed by sequence a decreasing sequence. More precisely, there exists an index k є {1,2,… ,n} such that there exists an indes . AlE]< Ali1 for all 1 i< k, and Ai]Ali 1 for all k< i< n A1,2,..,n] in O(logn) time the loop invariant (s) that your algorithm maintains and show why they lead to the correctness Give an algorithm to compute the maximum element of...

  • For an integer n > 0, consider the positive integer F. = 22 +1. (a) Use...

    For an integer n > 0, consider the positive integer F. = 22 +1. (a) Use induction to prove that F. ends in digit 7 whenever n 2 is an integer (b) Use induction to prove that F= 2 + IT- Fholds for all neN. (c) Use (b) to prove that ged(F, F.) = 1 holds for all distinct nonnegative integers m, na (d) Use (e) to give a quick proof that there must be infinitely many primes! That is...

  • 1. (Induction.) Consider the following program, called Ackbar(m,n). It takes in as input any two natural numbers m, n,...

    1. (Induction.) Consider the following program, called Ackbar(m,n). It takes in as input any two natural numbers m, n, and does the following: (i) If m-0, Ackbar(0, n) = n + 1. (ii) If n-0, Ackbar(rn,0) is equal to Ackbar(m-1, 1). iii) Otherwise, if n, m > 0, then Ackbar(m, n) can be found by calculating Ackbar(m - 1, Ackbar(m,n 1)) Here's a handful of calculations to illustrate this definition: Ackbar(1,0)-Ackbar(0,1) = 1 + 1-2 Ackbar (1, 1) Ackbar (0,...

  • Please provide x86 (MASM not NASM) .386 (32bit) assembly program for the following (needs to be...

    Please provide x86 (MASM not NASM) .386 (32bit) assembly program for the following (needs to be compatible with Visual Studio 2015.asm file): (10 points) Write an assembly program to find the largest element by searching an array int aryll-[11, 15,-3,-4, 0,60.11,-9,18) int index-l; int max- 0; int arraySize-sizeof array /sizeof max while (index < arraySize) if (ary[index] > max) max = ary[index]; - Use cmp instruction and the appropriate jump instruction (signed or unsigned) to translate the if and while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT