Question

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 + 1
        j = j + a[i]
    end while
    // j now has the value of a[1] + a[2] + … + a[n]
    return j
end function ArraySumA

A.

Q: j = a[1] + … + a[i – 1]
Q(0): j0 = a[1] + … + a[i0 – 1]
            true since j = 0, i = 1 before the loop is entered, so the equation becomes
            0 = a[1-1] which is 0 = a[0] and there is no a[0] term so the value is vacuously 0.
Q(k): jk = a[1] + … + a[ik – 1]
Q(k + 1): jk+1 = a[1] + … + a[ik+1 – 1]
            note: jk+1 = jk + a[ik] and ik+1 = ik + 1
            jk+1                                                    left side of Q(k + 1)
            jk + a[ik]                                            assignment rule
            a[1] + … + a[ik – 1] + a[ik]               inductive hyp
            ik+1 = ik + 1 so ik = ik+1 - 1               rewrite
            a[1] + … + a[ik – 1] + a[ik+1 - 1]       by substitution
This is the right side of Q(k + 1) so Q is our loop invariant
At loop termination j = a[1] + … + a[i- 1] and i = n + 1, so j = a[1] + … + a[n]

B.

begins the sum with a[0]

C.

makes one too many passes through the loop and adds a[n+1] to the sum

D.

Q: j = a[1] + … + a[n – 1]
Q(0): j0 = a[1] + … + a[n – 1]
cannot be proven true for base case so the function is proven incorrect

E.

none of these are correct

Homework Answers

Answer #1
A.

Q: j = a[1] + … + a[i – 1]
Q(0): j0 = a[1] + … + a[i0 – 1]
            true since j = 0, i = 1 before the loop is entered, so the equation becomes
            0 = a[1-1] which is 0 = a[0] and there is no a[0] term so the value is vacuously 0.
Q(k): jk = a[1] + … + a[ik – 1]
Q(k + 1): jk+1 = a[1] + … + a[ik+1 – 1]
            note: jk+1 = jk + a[ik] and ik+1 = ik + 1
            jk+1                                                    left side of Q(k + 1)
            jk + a[ik]                                            assignment rule
            a[1] + … + a[ik – 1] + a[ik]               inductive hyp
            ik+1 = ik + 1 so ik = ik+1 - 1               rewrite
            a[1] + … + a[ik – 1] + a[ik+1 - 1]       by substitution
This is the right side of Q(k + 1) so Q is our loop invariant
At loop termination j = a[1] + … + a[i - 1] and i = n + 1, so j = a[1] + … + a[n]

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
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,...
*********I need question 6 answered which is from question 5 which is ********* Question 5 :...
*********I need question 6 answered which is from question 5 which is ********* Question 5 : Program Correctness I (1 point) Use the loop invariant (I) to show that the code below correctly computes n P−1 k=0 2k (this sum represents the sum of the first n even integers where n ≥ 1). Algorithm 1 evenSum(int n) 1: p = 2(n − 1) 2: i = n − 1 3: while i > 0 do 4: //(I) p = nP−1...
Prove n+1 < n for n>0 Assume for a value k; K+1< K We now do...
Prove n+1 < n for n>0 Assume for a value k; K+1< K We now do the inductive hypothesis, by adding 1 to each side K+1+1 < k+1 => K+2< k+1 Thus we show that for all consecutive integers k; k+1> k Where did we go wrong?
1: A) Given the following vectorized code: >>x=[1:10]; >>f=x.^2+2; Rewrite this code using a for loop...
1: A) Given the following vectorized code: >>x=[1:10]; >>f=x.^2+2; Rewrite this code using a for loop that defines each element of f one at a time. Make sure to preallocate memory to f before filling each spot. B) See the following code. Rewrite the code in one line using the find function and a For loop. then write it again using a while loop x=[-1 3 7 2 4 0]; v=[]; for i=1:length(x) if x(i)<=2 v=[v, x(i)]; end end please...
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)...
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...
Part 1: Implement findknn Implement the function findknn, which should find the ?k nearest neighbors of...
Part 1: Implement findknn Implement the function findknn, which should find the ?k nearest neighbors of a set of vectors within a given training data set. The call of: [I,D]=findknn(xTr,xTe,k); should result in two matrices ?I and ?D, both of dimensions ?×?k×n, where ?n is the number of input vectors in xTe. The matrix ?(?,?)I(i,j) is the index of the ??ℎith nearest neighbor of the vector ???(?,:)xTe(j,:). So, for example, if we set i=I(1,3), then xTr(i,:) is the first nearest...
The following algorithm finds the initial substring of y that can be reversed and found in...
The following algorithm finds the initial substring of y that can be reversed and found in y. For example, longestInitialReverseSubstringLength(“aabaa”) = 5, because “aabaa” is the same string forwards and backwards, so the longest initial substring that can be reversed and found in the string is “aabaa”. Also, longestInitialReverseSubstringLength(“bbbbababbabbbbb”) is 6, because “babbbb” can be found in the string (see color-highlighted portions of the string), but no longer initial string exists reversed in any part of the string. longestInitialReverseSubstringLength(String y)...
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid...
IN C++ VERY EASY What's wrong with this code? The following function prints a reverse half-pyramid populated by the alternating dots and stars (see example below). The odd rows contain stars and even rows contain dots. Debug the code to fix all the compilation and run-time errors, so that the code generates the desired output. For instance, when the 'n' value passed to the function is 6, the output would look like the following. ****** ..... **** ... ** ....
You’re the grader. To each “Proof”, assign one of the following grades: • A (correct), if...
You’re the grader. To each “Proof”, assign one of the following grades: • A (correct), if the claim and proof are correct, even if the proof is not the simplest, or the proof you would have given. • C (partially correct), if the claim is correct and the proof is largely a correct claim, but contains one or two incorrect statements or justications. • F (failure), if the claim is incorrect, the main idea of the proof is incorrect, or...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT