Explain please: Let A[1...n] be an array with n elements. Consider the prefix-av
ID: 3722442 • Letter: E
Question
Explain please: Let A[1...n] be an array with n elements. Consider the prefix-average algorithm. See image:
Question 3 10 pts Let A[1..n] be an array with n elements. Consider the Prefix-Average algorithm, with the pseudocode below. Prefix-Average(A,n) 1.8=0 2. let B[1...n] be a new array 3, for j = 1 to n 4. 5. print s Which of the following is the correct loop invariant for the for loop? O At the start of each iteration j of the for loop, s represents the average of the elements in the prefix subarray A[0..j]. O At the start of each iteration j of the for loop, s represents the average of the elements in the prefix subarray 1.jj. O At the start of each iteration j of the for loop, s represents the average of the elements in the prefix subarray A[1.j+1]. At the start of each iteration j of the for loop, s represents the average of the elements in the prefix subarray A[1.j-1]. OExplanation / Answer
If you have any problems with the answer or want me to edit the answer, just let me know in the comments and I will try to get on to it as soon as possible. Do give a positive rating if you think this answer helped.
The starting index of our array is 1 and prefix average calculates average all the previous values. Hence the value of s should be till (j-1) as A[j] is the last value which is added in prefix average to calculate new average. Therefore correct option is d. S represents the average of elements in prefix subarray A(1…..j-1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.