Question

In: Computer Science

Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...

Consider the following program specification:

Input: An integer n > 0 and an array A[0..(n – 1)] of n integers.

Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)].

For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and the largest index at which 8 appears is index 4.

Consider the following implementation:

indexOfMax(n, A)
(1) i ← 1
(2) mA[0]
(3) b ← 0
(4) while i < n
(5)      if A[i] ≥ m then
(6)           mA[i]
(7)           bi
(8)      end
(9)      ii + 1
(10) end
(11) return b

In the implementation above, line numbers are given so you can refer to specific lines in your answers and ← is used to indicate assignment.

Part a (18 points)

Use induction to establish the following loop invariant right before the while test in line (4) is executed:

  1. 0< in
  2. m = A[b]
  3. m = max A[0 .. (i – 1)]   (i.e., m is the maximum value in that appears in the array A between indices 0 and i – 1, inclusive)
  4. b is the largest index at which max A[0 .. (i – 1)] appears

Hints and tips:

  • Use only one induction proof and prove each of the four parts of the invariant in your base case and inductive step.
  • Since the program has an if statement, your analysis will have to take into consideration the case that the if condition is true and the case that the if condition is false.
  • You may assume that i and b will always be integers (i.e., you don't have to prove this).

Part b (7 points)

Prove the correctness of the implementation by arguing

  1. partial correctness and
  2. termination.

Solutions

Expert Solution

Part A.

Here we can see that we have Array of Value and have to find max index of max value in Array.

Now, As program is is already implementated. We have to proof induction for this.

  1. 0< in------------------>> This is Where we will search for all elements
  2. m = A[b]-------------->> Once we find the max element store it in m
  3. m = max A[0 .. (i – 1)]   (i.e., m is the maximum value in that appears in the array A between indices 0 and i – 1, inclusive)------------------>>This max element will reside in index between 0 to i-1.
  4. b is the largest index at which max A[0 .. (i – 1)] appears----------------->>then finally max index will be in between 0 to i-1.

For, Prooving this let's assume.

Our First target will be to find out max element in array. And when we find out that this max element at b index then, we will search for 0 to b.

In fact, largest index will be at index b only.

Becouse of element we increase the index and element is find out by this then that index will be largest index.

However , one conflict my arrive at we have max element more then one value in array.

That time there is possiblity of have two index as result but we will choose bigger one in that.

So, we undestand that the comparion we are doing in if clause is most important.

Once 0 to n-1 loop end, always max element will be with largest index.

So, 0 to i-1 index max index will be i-1 as b.

Part B.

Here, Correctness of the implementation will be depands on data of input array.

If Array contains negative value then this program will not work correctly. For all postive integer it will work fine.

For Termination it will always terminate with finite value of n.


Related Solutions

Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Write a program to do the following. • Input an integer n. • Create a BST...
Write a program to do the following. • Input an integer n. • Create a BST S inserting the keys 1, 2, . . . , n in that order, which will result in a completely-skewed tree. • Measure the time to search for n + 1 in S. • Display the time taken for search. /** * Exception class for access in empty containers * such as stacks, queues, and priority queues. * @author Mark Allen Weiss */ public...
Write a program to do the following. • Input an integer n. • Create a BST...
Write a program to do the following. • Input an integer n. • Create a BST B containing the same items, but inserting them in an order that will yield the tree balanced. The following algorithm, known as pre-order traversal, gives you a strategy to produce that sequence. seq(low, high, T){    if (low <= high){        mid = (low+high)/2        T.insert(mid)        seq(low, mid-1, T)        seq(mid+1, high, T)    } } • Measure the...
Write a short main program that reads an integer n from standard input and prints (to...
Write a short main program that reads an integer n from standard input and prints (to standard output) n lines of * characters. The number of *’s per line should double each time, starting with 1. E.g., if n = 5, the output should be as follows: * ** **** ******** ****************
Write a program called x.c that reads an integer n from standard input, and prints an...
Write a program called x.c that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X". You can assume n is odd and >= 5. Solve this problem using only while loop. Solution: ./x Enter size: 5 *---* -*-*- --*-- -*-*- *---* ./x Enter size: 9 *-------* -*-----*- --*---*-- ---*-*--- ----*---- ---*-*--- --*---*-- -*-----*- *-------* ./x Enter size: 15 *-------------* -*-----------*- --*---------*-- ---*-------*--- ----*-----*---- -----*---*----- ------*-*------ -------*------- ------*-*------...
1. Write a Java program that asks the user to input a positive integer n first,...
1. Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array. 2 .Find the output of the following Java program and explain your answer
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds...
Program Specification Design an inventory class that stores the following members: serialNum: An integer that holds a part's serial number. manufactDate: A member that holds the date the part was manufactured. lotNum: An integer that holds the part's lot number. The class should have appropriate setter and getter functions. Next, design a stack class that can hold objects of the class described above. If you wish, you may use the linked list from program 5 as a basis for designing...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT