Thread: Counting number of items in an [][]array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    Counting number of items in an [][]array

    Is there a way to count how many items there are in a specific column? I've looked all over the place and nothing seems to be matching what I'm trying to find.

    I'm programming a card game similar to freecell and when I'm checking to see if there's a card in the column the user has chosen and when I'm trying to find the last card in the column so it can be moved, I have no clue how to really go about that. It's all represented as a 2-d array.

    With that side, once that's done, how would you put the card at the bottom of the column to be moved to?
    Thanks!

  2. #2
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    post ur code

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The number of items in an array is fixed. What you need to do is to have some dummy value that represents one-past-the-last-item.
    E.g. if your cards were represented by the numbers 1-52 then you could use 0 to indicate that you've read the last item in a column.
    If that doesn't answer the question, you'll most likely need to show code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    I know that any array is fixed in size, but what I care about is finding the last value in a colum of the array. If anyone is wondering what card game this is it's called Baker's Dozen. You start off with 13 columns of 4 cards in each column. A column can have max 11 cards and you place a lower value card on top of a higher value card or put it in 1 of the 4 suit piles starting with ace and going up. You can only move 1 card at a time.

    I'm not showing the displayBoard() function cuz that's not important. The first commented out section is me checking that the user didn't choose an empty column to move a card from. As soon as I can figure out how to find the last item in a column the sooner I can add more code.

    Code:
    typedef struct
    {
        char face[3];
        int suit;
    } Card;
    
    void fillDeck(Card eDeck[], char cardFaces[][3], int cardSuits[]);
    void shuffle(Card fDeck[]);
    void deal(Card fDeck[], Card stacks[][13]);
    void displayBoard(Card stacks[][13], Card piles[][13]);
    
    int main()
    {
        Card deck[52];
        char facesList[13][3] = {"A", "2", "3", "4", "5", "6", "7", "8",
                              "9", "10", "J", "Q", "K"};
        int suitsList[4] = {3, 4, 5, 6};
        Card stacks[4][13];
        Card piles[4][13];
        int oldPos, pile;
        int cardCount = 0;
        char newPos;
        
        fillDeck(deck, facesList, suitsList);
        shuffle(deck);
        system("PAUSE");
        deal(deck, stacks);
        
        while(cardCount != 51)
        {
            displayBoard(stacks, piles);
            printf("\n\nChoose a column(1-13): ");
            scanf(" %d", &oldPos);
            oldPos--;
            
    //        if(stacks[0][oldPos] == 0)
    //        {
    //            printf("There's nothing to move in this column.");
    //            system("PAUSE");
    //            system("CLS");
    //            continue;
    //        }
            
            printf("\nWhere to move this card to? (1-13 or p): ");
            scanf(" %c", &newPos);
            
            if(newPos == 'p') //needs wrong pile checker
            {
                printf("\nWhich pile? (1-4): ");
                scanf(" %d", &pile);
                pile--;
                //needs more stuff
            }
            
            //check card is one more than card to be placed
        }
        system("PAUSE");
        return 0;
    }
        
    void fillDeck(Card eDeck[], char cardFaces[][3], int cardSuits[])
    {
        int i;
         
        for(i=0; i<=51; i++)
        {
            strcpy(eDeck[i].face, cardFaces[i%13]);
            eDeck[i].suit = cardSuits[i/13];
        }
    }
    
    void shuffle(Card fDeck[])
    {
        int i;
        int randPos;
        Card temp;
        srand(time(NULL));
        
        for(i = 0; i <= 51; i++)
        {
            randPos = rand() % 52;
            temp = fDeck[i];
            fDeck[i] = fDeck[randPos];
            fDeck[randPos] = temp;
        }
    }
    
    void deal(Card fDeck[], Card stacks[][13])
    {
        int i, k;
        int j = 0;
        
        for(i = 0; i < 4; i++)
        {
              for(k = 0; k < 13; k++)
              {
                  stacks[i][k] = fDeck[j];
                  j++;
              }
        }
    }
    Last edited by A_User; 11-20-2011 at 07:49 PM.

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You could create another array containing the number of cards remaining in each stack. That would allow you to keep track of which card is the last one in each stack.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    That's brilliant. Just a little int array that knows the values of all the columns and I can just refer to that for all my checking needs. THANKS!

    btw nice sheep counting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Counting number of strings in an array
    By Hawkin in forum C Programming
    Replies: 4
    Last Post: 06-21-2010, 11:25 AM
  3. How dynamically assign the number of items
    By zcrself in forum C Programming
    Replies: 2
    Last Post: 08-19-2009, 06:37 AM
  4. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  5. Counting the # of items in an array
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2006, 02:02 AM