Thread: Array Help

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    11

    Array Help

    I am having problems with this function. This is an exercise from the book "C Programming: A Modern Approach" Chapter 10.

    Code:
    void read_cards(void)
    {
        char ch, rank_ch, suit_ch;
        int rank; 
        int suit = 0; 
        Bool bad_card;
        int cards_read = 0;
    
        /*	Sets all array elements to zero		*/
        for(rank = 0; rank < NUM_RANKS; rank++)
        {
    	hand[rank][suit] = 0;
    
    	for(suit = 0; suit < NUM_SUITS; suit++)
    	    hand[rank][suit] = FALSE;
        }
    
        while (cards_read < NUM_CARDS) 
        {
    	bad_card = FALSE;
    
    	printf("Enter a card: ");
    
    	rank_ch = getchar();
    	switch (rank_ch) 
    	{
    	    case '0':           exit(EXIT_SUCCESS);
    	    case '2':           rank = 0; break;
    	    case '3':           rank = 1; break;
    	    case '4':           rank = 2; break;
    	    case '5':           rank = 3; break;
    	    case '6':           rank = 4; break;
    	    case '7':           rank = 5; break;
    	    case '8':           rank = 6; break;
    	    case '9':           rank = 7; break;
    	    case 't': case 'T': rank = 8; break;
    	    case 'j': case 'J': rank = 9; break;
    	    case 'q': case 'Q': rank = 10; break;
    	    case 'k': case 'K': rank = 11; break;
    	    case 'a': case 'A': rank = 12; break;
    	    default:            bad_card = TRUE;
    	}
    
    	suit_ch = getchar();
    	switch (suit_ch) 
    	{
    	    case 'c': case 'C': suit = 0; break;
    	    case 'd': case 'D': suit = 1; break;
    	    case 'h': case 'H': suit = 2; break;
    	    case 's': case 'S': suit = 3; break;
    	    default:            bad_card = TRUE;
    	}
    
    	while ((ch = getchar()) != '\n')
    	    if (ch != ' ') bad_card = TRUE;
    
    	if (bad_card)
    	    printf("Bad card; ignored.\n");
    	else 
    	{
    	    hand[0][cards_read] = rank;
    	    hand[1][cards_read] = suit;
    	    printf("hand[1][%d] = %d. \n", cards_read, suit);
    	    cards_read++;
    	}
        }
        printf("Rank %d Suit %d. \n", hand[0][0], hand[1][0]);
        printf("Rank %d Suit %d. \n", hand[0][1], hand[1][1]);
        printf("Rank %d Suit %d. \n", hand[0][2], hand[1][2]);
        printf("Rank %d Suit %d. \n", hand[0][3], hand[1][3]);
        printf("Rank %d Suit %d. \n", hand[0][4], hand[1][4]);
    
    }
    This is the output I get from the command line.

    Code:
    Enter a card: 2h
    hand[1][0] = 2. 
    Enter a card: 3h
    hand[1][1] = 2. 
    Enter a card: 4h
    hand[1][2] = 2. 
    Enter a card: 5h
    hand[1][3] = 2. 
    Enter a card: 6h
    hand[1][4] = 2. 
    Rank 0 Suit 4. 
    Rank 1 Suit 2. 
    Rank 2 Suit 2. 
    Rank 3 Suit 2. 
    Rank 4 Suit 2.
    The thing I am confused about is when I print out to screen the value stored in hand[1][0] why is it 4 and not 2? I have noticed that it changes value if I enter different cards.

    Thanks for any help. If I need to provide any more information I will gladly do so.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    hand[rank][suit] = 0;
    It might be the beer talking, but I don't see where you have declared that array 'hand' - what is up with it?

    Is your program huge? Might be useful to see the whole thing if it is not overly large.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    Here is the top matter. I have not modified the analyze_hand and print_result functions to work with the changes requested in the exercise. They don't work with the changes I have made to the read_cards function. The part I am stuck on is before I even get that far. Here is the top matter.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    #define TRUE 1
    #define FALSE 0
    
    typedef int Bool;
    
    Bool straight, flush, four, three;
    int pairs;   /* can be 0, 1, or 2 */
    int hand[NUM_RANKS][NUM_SUITS];
    
    void read_cards(void);
    void analyze_hand(void);
    void print_result(void);
    
    /********************************************************** 
     * main: Calls read_cards, analyze_hand, and print_result * 
     *       repeatedly.                                      * 
     **********************************************************/
    int main()
    {
        for (;;) 
        {          /* infinite loop */
    	read_cards();
    	analyze_hand();
    	print_result();
        }
    
        return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What is your program even supposed to be doing? Why are you printing 'hand[1][ x ]' in your if check, but storing values in two places? If we actually had an idea of what you were really trying to do, it might help.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    The exercise is to rewrite a example program. Ultimately I am trying to store the rank and suit of the cards in a poker hand in a 5x2 array. My thoughts were since there were 5 cards in the hand I would store the rank in hand[0][*] and the suits in hand[1][*]. Then do the comparisons needed to analyze the hand.

    This is where I noticed the problem. I was trying to check for a flush (all suits should be the same) but it was not working. That is why I started looking at the values stored and noticed the value for hand[1][0] was not as it should be (2 in this case).

    I just don't understand why it is not a 2. That is all. The printf statements are just me trying to figure out why this is the case. They will be removed later when I figure out my error.

    I hope this helps clear things up.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you use structures for your cards then, and use an array of structures? Better yet, why don't you just number them 0 to 51, and call it a day? Something like this or that.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    11
    quzah your points are good ones. The exercise explicitly says to use a 5x2 array. Yet another fact I left out... Sorry.

    You guys are being more help than I am asking for. I am not complaining. This is a good thing.

    Maybe I am not clear. I do not want help finishing up the program. I just want to understand why my assignment statements are not working for the array specifically with the hand[1][0] portion not being a 2 or any other suit as I expect. Is there something obvious I am missing here. I don't see where the value should change. To me it looks straight forward.

    I just want to understand why the value changes for that array position and not any other for the suits i.e. hand[1][*]. I can not see the error in my logic.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd start from scratch if I were you. Or at least cut it down to a bare minimum.

    First make a deck which holds all of your cards. This will have to be a FACE x SUIT array, since you're set on doing it this way.
    Next, make a function that will pull one card from that deck. You will need to decide what value you're putting into the deck to mark that as used. Actually, you could just set the whole array to zero first, and if anything is in that particular spot, it means that card has been used. That's probably your best bet.
    Code:
    if( deck[ suit ][ face ] != 0 )
    {
        printf( "The card {insert format specifier for your card lookup} is already in use!\n",
            yourcardlookup( suit, face ) );
    }
    Something like that.

    Just make a small program that will grab a random card from the deck. Once you've got that figured out, try grabbing a random card and storing it some place. Store enough cards, and you've now got a hand.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM