Question

In this problem, we are given a sequence a1, a2,... , an of integers and we want to return a list o terms in the sequence that are greater than the sum of all previous terms of the sequence. For example, on an input sequence of 1,4, 6,3,2,20, the output should be the list 1,4,6, 20. The following algorithm solves this problem procedure PartialSums(a1, a2, . . . , an: a sequence of integers with n 〉 1) total : 0 Initialize an empty listL for i := 1 to n 2 4 if ai 〉 total Append aj to list L total := total + ai 7. return D

(a) Prove the following loop invariant by induction on the number of loop iterations: Loop Invariant: After the kth iteration of the for loop, total = a1 + a2 + · · · + ak and L contains all elements from a1 , a2 , . . . , ak that are greater than the sum of all previous terms of the sequence.

(b) Use the loop invariant to prove that the algorithm is correct, i.e., that it returns a list

of all terms in the sequence that are greater than the sum of all previous terms of the sequence.

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
(a) Prove the following loop invariant by induction on the number of loop iterations: Loop Invariant:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

  • Use a loop invariant to prove that the following algorithm correctly identifies the location of the...

    Use a loop invariant to prove that the following algorithm correctly identifies the location of the minimum value in the array data. Input: data: array of integers Input: n: size of data Output: index min such that data[min] <= data[i] for any i from 1 to n Algorithm: FindMin min = 1; for i = 2 to n do if data[i] < data[min] then min = i; end end return min

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • 1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci...

    1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N,...

  • PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP...

    PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP AND FORMAT: #include <iostream> using namespace std; int main() { //variables here    do { // program here             }while (true);    } Objectives To learn to code, compile and run a program containing SELECTION structures Assignment Write an interactive C++.program to have a user input the length of three sides of a triangle. Allow the user to enter the sides...

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • X G Answer The Following Muli m Belgium 3-1 Russia: Eden x ■ CBU I Cape Breton Univers resource%2...

    x G Answer The Following Muli m Belgium 3-1 Russia: Eden x ■ CBU I Cape Breton Univers resource%2Fcontent%2F1%2Fassig6,MATH 1203-W201 9pdf Due date: Thurs., Mar. 28 Please get an early start on this assignmext. If you have problens then come and see me to get help. Also remember, there are far more marks for procedure than for final answers. Write your solutions neatly and in a then come and see me to get help well organized fashion. Show all work....

  • Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops....

    Assignment 6: Recursion Learning Outcomes • Learn how to craft solutions using recursion instead of loops. Instructions This assignment will be different than previous assignments (and most assignments which come after it). In this assignment, you will be crafting four solutions to four different problems. This assignment will also have special requirements regarding how you may code. You are not allowed to use assignment statements. This includes using preincement, postincrement, predecrement, and postdecrement. You are allowed to use assignment to...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

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