Thread: finding positions of characters in a 2d array

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    44

    finding positions of characters in a 2d array

    I'm relatively new to C, and part of a program I have to write requires finding the position of a 'space' character within the array, which I'm having a lot of trouble with. It's a 5x5 array with one space in it, a puzzle.

    Here's what I have so far:


    Code:
    #include <stdio.h>
    
    #define ROW 5
    #define COLUMN 5
    
    void fromAbove(char aboveArray[5][5]);
    
    int main()
    {
            char array[ROW][COLUMN];
            int row = 0;
            int column;
            FILE *infile, *outfile, *test;
    
            infile = fopen("./puzzle.in","r");
            outfile = fopen("./puzzle.out","w");
            test = fopen("./test.out","w");
    
            fprintf(outfile,"Puzzle #1: \n");
            while(row <= 5)
            {
                    for(column = 0; column < 5; column++)
                    {
                            fscanf(infile,"%c",&array[row][column]);
                            fprintf(outfile," %c",array[row][column]);
                    }
                    row++;
            }
    
            fromAbove(**array);
    
            fclose(outfile);
            fclose(infile);
            fclose(test);
    
            return 0;
    
    }
    
    void fromAbove(char aboveArray[5][5])
    {
            int row;
            int column;
            char a = aboveArray[row][column];
    
            if(a == ' ')
            {
                    printf("%c",aboveArray[row][column]);
            }
    }

    Also, I'm getting these errors: "type of formal parameter 1 is incomplete", and "
    passing argument 1 of āfromAboveā makes pointer from integer without a cast"

    Any help would be appreciated. Thanks

    P.S. sorry I'm new here so no idea how to post code properly yet...

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Synhyborex!

    Code:
            while(row <= 5)
            {
                    for(column = 0; column < 5; column++)
                    {
                            fscanf(infile,"%c",&array[row][column]);
                            fprintf(outfile," %c",array[row][column]);
                    }
                    row++;
            }
    While you're reading in char's from the file, into the array, you can make a quick check to see where the space is:

    Code:
    int space_row=0, space_col = 0;
    //other code here
    
    //deep inside your while() and for() loop
    if(array[row][column] == ' ') {
      space_row = row;
      space_col = column;
    }
    Since you have ROW and COLUMN defines, it's better to use them, than to use the number 5 in your program. That way, if you ever change the ROW or COLUMN define value, the program will still work correctly.
    Last edited by Adak; 02-13-2011 at 09:19 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    alright awesome! definitely makes things easier. also requires me to kill the array in my function so i lose the errors i was getting. thanks a bunch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-23-2010, 02:19 PM
  2. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  3. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM