# Thread: Need help to find a bug

1. ## Need help to find a bug

Hi

The programme enters an endless loop after printing out. What am I doing wrong?

Thanks,
SK

Code:
```#include <bitset>
#include <math.h>

int main()
{
static const unsigned n = 27;
static unsigned int w[ n ] = {10,12,10,4,12,29,7,27,4,
7,29,29,12,12,7,29,7,4,4,3,13,27,12,14,7,4,10};
static float p[ n ] = {8.34,10.59,7.6,0.8,10.32,82.18,5.47,45.27,1.34,5.31,
63.92,61.09,11.18,10.03,4.39,59.21,3.37,0.48,2.27,
0.41,16.39,38.09,10.67,21.45,5.4,2.02,9.22};

static const unsigned nc = 1 << n;

double pr = 0.0f;

unsigned int sumv = 0;
unsigned int sumw = 0;
float sump = 0.0f;

double RES = 0.0f;

unsigned int i;
for ( i = nc; i--; )
{
std::bitset < n > v = i;
sumv = v.count();
if ( sumv < 18 ) continue;
sumw = 0;
sump = 0;

pr = pow( 0.6 , sumv ) * pow( 0.4 , n - sumv );

unsigned int j;
for ( j = n; j--; ) {
if ( v[ n - j - 1 ] ) {
sumw += w[ j ];
sump += p[ j ];
}
}

if ( sumw >= 255 && sump >= 308.01 ) RES += pr;
}

printf( "&#37;2.6f\n" , RES);

return 0;
}```

2. >> And the programme enters an endless loop after printing out.

After printing out? There is no code after printing out.

You realize that 1 << 27 is a very large number, right? It will take a while to finish. The code finishes for me eventually.

3. You FOR loop does not have a condition clause, and there is no "break" statement within the loop.

(edit: well, it does have a condition, but it's quite silly)

4. Originally Posted by Todd Burch
You FOR loop does not have a condition clause, and there is no "break" statement within the loop.

(edit: well, it does have a condition, but it's quite silly)
not sure I understand, which FOR loop and what condition clause

you mean:

Code:
`for( i = n ; i--; ) {}`
I thought it counts n-1, ..., 0 and is perfectly ok.

SK

5. It's fine. It's unorthodox, but it doesn't cause an infinite loop.

It only takes 20 seconds to finish in Release mode for me and a few minutes in debug mode.

Do you still think you have a problem?

6. Originally Posted by Daved
It's fine. It's unorthodox, but it doesn't cause an infinite loop.

It only takes 20 seconds to finish in Release mode for me and a few minutes in debug mode.

Do you still think you have a problem?
Ok, I am new to all this. What tipped me off is the big red STOP button in the Xcode Debugger Concole (on my Mac) that does not go away after the programme prints, which it normally does (i.e. gets replaced by a Go button). But this may hav nothing to do with my code...

SK

7. I just checked in Release mode. The programme does not return the shell prompt. It would be cool if someone could compile and run this thing on a Mac for me.

SK

8. Just so we are crystal clear here, you do allow it the necessary time to finish (I yielded the same times as Dave did with -dbg and -O3) and after it prints out 0.107477 it never returns to your command prompt?

9. Originally Posted by master5001
Just so we are crystal clear here, you do allow it the necessary time to finish (I yielded the same times as Dave did with -dbg and -O3) and after it prints out 0.107477 it never returns to your command prompt?
yes, the prompt cursor keeps blinking, but the command prompt does not re-appear:

last lines of the prompt (after printing the programme output):

logout
[Process completed]

yet I cannot type anything.

I found a similar issue on a Mac forum here:
http://www.macosxhints.com/article.p...01105230337264
but I don't really follow the explanation

SK

10. I unfortunately do not have a Mac test machine handy. Even a release version exhibits this behavior?

11. Well, I have a new Mac and I've not tried out XCode, so I gave it a shot. New Command-Line Utility C++ project...cut and paste the code...ran it (3 times), and got in the debugger console:

Code:
```[Session started at 2008-05-08 21:21:24 -0400.]
0.107477

The Debugger has exited with status 0.
[Session started at 2008-05-08 21:21:56 -0400.]
0.107477

The Debugger has exited with status 0.
[Session started at 2008-05-08 21:22:32 -0400.]
0.107477

The Debugger has exited with status 0.```
And 3 times in the Terminal:

Code:
```neon:Release me\$ ./CProgramming
0.107477
neon:Release me\$ ./CProgramming
0.107477
neon:Release me\$ time ./CProgramming
0.107477

real	0m5.784s
user	0m5.780s
sys	0m0.003s
neon:Release me\$```
Looks good to me.

12. Try doing a complete rebuild. Like delete all the intermediate files and whatnot... Perhaps its linking something weird into the program.

13. im new to c++ myself really but i always thought that a for loop should look like this?
for(initilze;condition;expression)
so maybe it would work better as for( i = nc ; ; i-- )
maybe ive overlooked your code but thats what i would do?

14. so maybe it would work better as for( i = nc ; ; i-- )
That would be an infinite loop.

15. very true but as im new to this i dont understand how the condition ( ; i-- ; )
does it mean if ( i ) is one less than the previous loop?
works? perhaps ( i = nc; i>0(or how ever many loops u need);i--)