Question - compound operators

Hi Guys,

trying to figure out why the value of a is 26? Can anyone clarify this? I get 16 if I work it out manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
using namespace std;

int result(int valueP)
{
    const int BONUS = 14;
    int count = 6;
    int a = 5;
    while (count > valueP)
    {
        a += BONUS - a / 2;
        count -= 2;
    }
    return a;
}

int main()
{
  cout << result (1);
    return 0;
}
closed account (2LzbRXSz)
It repeats the process multiple times. If you put
cout << a << endl; in the while loop under a+= BONUS - a / 2; you see that the output is
17
23
26
26


:)
Last edited on
line 11 is: a = a + (BONUS - (a/2)).
That should give you 26.
Topic archived. No new replies allowed.