Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the following code. int f(int x, int y) { int r = 1; while (y > 1) { if (y % 2 ==

Consider the following code. int f(int x, int y) { int r = 1; while (y > 1) { if (y % 2 == 1) { r = x * r; 1 4. Formally prove that the loop invariant in part (b) (that involves x and y) is true before and after each

Consider the following code. int f(int x, int y) { int r = 1; while (y > 1) { if (y % 2 == 1) { r = x * r; 1 2 3 4 5 6 7 8 9 } x = x= x; y = y / 2; } 10 return rx; 11 } 1. Complete the following table for iterations 0,1,2,3, 4 (iteration 0 means outside of the loop) iteration xy loop invariant 0 1 2 4 2. Find a loop invariant that involves r? 3. Find a loop invariant that involves x and y? 4. Formally prove that the loop invariant in part (b) (that involves x and y) is true before and after each execution of the loop. That is, you need to prove that the loop invariant holds true initially, then at the end of each iteration. 5. This function returns r r. What does it computes in terms of the inputs x and y?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

1 Initially x 3 r 1 y 4 first iteration y 2 1 is false So r remains sam... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Computer Network questions

Question

What applied experiences do you have? (For Applied Programs Only)

Answered: 1 week ago