Thread: Array fills nice but read out gives problems

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

    Array fills nice but read out gives problems

    I've written a function who fills the array with the numbers 1 to 9. It's a 8 puzzle game. First I fill the array for the winning numbers. And in the second for loop I'll compare the values from the existing board to the winning board. But the moment I read out the winning board I expect a 3 but get a 4.

    Code:
    bool
    won(void)
    {
    
        int count = 0;
        int winboard[d][d];
        int inc = 1;
        int swap = 0;
        int k = 0;
        int l = 0;
        for (i = 0 ; i <= d -1 ; i++)
        {
            for (j = 0 ; j <= d -1; j++)
            {
                winboard[i][j] = inc++;
    
                printf("test %d\n", winboard[i][j]);
            }
        }
        // init winning board and compare to current board
        for (i = 0 ; i <= d -1 ; i++)
        {
            for (j = 0 ; j <= d -1; j++)
            {
                k= winboard[i][j];
                printf("%d\n", k);
                l= board[i][j];
                //printf("%d\n", l);
                if(winboard[i][j] == board[i][j])
                    count++;
                if (count == (d*d)-1)
                    return true;
    
            }
    
        }
        return false;
    }

    Here the output from the program :

    Code:
    test 1
    test 2
    test 3
    test 4
    test 5
    test 6
    test 7
    test 8
    test 9
    1
    2
    4
    4
    5
    7
    7
    8
    9
    You ll see nicely filled but the read out ??
    Last edited by lamko; 08-03-2011 at 10:15 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your arrays are of size d-1 as you say, then your loops go way too far.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Where does your d variable come from?
    You can't just make them up and use them without initialization...

    Even if d is correctly initialized you are using it incorrectly...
    Code:
    int winboard[d-1][d-1];  // your board is now one too small
    
    for (i = 0 ; i <= d -1 ; i++)  // and what is this about?
    Your for() loop is going to overflow your array boundaries.

    I'm thinking you need to re-read the section on arrays in your tutorial/textbook...

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As tabstop said:
    Code:
    bool
    won(void)
    {
    
        int count = 0;
        int winboard[d-1][d-1];
        int inc = 1;
        int swap = 0;
        int k = 0;
        int l = 0;
        for (i = 0 ; i <= d -1 ; i++)
        {
            for (j = 0 ; j <= d -1; j++)
    In C arrays begin at zero. Can you see your problem?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You are running past the array bounds.

    Code:
    int winboard[d-1][d-1];
    The smallest is winboard[0][0] the largest is winboard[d-2][d-2]

    You are using winboard[d-1][d-1] which does not exist.

    Tim S.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    d i'll put in as program argument which is between min val 3 and max val 9. I'm not totally understanding what you mean by overflowing the array. I'm going to check that out.
    d are the dimensions of the array if I want an 8 puzzle. I'll input a 3 but arrays start at zero so it would correct it to start at 1.
    This is the winning board:

    0,0 0,1 0,2
    1,0 1,1 1,2
    2,0 2,1 2,2

    1 2 3
    4 5 6
    7 8 0

    Now the intialising board

    8 7 6
    5 4 3
    2 1 0
    Last edited by lamko; 08-03-2011 at 10:04 AM.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    14
    Quote Originally Posted by lamko View Post
    d i'll put in as program argument which is between min val 3 and max val 9. I'm not totally understanding what you mean by overflowing the array. I'm going to check that out.
    It means even tho you declare it as
    Code:
    int windboard[2] //2 elements in windboard[]
    you will only have index of 0 and 1 in the array.
    meaning that calling windboard[2] would be out of bounds.

    So, array index starts from 0 and ends at n - 1 when you have declared it like int a[n]

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    Quote Originally Posted by stahta01 View Post
    You are running past the array bounds.

    Code:
    int winboard[d-1][d-1];
    The smallest is winboard[0][0] the largest is winboard[d-2][d-2]

    You are using winboard[d-1][d-1] which does not exist.

    Tim S.
    If I fill in for d = 3
    winboard[2][2] as max size

    So I don't understand how do you come at winboard[d-2][d-2] as max size ??

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    Quote Originally Posted by kpark91 View Post
    It means even tho you declare it as
    Code:
    int windboard[2] //2 elements in windboard[]
    you will only have index of 0 and 1 in the array.
    meaning that calling windboard[2] would be out of bounds.

    So, array index starts from 0 and ends at n - 1 when you have declared it like int a[n]
    I thought with winboard[2] you get 3 elements

    I'll get it now I just should use d as array size that is what you mean.

    If updated the code in the starting post. I'll read some more on array because it's not exactly working with winboard[d][d]
    Last edited by lamko; 08-03-2011 at 10:17 AM.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lamko View Post
    I thought with winboard[2] you get 3 elements
    Think about this rationally... why on earth would asking for 2 elements get you 3?
    When you ask for 2 elements you get 2 elements... numbered 0 and 1 ...

    I'll get it now I just should use d as array size that is what you mean.
    There are more problems than that...
    You are overrunning the array bounds with every for() loop...

    Here... type this example up, run it and watch what comes out of it...
    Code:
    #include <stdio.h>
    #define SIZE 4
    
    int main (void)
      {
         int matrix[SIZE][SIZE];   // 4 x 4 element 2 dimensional array...
         int xpos,ypos;      // x and y matrix positions
    
         // fill the array with numbers
         for (xpos = 0; xpos < SIZE; xpos++)
           for (ypos = 0; ypos < SIZE; ypos++)
               matrix[xpos][ypos] = xpos * ypos;
    
         // display array content
         for (xpos = 0; xpos < SIZE; xpos++)
           for (ypos = 0; ypos < SIZE; ypos++)
               printf("matrix[%d][%d] = %d\n",xpos,ypos,matrix[xpos][ypos]);
    
       return 0; }

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by lamko View Post
    I thought with winboard[2] you get 3 elements
    No, you get exactly what you ask for in C. You asked for an array of 2 ints. Their indexes are 0 and 1.

    Quote Originally Posted by lamko View Post
    I'll read some more on array because it's not exactly working with winboard[d][d]
    Sounds like a good idea. Look at the link I posted.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    I've changed both nested for loops now to this :
    for (i = 0 ; i < d ; i++)
    for (j = 0 ; j < d; j++)

    It's working fine now.
    thx to all especially CommonTater for his example which made al clear !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread not playing nice; my array is full of zeros
    By voraciousveggie in forum C Programming
    Replies: 14
    Last Post: 03-16-2010, 10:36 PM
  2. Array fills with junk - help
    By Lucky Luke in forum C Programming
    Replies: 1
    Last Post: 12-13-2009, 05:13 PM
  3. Blending two or more radial gradient fills
    By pronecracker in forum Windows Programming
    Replies: 7
    Last Post: 05-27-2007, 11:39 PM
  4. Gradient fills
    By pronecracker in forum Windows Programming
    Replies: 5
    Last Post: 04-29-2007, 11:55 AM
  5. flood fills?
    By cozman in forum Game Programming
    Replies: 1
    Last Post: 04-30-2002, 11:23 PM