Loop invariant mistake -
this old midterm. looking on trying study final.
fun(int n, int a[]){ for(i = 0;i < a.length; += 2){ a[i] = n; } return; }
it asks loop invariant, @ location after loop begins, before assignment of a[i]. asks invariant , exit conditions imply loop achieves when exits.
i answered:
loop invariant < a.length
exit conditions >= a.length
this i+=2 implies array has entries equal n on every number n entry less or equal a.length
i not awarded full credit, , think may due loop invariant. can clarify?
sorry don't have enough reputation comment, write answer.
i think
loop invariant < a.length
should be
loop invariant < a.length , i%2=0
and for
this i+=2 implies array has entries equal n on every number n entry less or equal a.length
maybe lecture want it's even-indexed elements(the 1st,3rd... or a[0],a[2]...) , wouldn't necessary
less or equal a.length
Comments
Post a Comment