Thread: Need help to find a bug

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

    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 03:52 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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 04:02 PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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)
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    115
    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 04:10 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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
    115
    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
    115
    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 04:58 PM.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    115
    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 05: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
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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
    28,413
    so maybe it would work better as for( i = nc ; ; i-- )
    That would be an infinite loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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--)

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, 07: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, 01:29 AM
  4. Time for another round of: FIND THAT BUG!
    By Geolingo 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, 02:53 PM