Manjunath H.

asked • 06/26/20

For the following code What does f(7) evaluate to?

int f (int n) {

int ans = 0;

for (int i = 1; i < n; i++) {

if (i < n/2) {

ans -= i;

}

else {

ans += i;

}

}

return ans;

}

1 Expert Answer

By:

Nitin P. answered • 06/26/20

Tutor
4.9 (134)

Machine Learning Engineer - UC Berkeley CS+Math Grad

Mulugeta E.

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 = 15
Report

06/28/20

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.