Searching 2d array for row and column

I'm trying to search a 2d array for a row and column entry. The array is arrayShots[BS_GRID_ROWS][BS_GRID_COLS] and the rows would be characters and the columns would be numbers. The constants are declared elsewhere, not within in my code. The rows are A through J and the columns are 1 through 10. Thus arrayShots[0][0] could have [A][1] and so on.

Each pass through my function I will add another combination of row and column to this array. Just before I do this I need to search through the array and make sure the combination I'm entering, is not already inside the array somewhere.

I have two questions:

1) How do I add each entry to the array.
2) How would I search through the array to ensure the entry is not already within the array?

The code below is where I'm at currently:

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
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int
main()
{

    int column;
    bool duplicate;
    char row;
    int arrayShots[BS_GRID_ROWS][BS_GRID_COLS];

// srand was already initialized
// randomize row/column entry for the array in combination of A to J and 1 to 10

do {

    duplicate = false;
    row = (rand() % 10) + 65;
    column = (rand() % 10) + 1;
    for (char rowCh='A'; rowCh <= 'A'+BS_GRID_ROWS-1; rowCh++)
      for (int col=1; col <= BS_GRID_COLS; col++){
         if (row,column == arrayShots[rowCh][col]
            duplicate = true;
  }
} (while duplicate == true);

arrayShots[row][column];

}


So essentially my questions come down to these two lines:

if (row,column == arrayShots[rowCh][col]
and
arrayShots[row][column]

The first line i want to check the array for the random row and column i genereted.
The second line I want to add the row and column I generated to the array.

Am I doing either of these correctly? Really appreciate any help.
Last edited on
Likely no, you are not.

- if (row,column == arrayShots[rowCh][col] this code makes no sense to me. First, you are missing the closing brace, second, you are probably misusing the comma. I can't even figure out what is it that you were intending to do.

- "check the array for the random row and column" This sentence makes no sense. What do you want to check for? Perhaps the existence of row and column?

- You can't add rows or columns to C arrays. The statement arrayShots[row][column]; doesn't do anything. Is this some PHP thing?
Last edited on
No, i'm trying to add each row and column I randomly generate with the code

1
2
row = (rand() % 10) + 65;
column = (rand() % 10) + 1;


So i need row and column, to be added to my array arrayShots.

I'm aware that
 
if (row,column == arrayShots[rowCh][col])

is not working. I'm asking how I can compare the row and column I generate, to values which are already in my array.
- You can't add rows or columns to C arrays.
- You can't compare rows or colums to a value in an array, because you can't compare a set of values to a single value. Like, try comparing (5,8,9) to the value 6.
I'm nopt trying to add rows and columns to the array, i'm trying to add the value of my variables row and column to the array.
Perhaps you mean: how to compare an index of a row, stored in variable row, to a value in an array. Well, that's easy, if you know which value from the array you want. Like:
if (row == array[0][3]) will compare the value of variable row to the value in the array at first row, fourth column.
Yes that is what i need to do, however I need to compare both values at the index, so say my row and column variables have A6 when randomly generated and those are entered into the array a [0][3]. Then later I generate A6 again, I need to search through the array for A6, and if it exists my loop will force the numbers to be randomly generated again.

Also the the array location is not known, hence my loops to search through the array.
Last edited on
Yes, I think I'm starting to understand you.

For the starters, you should know this: the 'cells' (i.e. elements) in the array exist from the moment when you have created the array.

So the statement:
int arrayShots[BS_GRID_ROWS][BS_GRID_COLS];
immediately creates the entire array with all the specified elements. The values of the elements are unknown at this moment (but each element does have a value).

So you cannot check whether a value in an array 'exists', because all elements always have a value. Instead, you can only check whether a value (of some array element) IS EQUAL to some specified value.


Last edited on
Yes however, I should be able to then add values to the array elements correct? I want to add the values of my row and column variables to the array, only if those values are not already within the array.

Those are the two pieces of the puzzle I don't understand. I understand looping through the array using an incremental variable to search each element [0][1], [0][2], etc. However I don't understand how to put values into those elements, or how to check the elements for existing values. I need to check two things, row & column, not just one.
Well, if I read this correctly... You could try a nested loop.

1
2
3
4
5
6
7
8
9
10
11
 Psuedo --
for x=0 to value
{
for y = 0 to value
{
if array [x][y] == whatever {
do something
}
whatever ++
}
}


Or something like that. That would run through the loop, set a simple compare, and allow you to take action.
Last edited on
rapalabrowns - you have some real issues with C++ vocabulary.

Ok, first, there seem to be two separate problems. First, whether the array contains a value equal to variable 'row', second, whether the array contains a value equal to variable 'column'.

so for example, to test for the first one, you should use:

if (row == arrayShots[rowCh-'A'][col])

I want to add the values of my row and column variables to the array, only if those values are not already within the array.

Now, the problem is that in your explanation, you didn't say to what element of the array to assign the value of 'row' (when the value of 'row' is not already in the array).

Last edited on
Did you initialize BS_GRID_ROWS,BS_GRID_COLS?
BS_GRID_ROWS and BS_GRID_COLS are initialized in another place.

Kevin C C++ is very new to me, as is programming in general, sorry for not being 100% on the vocab yet.

I think im going to leave this alone for now, thanks for trying.
Topic archived. No new replies allowed.