finding positions of characters in a 2d array

This is a discussion on finding positions of characters in a 2d array within the C Programming forums, part of the General Programming Boards category; I'm relatively new to C, and part of a program I have to write requires finding the position of a ...

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21