Question 7 Question 1g Consider the following STRANGE ALGORITHM: Algorithm 1 STRANGE ALGORITHM Require: Integer n 1: S+0
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Question 7 Question 1g Consider the following STRANGE ALGORITHM: Algorithm 1 STRANGE ALGORITHM Require: Integer n 1: S+0
Question 7 Question 1g Consider the following STRANGE ALGORITHM: Algorithm 1 STRANGE ALGORITHM Require: Integer n 1: S+0 2: for i← 1 ... n do 3: if i is odd then S+S-i 4: 5: else 6: S+S+i 7: end if 8: end for 9: return S Which of the following loop invariants is correct? At the beginning of iteration; (i.e., after; is updated in Line 2 and before the code in Line 3 is executed) the following property holds: OS-(-1)(1/2) if ; is odd and S-(-1)(i-1)/2 if ; is even OS=-(i+1)/2 if; is odd and S = 1/2 if; is even S-(-1)+³((i-1)/2) if; is odd and S-(-1)+¹(1/2) if ; is even OS-1/2-0.5 ifi is odd and S= -1/2 if ; is even