Need help to find a bug

This is a discussion on Need help to find a bug within the C++ Programming forums, part of the General Programming Boards category; Hi The programme enters an endless loop after printing out. What am I doing wrong? Thanks, SK Code: #include <bitset> ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    58

    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;
    }
    Last edited by serge; 05-08-2008 at 04:52 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> 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.
    Last edited by Daved; 05-08-2008 at 05:02 PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    58
    Quote Originally Posted by Todd Burch View Post
    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
    Last edited by serge; 05-08-2008 at 05:10 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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. #6
    Registered User
    Join Date
    May 2008
    Posts
    58
    Quote Originally Posted by Daved View Post
    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...

    Anyway, thanks for your help!!
    SK

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    58
    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. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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?
    Last edited by master5001; 05-08-2008 at 05:58 PM.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    58
    Quote Originally Posted by master5001 View Post
    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
    Last edited by serge; 05-08-2008 at 06:07 PM.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I unfortunately do not have a Mac test machine handy. Even a release version exhibits this behavior?

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    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. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Try doing a complete rebuild. Like delete all the intermediate files and whatnot... Perhaps its linking something weird into the program.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    so maybe it would work better as for( i = nc ; ; i-- )
    That would be an infinite loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    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--)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 08:52 AM
  2. Can't Find Bug in basic MP3 Sorter
    By trickeastcs in forum C++ Programming
    Replies: 12
    Last Post: 12-14-2007, 05:31 PM
  3. Replies: 5
    Last Post: 04-16-2004, 02:29 AM
  4. Time for another round of: FIND THAT BUG!
    By DeepBlackMagic in forum C++ Programming
    Replies: 0
    Last Post: 10-29-2003, 05:29 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 03:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21