Thread: Count the number of element in 2D char array

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    Count the number of element in 2D char array

    My program is to process the elements in a 2D array, it should return the totla number '9' count in the car board,
    but I stop in the line ''countemptyPosition(char board[8][8]);'', it response with expression syntax, can someone help me to point my mistake? thk a lot.

    Code:
    #include <stdio.h>
    
    int countemptyPosition(char board[8][8]){
    
    int count, x,y;
    
     for (x=0;x<8; x++)          /* x from 0 to 7 */
       for (y=0;y<8; y++) {      /* y from 0 to 7 */
        if (board[x][y]=='9')     /* if=9 then increment count by 1*/
          count++;}
    
     return count;
    
    }
    
    
    int main(void){
    
      int count;
      char board[8][8]={0,7,5,6,8,3,5,3,0,6,5,6,4,8,5,9,0,2,1,6,4,1,1,1,0,1,5,6,9,3,4,3,0,2,5,6,4,3,1,3,0,4,6,6,9,3,5,0,0,4,5,6,4,3,5,1,0,4,5,6,4,3,5,3};
      countemptyPosition(char board[8][8]);
    
      printf("count=%d\n",count);
      getchar();
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int count, x y;
    Try setting count to a value before you use it. Perhaps something like 0.

    Code:
    int count;
    
    ...
    
    countemptyPosition(char board[8][8]);
    Also consider using the return value of countemptyPosition. Perhaps store that value in count.

    [edit]
    Edited for fun color and clarity.
    [/edit]

    Quzah.
    Last edited by quzah; 04-23-2004 at 10:43 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    To call the function and assign the result
    Code:
    count = countemptyPosition(board);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM