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

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

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