Thread: Need help with array, finding position of a value.

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    Need help with array, finding position of a value.

    So for a project I'm working on, I'm using an array and generating it's values randomly but unique. Currently I'm working on a 3X3 array and the generated values are in the range from 1-9. So I wrote a function that will tell me the position of the cell whose value is 9.
    This is the function I wrote:
    Code:
    void Llogaritje1(int t[3][3],int &i, int &j){
        int y,l;
        for(y=0;y<3;y++){
            for(l=0;l<3;l++){
                if(t[y][l]==9){
                    i=y;
                    j=l;
                    break;
                }
            }if(t[y][l]==9) break;
        }
    }
    But it doesn't work on all cells. Seems like at cells t[1][0] and
    t[2][0] the values that i and j take are 0 0 since when I print them after excecuting the function that's what it returns. I really don't understand why. Any help?
    Last edited by Marko2961; 12-24-2013 at 11:50 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose it might "seem" that way, but it isn't that way. If 9 doesn't appear anywhere, then i and j will have the same values they started with. If a 9 is found, then i and j will have the coordinates of the first one found. Is there a reason to believe 9 must exist in the box? If so, then I would be checking the part where the random numbers were assigned, if it were me.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Yeah, as I forgot to mention earlier(now edited), the values are unique, so basically the array would have all the values from 1 to 9. So there is 100% a 9 in there. And this happens only when the value nine is in the cell t[1][0] and t[2][0].
    Also here is a picture of an example.
    Need help with array, finding position of a value.-untitled-png
    Now I could solve it in another way, but I really don't understand what's wrong?
    Last edited by Marko2961; 12-24-2013 at 12:05 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ah, I see the problem, which is I failed to count the braces. That seemingly-redundant line 10 is still inside the big for-loop (but after the little for-loop). So, for instance, after the first row, it checks whether t[0][3] equals 9. Now t[0][3] is not exactly a valid space in the array, but due to the pointer arithmetic it refers to t[1][0]. This then triggers the break before the variables can be assigned.

    You should probably just get rid of that if statement altogether.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    Okay, thanks for the help. I'll solve it by putting its values in a vector and then finding its pos. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-17-2011, 06:21 AM
  2. Finding mouse coordinates without disturbing its position
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 10-21-2008, 11:19 AM
  3. finding a position in stl map
    By kolistivra in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2007, 11:21 AM
  4. Get Position in array
    By mag_chan in forum C Programming
    Replies: 7
    Last Post: 10-08-2005, 04:35 PM

Tags for this Thread