Thread: qsort is not sorting my hand by the face value..also need 2nd sort for suit

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    5

    qsort is not sorting my hand by the face value..also need 2nd sort for suit

    This is what i have so far, it will deal but not sorted at all, . what am i messing up?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define DECK_SIZE 52
    
    struct card
    {
        const char *face;
        const char *suit;
    };
    
    typedef struct card Card;
    
    void fillDeck(Card * const, const char *[], const char *[]);
    void shuffle(Card * const);
    void deal(const Card * const);
    void qsort(void *base, size_t nitems, size_t size, int (*compr)(const void *, const void*));
    
    //need to adjust the comparator function to sort by faces and if they have same face then they use suit to sort the double faces.
    int cmpface(void *a, void *b)
    {
        struct Card *pa = a;
        struct Card *pb = b;
    
    }
    
    int main()
    {
        Card deck[DECK_SIZE];
        const char *face[] ={"Ace","King","Queen","Jack","Ten","Nine","Eight","Seven","Six","Five","Four","Three","Two"};
        const char *suit[] ={"Clubs","Diamonds","Hearts", "Spades"};
        srand(time(NULL));
        fillDeck(deck, face, suit);
        shuffle(deck);
        qsort(face, 13, sizeof(int), cmpface);
        deal(deck);
        return 0;
    }
    
    void fillDeck(Card * const wDeck, const char * wFace[], const char * wSuit[])
    {
        int i;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            wDeck[i].face = wFace[i%13];
            wDeck[i].suit = wSuit[i/13];
        }
    }
    
    void shuffle(Card * const wDeck)
    {
        int i,j;
        Card temp;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            j=rand()%DECK_SIZE;
            temp = wDeck[i];
            wDeck[i] = wDeck[j];
            wDeck[j] = temp;
        }
    }
    
    void deal (const Card * const wDeck)
    {
        int i;
        printf("\nHere is a 10-card hand that was dealt randomly by the machine:\n\n");
        for(i=0; i<=10; i++)
        {
            printf("\t%5s of %-8s\n", wDeck[i].face, wDeck[i].suit);
        }
        putchar('\n');
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You seem to have forgotten to finish implementing cmpface.
    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

  3. #3
    Registered User
    Join Date
    Nov 2014
    Posts
    5
    Quote Originally Posted by laserlight View Post
    You seem to have forgotten to finish implementing cmpface.
    oops posted old version, this one is the one i wanted to post:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define DECK_SIZE 52
    
    typedef struct
    {
        char *face, *suit;
    }Card;
    
    void fillDeck(Card * const, char *[], char *[]);
    void shuffle(Card * const);
    void deal(const Card * const);
    void qsort(void *base, size_t nitems, size_t size, int (*compr)(const void *, const void*));
    
    //need to adjust the comparator function to sort by faces and if they have same face then they use suit to sort the double faces.
    int cmpface(const void *p_a, const void *p_b)
    {
        char **a = (char **)p_a;
        char **b = (char **)p_b;
        return strcmp(*a, *b);
    }
    
    int main()
    {
        Card deck[DECK_SIZE];
        char *face[] ={"Ace","King","Queen","Jack","Ten","Nine","Eight","Seven","Six","Five","Four","Three","Two"};
        char *suit[] ={"Clubs","Diamonds","Hearts", "Spades"};
        srand(time(NULL));
        fillDeck(deck, face, suit);
        shuffle(deck);
        qsort(face, sizeof(face)/sizeof(char *), sizeof(char *), cmpface);
        deal(deck);
        return 0;
    }
    
    void fillDeck(Card * const wDeck, char * wFace[], char * wSuit[])
    {
        int i;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            wDeck[i].face = wFace[i%13];
            wDeck[i].suit = wSuit[i/13];
        }
    }
    
    void shuffle(Card * const wDeck)
    {
        int i,j;
        Card temp;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            j=rand()%DECK_SIZE;
            temp = wDeck[i];
            wDeck[i] = wDeck[j];
            wDeck[j] = temp;
        }
    }
    
    void deal (const Card * const wDeck)
    {
        int i;
        printf("\nHere is a 10-card hand that was dealt randomly by the machine:\n\n");
        for(i=0; i<=10; i++)
        {
            printf("\t%5s of %-8s\n", wDeck[i].face, wDeck[i].suit);
        }
        putchar('\n');
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you sort face?
    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

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Why do you sort face?
    you are right i need to use deck, i also made a few changes i made face array a int array to make life easier i am going to build a comparator function.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define DECK_SIZE 52
    
    typedef struct
    {
        int face;
        char *suit;
    }Card;
    
    void fillDeck(Card * const, int[], char *[]);
    void shuffle(Card * const);
    void deal(const Card * const);
    void qsort(void *base, size_t nitems, size_t size, int (*compr)(const void *, const void*));
    
    //need to adjust the comparator function to sort by faces and if they have same face then they use suit to sort the double faces.
    int cmpface(const void *p_a, const void *p_b)
    {
        
    }
    
    int main()
    {
        Card deck[DECK_SIZE];
        int face[] ={14,13,12,11,10,9,8,7,6,5,4,3,2};
        char *suit[] ={"Clubs","Diamonds","Hearts", "Spades"};
        srand(time(NULL));
        fillDeck(deck, face, suit);
        shuffle(deck);
        qsort(deck, 10, sizeof(Card), cmpface);
        deal(deck);
        return 0;
    }
    
    void fillDeck(Card * const wDeck, int wFace[], char * wSuit[])
    {
        int i;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            wDeck[i].face = wFace[i%13];
            wDeck[i].suit = wSuit[i/13];
        }
    }
    
    void shuffle(Card * const wDeck)
    {
        int i,j;
        Card temp;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            j=rand()%DECK_SIZE;
            temp = wDeck[i];
            wDeck[i] = wDeck[j];
            wDeck[j] = temp;
        }
    }
    
    void deal (const Card * const wDeck)
    {
        int i;
        printf("\nHere is a 10-card hand that was dealt randomly by the machine:\n\n");
        for(i=0; i<=10; i++)
        {
            printf("\t%5s of %-8s\n", wDeck[i].face, wDeck[i].suit);
        }
        putchar('\n');
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Why do I get the feeling that you've completely missed the point?
    Sorting A 10 Card Poker Hand Using Qsort - C And C++ | Dream.In.Code

    You need to get it into your mind that the thing you store doesn't have to be the same as the thing you print.

    Otherwise, you're just going to be stuck with storing char* in your struct and then face an impossible task of figuring out how to compare two strings in a "numeric" way, without knowing what numbers the strings represent.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2014
    Posts
    5
    i made the comparator function but i ran it several times and the last number on the list is almost always out of place ?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define DECK_SIZE 52
    
    typedef struct
    {
        int face;
        char *suit;
    }Card;
    
    void fillDeck(Card * const, int[], char *[]);
    void shuffle(Card * const);
    void deal(const Card * const);
    void qsort(void *base, size_t nitems, size_t size, int (*compr)(const void *, const void*));
    
    //need to adjust the comparator function to sort by faces and if they have same face then they use suit to sort the double faces.
    int cmpface(const void *p_a, const void *p_b)
    {
    //    struct Card *a = p_a;
    //    struct Card *b = p_b;
        int a = *(int *)p_a;
        int b = *(int *)p_b;
        return a - b;
    }
    
    int main()
    {
        Card deck[DECK_SIZE];
        int face[] ={14,13,12,11,10,9,8,7,6,5,4,3,2};
        char *suit[] ={"Clubs","Diamonds","Hearts", "Spades"};
        srand(time(NULL));
        fillDeck(deck, face, suit);
        shuffle(deck);
        qsort(deck, 10, sizeof(Card), cmpface);
        deal(deck);
        return 0;
    }
    
    void fillDeck(Card * const wDeck, int wFace[], char * wSuit[])
    {
        int i;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            wDeck[i].face = wFace[i%13];
            wDeck[i].suit = wSuit[i/13];
        }
    }
    
    void shuffle(Card * const wDeck)
    {
        int i,j;
        Card temp;
        for(i=0; i<=DECK_SIZE-1; i++)
        {
            j=rand()%DECK_SIZE;
            temp = wDeck[i];
            wDeck[i] = wDeck[j];
            wDeck[j] = temp;
        }
    }
    
    void deal (const Card * const wDeck)
    {
        int i;
        printf("\nHere is a 10-card hand that was dealt randomly by the machine:\n\n");
        for(i=0; i<=10; i++)
        {
            printf("\t%5d of %-8s\n", wDeck[i].face, wDeck[i].suit);
        }
        putchar('\n');
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're comparing Card objects, not ints. Your comparator should probably look like this:
    Code:
    // sort by faces and if they have same face then they use suit to sort the double faces.
    int compare_cards(const void *p_a, const void *p_b)
    {
        const Card *a = (const Card*)p_a;
        const Card *b = (const Card*)p_b;
        if (a->face < b->face)
        {
            return -1;
        }
        else if (a->face > b->face)
        {
            return 1;
        }
        else
        {
            return strcmp(a->suit, b->suit);
        }
    }
    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

  9. #9
    Registered User
    Join Date
    Nov 2014
    Posts
    5
    OH ok, this makes a whole lot more sense for the card structure has the int array and char array in it and then do what i need to in compare function if i cast to card object., thank you for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort not sorting structs
    By ArcadeEdge in forum C Programming
    Replies: 6
    Last Post: 04-11-2012, 05:44 AM
  2. Sorting hand of cards in poker
    By yangladesh in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2011, 04:25 PM
  3. how to sort a player's hand in seven card rummy
    By dex0391 in forum C Programming
    Replies: 4
    Last Post: 06-19-2010, 09:40 PM
  4. Can I use STL qsort() for sorting structure?
    By franziss in forum C++ Programming
    Replies: 14
    Last Post: 09-20-2005, 05:34 AM
  5. qsort not sorting
    By sweets in forum C Programming
    Replies: 3
    Last Post: 08-08-2004, 04:46 AM