Question 1g Consider the following STRANGE ALGORITHM: Algorithm 1 STRANGE ALGORITHM Require: Integer n 1: S-0 2: for i+1
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Question 1g Consider the following STRANGE ALGORITHM: Algorithm 1 STRANGE ALGORITHM Require: Integer n 1: S-0 2: for i+1
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 iis 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) ifi is odd and S-(-1)(-1)/2 if is even OS--(+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 S-1/2-0.5 ifi is odd and S--1/2 ifiis even