Inactive Tutor answered 06/26/20
7/2 = 3 (floor division), so for iterations 1 and 2, the variable ans is decremented, resulting in a value of -2. For iterations 3 through 6, ans is incremented, resulting in a value of -2 + 4 = 2.
Manjunath H.
asked 06/26/20int f (int n) {
int ans = 0;
for (int i = 1; i < n; i++) {
if (i < n/2) {
ans -= i;
}
else {
ans += i;
}
}
return ans;
}
Inactive Tutor answered 06/26/20
7/2 = 3 (floor division), so for iterations 1 and 2, the variable ans is decremented, resulting in a value of -2. For iterations 3 through 6, ans is incremented, resulting in a value of -2 + 4 = 2.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Inactive Tutor
Corrections: for iterations 1 and 2 the ans variable will be decremented to -3. Then for the following iterations: Iteration 3: ans = -3 + 3 = 0 Iteration 4: ans = 0 + 4 = 4 Iteration 5: ans = 4 + 5 = 9 Iteration 4: ans = 9 + 6 = 1506/28/20