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] |
|
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] |
|
E. |
none of these are correct |
A. |
Q: j = a[1] + … + a[i – 1] |
Get Answers For Free
Most questions answered within 1 hours.