Prove the correctness of the given program

Assignment Help JAVA Programming
Reference no: EM131836294

Part -1:

Using our rules for Hoare logic, prove the correctness of the following program that computes the integer nth power of integer x. Although you might want to figure out the proof using tree layout, give your solution in linear form similar to the proofs of division given in the textbook.

We give the initial assumption

{n ≥ 0}

and final goal

{ y = xn }

in the program below:

// Slow Exponentiation

{ n ≥ 0 }
y := 1
k := n

while (k > 0) do

y := y ×

k := k - 1

done
{ y = xn }

{y = xn }

Bonus points Why is this called slow exponentiation? To get one bonus point, give code that is exponentially faster in n, and to get the second bonus point, argue convincingly that your speedup is exponential.

Part -2:

Using our rules for Hoare logic, show the correctness of the following program that computes the index of the largest element in an array of integers, A; again present your proof in linear form. Assume that arrays are indexed from 1 to n and n > 0, as in the first tutorial.

// Find Maximum
{ length(A) = n ∧ n > 0 }

maxI .= 1

for j from 2 upto n do

if A [j ]>A [maxI ]

then maxI .= j

else skip

done

{ ∀j ∈[1,n] A[maxI] ≥ A[j] }

Programming Part - 1

P1 Alice believes that the following Java function correctly copies a range of elements in an array to a new location (overwriting what is there).

static void rangeCopy (int A [],
int from , int endWith , int to ) {
for (; from <=endWith ; from +=1, to +=1) {
A [to ] = A [from ];
}
}
So, if
A = [ 0, 1, 2, 3, 4]
initially, then rangeCopy (A, 0, 1, 3) would change A to become
A = [ 0, 1, 2, 0, 1]
This is useful in many places: from copying ranges of cells in Excel to the
memcpy(1) system-library function.

Although the function works for the above example, there are a number of situations where it will do the wrong thing. Identify as many distinct ones of these that you can, explaining the problem and giving a counter-example where the function does the wrong thing. One important case will require you to reason about some kind of separation property. Finally, give a correct Java function, embedded in a class that tests for the various problematic cases you've identified.

Reference no: EM131836294

Questions Cloud

Making a function h : Maybe making a function h = f - g and using that somehow?
Explain the disadvantages of using the payback method : Explain the disadvantages of using the payback method. Compare and contrast the internal rate of return (IRR) method from the net present value method (NPV).
Determine the relationship of the first set : Use a Venn diagram to determine the relationship of the first set to the second no matter the choice of sets A and B.
How the client behaviors impacted the test results : Analyze the data in your client's case history. You will be addressing how the client's behaviors and test conditions impacted the test results.
Prove the correctness of the given program : CMPT 340-T1 - Why is this called slow exponentiation? To get one bonus point, give code that is exponentially faster in n, and to get the second bonus point
State the midpoint of the line segment : a.) state the midpoint of the line segment with the given endpoints.
State the domain of the function : f(x)=x^2-16/v2x-15 (square root extends to 2x-15) a. Calculate f(3) b. State the domain of the function f(x)=x^2-16/v2x-15
How would assess the current model of shared responsibility : How would you assess the current model of shared responsibility? Is it sufficient to adequately address future challenges?
State the domain of the function : f(x)=x^2-16/v2x-15 (square root extends to 2x-15) a. Calculate f(3) b. State the domain of the function f(x)=x^2-16/v2x-15

Reviews

len1836294

1/29/2018 2:58:53 AM

Submit your answers as two files: 1.a PDF file abc123-a1.pdf containing your written answers to the two written questions and any written details you want to submit for the programming question (e.g. explanations of the problematic cases, a separation logic proof of your new function). 2.a Java source file abc123-a1.java containing your code for the replacement function and your test cases. Note that abc123 must be replaced with your NSID and that each file must contain an identity block giving • your name • your NSID • your student number • this course and assignment number

Write a Review

JAVA Programming Questions & Answers

  Write a driver program that reads in animals of type horse

Write a driver program that reads in 3 animals of type Horse and prints out the name, age and height of all non pure blood Horse objects that are 4 or more years old. The following information should be read per Horse:

  Java servlet uses doget to return markup document

Write down Java servlet which uses doGet to return markup document which provides your name, e-mail address, and mailing address along with a brief autobiography.

  Write an application calccircumference

Write an application (CalcCircumference) that inputs from the user the radius of a circle as an integer and prints the circle's diameter, circumference and area using the floating-point value 3.14159 for ?.

  Reads contents of two vectors

Write a program that reads contents of two vectors, and then displays the sum of these two vectors. The program should prompt the user to enter the size of the vectors first.

  Write a java program to prompts a user to enter information

Write a Java program prompts a user to enter demographic information

  Write a program that displays a frame window w pixels wide

Write a program that displays a frame window W pixels wide and H pixels high. Use the Scanner to enter the values for W and H. The title of the frame is also entered by the user.

  Write a jsp program that generates subtraction quiz randomly

Write a JSP program that generates subtraction quizzes randomly, as shown in Figure 43.14a (http://postimg.org/image/ze4uwdhqp/) . The first number must always be greater than or equal to the second number.

  Finally make a java test class in your test class you must

finally create a java test class. in your test class you should at a minimum a construct 200 instances of each subclass

  Program that counts the number of occurrences of lowercase

Write a program that counts the number of occurrences of lowercase and uppercase vowels in entered lines of text. Use a two-dimensional array to store the vowel counts. The array's first column holds the counts for the lowercase vowels, and the secon..

  Create a method named justsold that increments the hotdogs

Finally, add a static variable that tracks the total number of hotdogs sold by all hot dog stands and a static method that returns the value in this variable.

  Create a java program that contains a class named rectangle

Create a Java program that contains a class named Rectangle. The Rectangle class should contain two attributes to store the length and breadth of a rectangle.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd