Problem Testing values of 2D Array


Why is the conditional detecting values that are NOT defined in the initalized 2d array ....?


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

  //INITIALIZATION of 2d ARRAY
int validtracks[17][2] = {{1,1}, {1,3}, {1,6},{2,3},{2,6},{3,4},{3,6},{3,8}, 
{4,2},{4,3},{4,7},{5,3},{5,5},{6,1}, {6,3},{7,1},{7,3}};





//main code
while (matchtrack == 0) 
{
     rndint = rand() % 7 + 1;

     for (int r = 1; r < 18; r++)
        {

        if (validtracks[r][2] == rndint && validtracks[r][1] ==   
            rndtracks[1] && matchtrack == 0)
             {
             matchtrack = 1;
              }

        }
}







I have narrowed the problem down to the conditional:

1
2
3
4
if (validtracks[r][2] == rndint && validtracks[r][1] ==rndtracks[1] && matchtrack ==0)
            {
            matchtrack = 1;
            }




For some reason it's detecting a match with elements in the array, when it shouldn't be:
1
2
3
//initialized array
int validtracks[17][2] = {{1,1}, {1,3}, {1,6},{2,3},{2,6},{3,4},{3,6},{3,8}, 
{4,2},{4,3},{4,7},{5,3},{5,5},{6,1}, {6,3},{7,1},{7,3}};
First off:

for (int r = 1; r < 18; r++) is going out of bounds on the array, arrays start at 0 and end at size-1, so in your case validtracks[0->16][0->1].

if (validtracks[r][2] just like above is also going out of bounds on the array.
Hello jpz79,

Welcome to the forum.

megatron 0 has a good point although I differ with the "size - 1" part it should be "r < size;" Where in this case "size" is 17. Remember that C++ as with other languages are zero based meaning things like arrays, lists, vectors and others start at zero for the first element. with the for loop starting at "1" you miss the first element of the array.

As I look at your code I have more questions for now. What is "rndtracks[1]"? Where is it defined and how is it populated? The same questions for "matchtrack".

For some reason it's detecting a match with elements in the array, when it shouldn't be:
What is matching and what is not? This could come from the for loop starting at "1" instead of zero throwing everything off by one.

There is not enough of the program there for testing and answering. What you may think is a problem with the for loop may actually start somewhere else and just show up at the for loop.

To find a real answer I need to know the value of "validtracks[r][2]", "rndint", "rndtracks[1]" and " matchtrack" only then can you figure out what is going on.

As a suggestion this can help you visualize the 2D array better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int validtracks[17][2] =
{
	{ 1, 1 },  // <--- Row zero.
	{ 1, 3 },
	{ 1, 6 },
	{ 2, 3 },
	{ 2, 6 },
	{ 3, 4 },
	{ 3, 6 },
	{ 3, 8 },
	{ 4, 2 },
	{ 4, 3 },
	{ 4, 7 },
	{ 5, 3 },
	{ 5, 5 },
	{ 6, 1 },
	{ 6, 3 },
	{ 7, 1 },
	{ 7, 3 }  // <--- Row 16.
};


Hope that helps,

Andy
Hi Andy,

When I said "size - 1" I didn't mean in the for loop, though I probably should have stated that. I meant to say the last element you can retrieve will be 1 less than the size stated in the [] operator upon declaration. So in this persons case the last element they could retrieve from int validtracks[17][2]; would be validtracks[16][1].

My apologies for the misunderstanding Andy,
Last edited on
@ megatron 0,

I see your point now, but in the beginning I thought you were talking about the for loop. That is why I got the wrong idea.

No worries it happens.

Andy
Topic archived. No new replies allowed.