this method return "StackOverflowException" error for some inputs;like "12"
where is Infinite loop point?

public void cycle(int f){
            int i, j;
            if (f == 0)
                return;
            else
            {
                for (i = f; i >= 0; i--)
                {
                    for (j = 0; j <= 10; j++)
                    {
                        if (i == Math.Pow(2, j))
                        {
                            f = f - (int)Math.Pow(2, j);
                            switch (j)
                            {
                                case 1:
                                    cbx1.Checked = true;
                                    break;
                                case 2:
                                    cbx2.Checked = true;
                                    break;
                                case 3:
                                    cbx3.Checked = true;
                                    break;
                                case 4:
                                    cbx4.Checked = true;
                                    break;
                                case 5:
                                    cbx5.Checked = true;
                                    break;
                                case 6:
                                    cbx6.Checked = true;
                                    break;
                                case 7:
                                    cbx7.Checked = true;
                                    break;
                                case 8:
                                    cbx8.Checked = true;
                                    break;
                                case 9:
                                    cbx9.Checked = true;
                                    break;
                                case 10:
                                    cbx10.Checked = true;
                                    break;
                            }
                            break;
                        }
                    }
                }
        cycle(f);
        }
    }

Recommended Answers

All 8 Replies

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.

Why "line 11 will never be true"?.In Line 7 i will change.For example by using 12 as input value when i become 8 (when j become 3) statement in line 11 become true.

Say i = 12, the input you stated. What power of 2 is equal to 12?

Yes.You are right,none of powers of 2 aren't 12.But in the later rounds of first loop (Line 7) when i reach to 8 ,statement in line 11 become true so value of f will change.
excuse me for my foolish questions.

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 for (i = f; i >= 0 && f != 0; i--) should fix it.

Thank you so much.but I have some problems yet.I want this equivalents for checkboxes :
1==>cbx1
2==>cbx2
4==>cbx3
8==>cbx4
.
.
512==>cbx10
But in my code order of equivalents is like this:
2==>cbx1
4==>cbx2
8==>cbx3
16==>cbx4
.
.
1024==>cbx10
Also I think this is the reason for encounter error for some inputes like 9

You need to add a Case 0 and a checkbox for it.

I change my code to this and all of problems solved.last changes Bolded
Thank you so much Momerath

public void cycle(int f)
            {
            int i, j;
            if (f == 0)
                return;
            else
            {
               [B] for (i = f; i >= 0 ; i--)[/B]
                {
                    for (j = 0; j <= 10; j++)
                    {
                        if (i == Math.Pow(2, j))
                        {
                            f = f - (int)Math.Pow(2, j);
                            [B]i = f+1;[/B]
                            switch (j)
                            {
                                case [B]0[/B]:
                                    cbx1.Checked = true;
                                    break;
                                case [B]1[/B]:
                                    cbx2.Checked = true;
                                    break;
                                case [B]2[/B]:
                                    cbx3.Checked = true;
                                    break;
                                case [B]3[/B]:
                                    cbx4.Checked = true;
                                    break;
                                case [B]4[/B]:
                                    cbx5.Checked = true;
                                    break;
                                case [B]5[/B]:
                                    cbx6.Checked = true;
                                    break;
                                case [B]6[/B]:
                                    cbx7.Checked = true;
                                    break;
                                case [B]7[/B]:
                                    cbx8.Checked = true;
                                    break;
                                case [B]8[/B]:
                                    cbx9.Checked = true;
                                    break;
                                case [B]9[/B]:
                                    cbx10.Checked = true;
                                    break;
                            }
                            break;
                        }
                    }
                }
        cycle(f);
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.