Line 11 will never be true for any value that is not a power of two. Because of this you never will reach line 13 for these values so the value of 'f' won't change. You then, in line 51, call the method again with the exact same value you started with so it repeats calling this method until the stack overflows (about 250,000 calls later).
The error in your code is line 11.
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
Say i = 12, the input you stated. What power of 2 is equal to 12?
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
So lets run through your code and see what happens. We'll ignore the setting of the check boxes as they don't affect the values of the variables.
So we call cycle with 12, f = 12
f does not equal zero
i = 12
j = 0 .. 10, i is never equal to any of the powers of two
i = 11
j = 0 .. 10, i is never equal to any of the powers of two
i = 10
j = 0 .. 10, i is never equal to any of the powers of two
i = 9
j = 0 .. 10, i is never equal to any of the powers of two
i = 8
j = 0,1: nothing. j = 2, we have a winner!
f = f-pow(2,j) = 12 - 8 = 4, we break the 'j' loop
i = 7
j = 0 .. 10, i is never equal to any of the powers of two
i = 6
j = 0 .. 10, i is never equal to any of the powers of two
i = 5
j = 0 .. 10, i is never equal to any of the powers of two
i = 4, another one found with j = 2
f = f - Pow(2, 2) = 4 - 4 = 0
i = 3
j = 0 .. 10, i is never equal to any of the powers of two
i = 2, another one found with j = 1
f = f - Pow(2,1) = 0 - 2 = -2
i = 1, another one found with j = 0
f = f - Pow(2,0) = -2 - 1 = -3
i = 0
j = 0 .. 10, i is never equal to any of the powers of two
Now we are out of the loops with f = -3, and we call cycle again
f = -3
i loop never runs as i starts less than zero, so all loops skipped and we call cycle again with f = -3.
Repeat until stack overflow.
Changing line 18 to [icode]for (i = f; i >= 0 && f != 0; i--)[/code] should fix it.
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
You need to add a Case 0 and a checkbox for it.
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558