Question

17. hy the correctness of the following program segment with the precondition and postcondition shown {x60 if x > 0 then y =

0 0
Add a comment Improve this question Transcribed image text
Answer #1

17.

From the question:

if x not equal to 0

Precoditions:

(1)

if x>0

(2)

if x<0 (since x not equal ot zero dont take x as 0)

Post condition:

{y>0}

case1:

if x>0 then

let x = 1

y= 2*x = 2*1 = 2

since 2>0 the post condition y>0 is true.

case2:

else // it indicatesif x<0

let x = -3

y= (-2)*x = 2*-(-3) = 6

since 6>0 the post condition y>0 is true.

Hence proved

18.

From the question:

if x not equal to 0

Precoditions:

(1)

if x>0 (since x not equal ot zero dont take x as 0)

(2)

if x<0

Post condition:

result is |x|

case1:

if x>0 then

let x=1

abs = x = 1

since x = abs = |x| = 1 the post condition |x| is true.

case2:

else // it indicatesif x<0

let x=-1

abs = -x = -(-1) = 1

since x = -1 and abs = |x| = 1 the post condition |x| is true.

Hence proved

Any doubts leave a comment. Please Rate My Answer

Add a comment
Know the answer?
Add Answer to:
17. hy the correctness of the following program segment with the precondition and postcondition shown {x60...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. (15 marks) Consider the following program: >> Precondition: x and y E Z. Postcondition: Return...

    2. (15 marks) Consider the following program: >> Precondition: x and y E Z. Postcondition: Return the sum x + y. add(x, y): 1. if x == 0: 2. return y 3. elif x > 0: 4. return add(x - 1, y) + 1 5. else: 6. return add(x + 1, y) - 1 Prove that this program is correct in terms of its specification.

  • 16. Verify the correctness of the following program segment with the assertions shown. x=z+1 y=x+2 y-6) if y >0 then...

    16. Verify the correctness of the following program segment with the assertions shown. x=z+1 y=x+2 y-6) if y >0 then z=y' + 1 else z=2*y end if (z = 7} 16. Verify the correctness of the following program segment with the assertions shown. x=z+1 y=x+2 y-6) if y >0 then z=y' + 1 else z=2*y end if (z = 7}

  • Problem 4 (11 pts, 1 pt. for each condition): Forward reasoning Find the strongest postcondition of...

    Problem 4 (11 pts, 1 pt. for each condition): Forward reasoning Find the strongest postcondition of each code sequence by inserting the appropriate condition in each blank. The first condition in part (a) is supplied as an example Please simplify your answers as much as possible. Assume all variables are ints Copy all code to your answer file and fill in the blanks. Carry all variables forward. Show all work. fx> 0 ) x10 10 y 20 -xi x11 )...

  • (b) Trace the following code. // Precondition: n > 0 // <method description here> public static...

    (b) Trace the following code. // Precondition: n > 0 // <method description here> public static int dothing (int n) { int ans = ; for (int x 1; x <= n; x++) ( for (int y = x; y <= n; y++) ( ans+ } return ans; } (ii) If you were asked to write a one-line description above this method, what would it be? (iii) Could you rewrite the method so that it runs more efficiently?

  • Consider the vectors shown. Which is a correct statement about A - B? 1) Consider the...

    Consider the vectors shown. Which is a correct statement about A - B? 1) Consider the vectors shown. Which is a correct statement about A- B? у В (15.0 m) 30.0% D(10.0 m) A. x-component > 0, y-component > 0 53.00 B.x-component > 0, y-component < 0 C. x-component<0, y-component > 0 x о 25.00 D. x-component < 0, y-component < 0 C(12.0 m) À (8.00 m) E. x-component = 0, y-component > 0

  • 2 BALL AND PLANE Consider a spherical shell of radius R and charge per unit area...

    2 BALL AND PLANE Consider a spherical shell of radius R and charge per unit area σ1 sitting at the origin. There is also an infinie plane parallel to the x- y plane sitting at z-zo with charge per unit area Oz. We will take Zo > R. Compute the electric field at the following locations: 2.1 10 POINTS The origin. 2.2 15 POINTS The point (xo,0,0) with xo> R 2.3 15 POINTS The point (X1, 0,21) with 0 <...

  • Consider the following code: int w- 10; while (w>0) scanf("%d", &y); if(y>0){ V++i else The value...

    Consider the following code: int w- 10; while (w>0) scanf("%d", &y); if(y>0){ V++i else The value stored in variable v at the end of the execution of the loop could best be described as the sum of all positive numbers scanned from the terminal the number of times the loop executed the sum of all non-positive numbers scanned from the terminal the number of positive integers scanned from the terminal the number of negative integers scanned from the terminal

  • A weird vector space. Consider the set R+ = {x ER : x > 0} =...

    A weird vector space. Consider the set R+ = {x ER : x > 0} = V. We define addition by x y = xy, the product of x and y. We use the field F = R, and define multiplication by co x = xº. Prove that (V, O, RO) is a vector space.

  • 6. [8 marks Consider the sequence 10, 11, ... defined by To = 0, 11 =...

    6. [8 marks Consider the sequence 10, 11, ... defined by To = 0, 11 = 1, and ri = i-1 + 24-2 for all i > 2. Consider the following algorithm that attempts to compute the value In, given an n > 0. Algorithm 6 1: //pre: (n e Z) ^ (n > 0) 2: if n == 0 then 3: bro 1: else 5: a 0 6: 5+ 1 it1 //LoopInv: while i<n do ct a+b atb bc...

  • In each of the following questions, you are given an i.i.d. sample and two hypotheses. For...

    In each of the following questions, you are given an i.i.d. sample and two hypotheses. For any ?∈(0,1), define a test with asymptotic level ?, then give a formula for the asymptotic ?-value of your test. a) b) c) i.id. Xi, , xn Poiss (A) for some unknown λ > 0; V.S x) where Z ~ N (0, 1), and q(alpha) Type barx-n for Xn, lambda-0 for λο. . If applicable, type abs(x) for lxi. Phi(x) for Φ (x) =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT