Thread: Sorry, one more request for help

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Sorry, one more request for help

    Last problem, I promise!

    I'm getting a crash with the following code ( exact crash report to follow code) and I don't really know why.

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* include stdlib.h for rand functionality */
    #include <time.h> /* include time.h for srand seed */
    
    int main( void )
    {
    /* initialize variables */
    int draw(); /* draw function prototype */
    int hold; /* variable for bubble sorting hands */
    int parray[5] = {0}; /* player's hand */
    int carray[5] = {0}; /* computer's plan */
    int pass; /* passes for sorting */
    int i; /* counter for sorting */
    
    /* seed random number generator */
    srand((unsigned) time(0));
    
    /* Draw hands*/
    parray[0] = draw(); parray[1] = draw(), parray[2] = draw(); parray[3] = draw(); parray[4] = draw();
    carray[0] = draw(); parray[1] = draw(), carray[2] = draw(); carray[3] = draw(); carray[4] = draw();
    
    /* bubble sort player's hand*/  
    for( pass = 0; pass < 5; pass++)  
    { 
    
        /* loop to control number of comparisons per pass */  
        for ( i = ( pass + 1 ); i < 5; i++)  
        {  
            /* compare adjacent elements and swap them if first element is greater than second element */  
            if ( parray[ pass ] > parray[ i ] )  
            {  
                hold         = parray[ pass ];  
                parray[ pass ] = parray[ i ];  
                parray[ i ]    = hold;  
            }  
        } 
    
    }  
    /* bubble sort computer's hand*/  
    for( pass = 0; pass < 5; pass++)  
    { 
    
        /* loop to control number of comparisons per pass */  
        for ( i = ( pass + 1 ); i < 5; i++)  
        {  
            /* compare adjacent elements and swap them if first element is greater than second element */  
            if ( carray[ pass ] > carray[ i ] )  
            {  
                hold         = carray[ pass ];  
                carray[ pass ] = carray[ i ];  
                carray[ i ]    = hold;  
            }  
        } 
    
    }  
    
    /* assign array values to corresponding cards for easier human understanding */
    
    /* print hands */
    printf( "%d, %d, %d, %d, %d\t\t%d, %d, %d, %d, %d\n", parray[0], parray[1], parray[2], parray[3], parray[4], carray[0], carray[1], carray[2], carray[3], carray[4]);
    printf( "   Player  \t \t \t    Computer\n\n");
    
    return 0; /* indicate program ended normally */
    }
    
    
    int draw ()
    {
    static int cards[ 52 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52};
    int chosen = 0;
    int card;
    
    chosen = rand () % 52;
    
    card = cards[ chosen ];
    
    while ( cards[ chosen ] == 99 ) {
    	draw();
    }
    
    cards[ chosen ] = 99;
    
    return card;
    }

    I'm using Visual C++ 2008 Express edition to write/compile. (To differentiate to C from C++ I'm changing the extension from .cpp to .c and turning off pre-compiled headers.) I'm using Windows 7 Home Premium on my laptop.

    The error message is:
    Code:
    Problem signature:
      Problem Event Name:	APPCRASH
      Application Name:	poker.exe
      Application Version:	0.0.0.0
      Application Timestamp:	4b9e7416
      Fault Module Name:	poker.exe
      Fault Module Version:	0.0.0.0
      Fault Module Timestamp:	4b9e7416
      Exception Code:	c00000fd
      Exception Offset:	00011819
      OS Version:	6.1.7600.2.0.0.768.3
      Locale ID:	1033
      Additional Information 1:	65c6
      Additional Information 2:	65c6d20b57aedc2c7b06b00be67164f7
      Additional Information 3:	7a9d
      Additional Information 4:	7a9d4400e8726e69ded28cd07dd569c9
    
    Read our privacy statement online:
      http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
    
    If the online privacy statement is not available, please read our privacy statement offline:
      C:\Windows\system32\en-US\erofflps.txt
    any ideas?


    The only thing I notice is that it will run fine sometimes (although computer's first card always seems to be 0 for some reason) but then for a while it will just error out. Could it be a problem with using the time to seed the random number generator?
    Last edited by ericdabear; 03-15-2010 at 12:02 PM. Reason: I suspect the time?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your draw function could use recursion of 51 levels for the last card... try to use iteration instead...

    replace
    Code:
    chosen = rand () % 52;
    
    card = cards[ chosen ];
    
    while ( cards[ chosen ] == 99 ) {
    	draw();
    }
    with something like
    Code:
    do
    {
        chosen = rand () % 52;
        card = cards[ chosen ];
    }while ( card  == 99 );
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might be exceeding the maximum recursion depth by calling draw() recursively until a card whose value is not 99 is found.

    A better approach might be to shuffle the cards array once, then have draw() return the next available card value.
    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

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    thanks for the help, got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not able to create 1024 socket request.
    By rahul.shukla in forum Networking/Device Communication
    Replies: 3
    Last Post: 02-15-2010, 03:49 PM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  4. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  5. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM