Using rules for hoare logic show correctness of the program

Assignment Help Mathematics
Reference no: EM131836349

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: EM131836349

Questions Cloud

Why is it difficult to generate revenue through advertising : On the social networks, why is it difficult to generate revenue through advertising ?
Find the capacitance of capacitor : If the charge carried by the capacitor is Q, neglecting the edge effects (fringing field), find the electric field as a function of radial distance r.
Small business owner visits his bank to ask for loan : A small business owner visits his bank to ask for a loan. how much would it be willing to lend the business owner?
What is a crumple zone : What is a "crumple zone"? What is their purpose, and why do they increase safety during a crash? Refer to specific physics concepts.
Using rules for hoare logic show correctness of the program : CMPT 340-T1 - Using our rules for Hoare logic, show the correctness of the following program that computes the index of the largest element in an array
Identify the relevant elements to include in the code : What ongoing processes and leadership actions are important to ensure long-term awareness of and compliance with the code of conduct?
Ball and hearing the crack of the bat : You may neglect the small amount of time it takes for the light to travel the 500 ft.
How the event impacted the economy in general : How did it influence the workplace? How did it influence or impact the workforce (employees/ people doing the jobs at that time)?
Magnitude of the net electrostatic force : Find the magnitude of the net electrostatic force exerted on the point charge q2. Let q1= +26.0 µC, q2= -28.0 µC, q3= +43.0 µC.

Reviews

len1836349

1/29/2018 3:43:15 AM

Submission Instructions 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

Mathematics Questions & Answers

  Questions on ferris wheel

Prepare a Flexible Budget Gator Divers is a company that provides diving services such as underwater ship repairs to clients in the Tampa Bay area.

  Logistic map

This assignment has two question related to maths. Questions are related to bifurcation cascade and logistic map.

  Finding the probability of cards

This assignment has questions related to probabiltiy.

  Systems of ode

Find all the xed points, and study their stability and Draw the phase portrait of the system, as well as the graphs of the solutions in all relevant cases.

  Derive the boolean expression

Derive the Boolean Expression and construct the switching circuit for the truth table stated

  System of equations

Evaluate which equations are under-identified, just-identified, and over-identified.

  Linear programming problem

Linear programming problem consisting of only two constraints with one objective function.

  Find the natural domain

Find the natural domain of the given functions.

  Introduction to numerical methods

Compute the coecients of the polynomials using the term recurrence relation.

  Chart of the topological manifold

De?nition of smoothness of functions on a smooth manifold is chart independent and hence geometric.

  Mathematics in computing

Questions related on mathematics in computing.

  Complex problems

Complex problems

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