Thread: Multi-dimensional array searching

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    15

    Multi-dimensional array searching

    I recently created a program that shuffles a deck and prints it out. This was done using a multi-dimensional array
    Code:
    void print_deck(int deck[], int size)
        {
            int i;
            char suit[4][9]={"Diamonds","Hearts","Clubs","Spades"};
            char rank[13][6]={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    
    
            for (i=0; i<52; i++)
                printf("%s of %s\n",rank[deck[i]%13],suit[deck[i]/13]);
        }
    I am now trying to have a user input the rank and suit (just the first letter or number) and have the program output the card number that corresponds to in the shuffled deck. For example, 2 H is the 2 of hearts and a loop or something will search the array looking for the 2 and the H. When then are found the loop stops and the card number in the deck is displayed. Is there any easy method of doing this?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That isn't "multi-dimensional array searching".

    That is searching for a match of the first character of a string in a "single-dimensional array".

    Approach the task with that in mind and see what you can do.

    Soma

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    Ok that make sense. I am now having trouble searching the entire deck. It searches the first thirteen then stops searching.
    Code:
    int search_deck(int deck[], int size)
        {
            int i;
            char c,c2;
            char suit[4][9]={"Diamonds","Hearts","Clubs","Spades"};
            char rank[13][6]={"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
    
    
            printf("\nEnter the desired rank of card followed by the suit(eg:2 D): ");
            fflush(stdin);
            scanf("%c %c",&c,&c2);
            putchar('\n');
    
    
            for (i=0; i<52; i++)
                {
                    printf("%s of %s\n",rank[deck[i]%13],suit[deck[i]/13]);
    
    
                    if (suit[i][0]!=c2)
                        card++;
                }
        }

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You have two array, two `char' variables, and two requests for user input; why do you imagine that you can combine the search into one loop so easily?

    *shrug*

    It can be done, but it is ugly.

    Instead, consider that you might want two loops.

    Soma

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char suit[4][9]={"Diamonds","Hearts","Clubs","Spades"};
    ...
    for (i=0; i<52; i++)
    {
         printf("%s of %s\n",rank[deck[i]%13],suit[deck[i]/13]);
    
         if (suit[i][0]!=c2)
            card++;
    }
    Where is card declared?
    What will happen when i becomes 4 (look at your declaration of the suit array)?

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    The card is declared globally. Ok so if I was to use two loops how do I compare the suit to the deck suit. If I do something like this...
    Code:
    for(j=0;j<4;j++)
                        {if (suit[j][0]== c2)
                            printf("Yes  ");}
    then it will always be if the entered letter is a C, H, S, D. How would I do this but compare the entered letter to the current card in the deck?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can actually do this with one loop. You know how to compute the rank of a card in the deck (rank[deck[i]%13]), and the suit (suit[deck[i]/13]). Go through each card in deck[]. If the rank the user entered (c) is equal to the rank of the current card and the suit entered (c2) is equal to the suit of the current card, it's a match.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    15
    I have tried this numerous times and it did not work

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, let's see what you came up with. You know there's an if statement involved, and you know there is an && and two checks for ==. You could at least attempt something with the correct form. Give it a shot and post back with some code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Dimensional Array
    By johnmackin in forum C Programming
    Replies: 0
    Last Post: 06-13-2012, 06:45 PM
  2. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  3. multi-dimensional array
    By shuo in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2008, 01:03 AM
  4. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  5. multi-dimensional array
    By mcorn in forum C Programming
    Replies: 2
    Last Post: 08-04-2002, 09:14 AM