expand icon
book ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff cover

ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff

Edition 2ISBN: 978-0131409095
book ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff cover

ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff

Edition 2ISBN: 978-0131409095
Exercise 1
Consider the following algorithm with precondition Pre , intermediate assertion A , and postcondition Post :
ScanAndCount
/ * Algorithm to find and display a count of the elements of an array x [0], …, x [ n ? 1] that exceed a given value cutoff. * /
Pre : Input consists of an integer n ? 1, an array x storing n real numbers, and a real number cutoff.
1. Initialize count = 0.
2. Initialize i = 0.
3. While i n do the following:
a. Increment i by 1.
b. If x [ i ] cutoff
Add 1 to count.
A : The value of i is the number of times execution has reached this point, and count is the number of array elements among x [0],… x [ i ?1] that are greater than cutoff.
End while.
4. Display count.
Post : The algorithm terminates, and when it does, the value of the variable count is the number of elements in array x that are greater than cutoff.
Using the given precondition, intermediate assertions, and postcondition, prove the correctness of this algorithm. Note that on each pass through the loop execution may follow either of two possible paths.
Explanation
Verified
like image
like image

To prove:
When the algorithm terminates...

close menu
ADTs, Data Structures, and Problem Solving with C++ 2nd Edition by Larry Nyhoff
cross icon