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

  Calculate the mean, median, mode of the measurements

Calculate the mean, median, and mode of the measurements taken in Module 1 SLP. Be sure to express each value of central tendency in units.

  Determine the area burned by the fire

Calculate the indicated areas. All data are accurate to at least two significant digits.

  Find the numbers of indistinguishable configurations

Suppose n batons with small holes drilled through their midpoints are strung along a piece of wire and each end of each baton is painted.

  Example of a geometric sequence

A rabbit population grew in the following pattern: 2, 4, 8, 16... If all of the rabbits live and continue in the pattern, how many rabbits will be in the 8th generation?   (32, 128, 256, or 512?) This is an example of a geometric sequence.

  What postion is the weapon most dangerous

Suppose you have to fight a zombie. If you swing your weapon with enough velocity you win the fight - what postion is the weapon most dangerous

  What sample size would the economist need

What sample size would the economist need to use if he wants a 95% confidence interval no wider than plus or minus $100?

  Sketch graph of distance from kalamazoo as function of time

You drive at a constant speed from Chicago to Detroit, a distance of 275 miles. About 120 miles from Chicago you pass through Kalamazoo, Michigan.

  Find the probability of x successes given the probability

Assume that a procedure yields a binomial distribution with a trial repeated n times. Use the binomial probability formula to find the probability of x successes given the probability p of success on a single trial. Round to three decimal places.

  Major repair during its first year of purchase

a. What is the probability that a randomly selected car in the city needs a major repair during its first year of purchase?

  What percent of george''s old salary is the $4,600 raise

What percent of George's old salary is the $4,600 raise?

  What are the dimensions of the garden

a rectangular garden has a perimeter of 184 feet. the length of the garden is 22 feet more then the width. what are the dimensions of the garden?

  Multivariable calculus

Determine dz/dt when z=xln(x+10y) x=sint    y=cost  multivariable calculus

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