Question

For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing...

For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing correctness for the
main loop in each of the following programs and briefly argue initialization, preservation, and termination.

EXAMPLE PROBLEM:

//Function to return the max of an array A
Maximum(array of integers A)
Local integer
integer m
m=0
for i = 1 to n
    if A[i] > m
        then m = A[i]
end function Maximum

EXAMPLE SOLUTION:  

The loop invariant is m = max(0, max_{1 \leq j \leq i-1} A[j]).

INITIALIZATION: at loop body entry, i=1 and m = 0, 
  so m=max(0, max_{1 \leq j \leq i-1}(A[j])=max(0, max_{1 \leq j \leq 0}A[j]).

PRESERVATION: if m=max(0, max_{1 \leq j \leq i-1} A[j]), then we have two cases: 

(1) If condition A[i] > m is true, then assignment m=A[i] makes m=max(0, max_{1 \leq j \leq i} A[j]),

(2) Otherwise if condition A[i] > m is false, then m is not assigned a new value and so remains unchanged, yet m=max(0, max_{1 \leq j \leq i} A[j]) since A[i] <= m.
In other words, at iteration i A[i] gets included in the calculation of maximum m.
Afterwards, incrementing i by 1 in the next iteration restores the invariant.

TERMINATION: when the loop terminates, i==n+1, so the invariant
implies m=max(0, max_{1 \leq j \leq n}A[j]) as desired.

======================================================================

E1.
//Function to return the value of x^2 for x>=1
Square(positive integer x)
Local integers
integers i,j
i=1
j=1
while i \noteq x do
    j=j + 2i + 1
    i=i + 1
end while
return j
end function Square

Homework Answers

Answer #1

Loop invariant is an indermediate value that is true according to the given condition.

So lets see, what is invariant for above problem.

j=1,i=1

x=4

While (i! =x)

LOOP 1, while condituon is TRUE

j= 1+ 2*1+1=4,,//j is the invariant here

i=1+1=2

LOOP 2 while condition is TRUE

j=4+2*2+1=9

i=2+1=3

LOOP 3 while condition is TRUE

j= 9+ 2*3 +1 =16

i=4

LOOP 4, condition fails.

Now, you can se, the condition inside while fails, that is i==x, 4==4, so it comes out of the loop

Also, j=16, that is equal to x^2, that means invariant for this problem holds true as per the function specification.

Hope i helped you.

Know the answer?
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
Similar Questions
Assume that the inner loop works correctly. Using a loop-invariant proof for the outer loop, formally...
Assume that the inner loop works correctly. Using a loop-invariant proof for the outer loop, formally prove that your pseudo-code correctly sorts the given array. Be sure that your loop invariant and proof cover the initialization, maintenance, and termination conditions Given code: /** * a[0:n-1] is an array of n elements. * temp is a variable to facilitate exchange. */ BubbleSort(a, n) Begin for k = 1 to n-1 increment by 1 do // where, k is for the pass...
Multiple Choice: The following function is intended to return the value of a[1] + a[2] +...
Multiple Choice: The following function is intended to return the value of a[1] + a[2] + … + a[n] for n ≥ 1. (The sum of the first n entries in an array of integers). Prove that the function is correct, or explain why it does not produce correct results. ArraySumA(integers n, a[1], a[2], … , a[n]) Local variables: integers i, j     i = 0     j = 0     while i ≤ n do         i = i +...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Find the exact number of times (in terms of n) the innermost statement (X = X...
Find the exact number of times (in terms of n) the innermost statement (X = X + 1) is executed in the following code. That is, find the final value of X. Then express the total running time in terms of O( ), Ω( ), or Θ( ) as appropriate. X = 0; for k = 1 to n     for j = 1 to n – k         X = X + 1; The following program computes and returns...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100...
(a) Write an algorithm (use pseudo-code) to determine whether a function f ∶ Z100 → Z100 is surjective. That is, supply a “Method” to go with Input: A function (array) f with f(i) ∈ Z100 for i = 0, 1, . . . , 99. Output: Boolean B. B=‘true’ if f is surjective, ‘false’ otherwise. Try to make your algorithm as efficient as possible. Do NOT include an implementation of your algorithm in a programming language. (b) How many comparisons...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar...
Please post all code in Pseudo code. Please post ORIGINAL answers do not copy from similar questions. Please post in a format that can be directly copied. Reasoning on answers would be most helpful but not required. Thank you in advance for your help. 1.Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2,...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A):...
Given the following algorithm, matching each statement to the correct sequence for complexity analysis. procedure Alg3(A): A is a list of n integers 1 for i = 1 to n-1 do 2   x=aix=ai 3 j = i − 1 4 while (j ≥≥ 0) do 5 if x≥ajx≥aj then 6 break 7 end if 8   aj+1=ajaj+1=aj 9 j = j − 1 a end while b   aj+1=xaj+1=x c end for d return A The complexity of this algorithm is O(n2)O(n2)...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...