# 2 dimensional array question

• 10-15-2006
Roy01
2 dimensional array question
I racked my brain for a day and can't figure it out. It is extremely frustrating but I just can't give up.

Suppose a bingo is build with an nXn array like this.

11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55

n can be any integer and its value is input by users.

Here are my questions:

1. I want to define 2 arrays so that

int array1[n][n];
int array2[n][n];

You know n is unknown. It is just illegal to do that. Any trick can do it ?

2.
The entries of the table is input by users.
A row is entered at a line.
eg.
11 12 13 14 15
21 22 23 24 25
How to end a line after "cin>>"?

3.
This is really one big headache for me.
After inputting all entries in the table, users choose number randomly. If a chosen number match with an entry in a table, the entry is clear. When a row , column or diagonal is clear, the game is ended.

Here is my idea:
array1 is used to store the value of the entries in the table.
array2 is used to store the value that determine whether the entries in the table is clear or not .
If an entry (array1[row][col]) in the table is clear, then array2[row][col]=1.
Else, array2[row][col]=0.

I just can't figure out how to write the condition that a row , column or diagonal is clear. Any idea? Does my idea work?

Plz save my life.
• 10-15-2006
robatino
As far as 1) goes, you don't really need a 2D array - you can use a 1D array of length n^2, and allocate it using new:
Code:

`int *array = new int[n*n];`
• 10-15-2006
Daved
>> It is just illegal to do that. Any trick can do it ?
You use a dynamic array. In C++ the best way to do that is usually the C++ vector class. So in this case, you can use a vector of vectors, or you can use a single vector with the trick robatino mentioned. You can also use a dynamic C style array with new if you want. To create a two dimensional array of that type, you would have an array of n pointers, then use a for loop to allocate n arrays of int.

>> How to end a line after "cin>>"?
If you know how large n is at that time, just use a for loop to read them in, assuming the input is valid the end of the line will skipped correctly. If you don't know what n is, then you could read the line in as a string with getline, then use a stringstream to get each int value out. Or, after each read with cin>>, you could check the next character to see if it is a space ' ' or a newline '\n'.
• 10-15-2006
twomers
What robatino said is right, but if you want to dynamically create a 2d array, use something like this -
Code:

```int **Allocate_2D_Array( int x, int y ) {         int **Array = new int *[y];         for ( int i=0; i<y; i++ )         {                 Array[i] = new int [x];         }         return Array; } void Dealocate_2d_array ( int **Array, int y ) {         for ( int i=0; i<y; i++ )         {                 delete []Array[i];         }         delete []Array; } int main( void ) {         int x, y;         // Get your user defined array dimensions         cout<< "Enter X: ";         cin >> x;         cout<< "Enter Y: ";         cin >> y;         // Allocate memory         int **p_2d_array = Allocate_2D_Array( x, y );         // Fill array with variables         for ( int j=0; j<y; j++ )         {                 for ( int i=0; i<x; i++ )                 {                         p_2d_array[j][i] = i+j;                 }         }         // print out array         for ( int j=0; j<y; j++ )         {                 for ( int i=0; i<x; i++ )                 {                         cout<< p_2d_array[j][i] << " ";                 }                 cout<< '\n';         }         // delete allocated memory         Dealocate_2d_array( p_2d_array, y );         return 0; }```
Pre Edit: STL would be better, I think. There is little to no error checking here, and it would be better if you made functions of both the allocation stage and deallocation stage to make it neater.

Post Edit: Here's some functions. I thought I'd not make a really long post, and just edit over the old one.
• 10-15-2006
robatino
In Dealocate_2d_array, after delete[]'ing all the Array[i]'s, it should finally delete[] Array itself. Also, in Allocate_2D_Array, if Array is allocated with length y+1 and then zero-terminated, then you don't need to pass y to Dealocate_2d_array - it can just stop the loop when it reaches the final 0. Keeping track of these kind of details is why I hate using actual n-dimensional arrays. Of course, if the rows are of different lengths, there's no choice.
• 10-15-2006
twomers
I rarely use new/delete/delete[] to be honest - STL works well for my purposes. Good call on the missing delete[], robanino. Just edited it in there.
• 10-16-2006
Roy01
Thank you, guys.
Sorry, I wish I could understand what you said totally. I just find out that I am not allowed to use pointer or dynamic array for this assignment because I haven't been taught these yet......Now I am still stuck here. :/Any other possible way to define array with unknown parameter?
• 10-16-2006
twomers
Make a very big array, and only use some of it. Have you been given a maximum size?
• 10-16-2006
Roy01
Quote:

Originally Posted by twomers
Make a very big array, and only use some of it. Have you been given a maximum size?

Yes. Thanks for your hint! Now I can move on.:D
• 10-17-2006
Roy01
I am stuck again. I wonder how to identify the position of each entry in the table when the entries occupied in the 2D array of a certain size is unknown? Is it possible to use array to do this? I really have no idea. Please at least give me a hint.
• 10-17-2006
Loctan
Quote:

Originally Posted by Roy01
I am stuck again. I wonder how to identify the position of each entry in the table when the entries occupied in the 2D array of a certain size is unknown? Is it possible to use array to do this? I really have no idea. Please at least give me a hint.

I am sorry, maybe it's late, but I don't really understand your question. Are you referring to robatino's suggestion of having a 1 dimensional array correspond to the "real" two dimensional array? That should be relatively easy using integer division and the modulo operator if that's what you're asking.
• 10-17-2006
Roy01
In this case, this is a bingo game. The size of the table and all entries are input by users. In order to write code that make the compiler know when a row , column or diagonal is clear, I need to make the compiler to identify the position of each entry in the table. It is very complicated since the size of the table is unknown. Still, no progress. :(
• 10-17-2006
jafet
Well, using a 1D array, you can access the 2D coordinate (x, y) with the formula array[x * side_length + y]. Using this, you can:

Code:

```for each row and column and diagonal     loop, check each square in row/column/diagonal         if there is an uncrossed square ++foo;     if foo = 0 then the line is cleared if foo is never = 0 then the game continues```
You could write three similiar code blocks; one checks rows only, one checks columns only, one checks the diagonals.
• 10-22-2006
Roy01
Thank you all of you.
jafet, you are brilliant. Your suggestion inspired me.
Now the code is almost done.
• 10-22-2006
Roy01
control flow problem
My code has logical error. I have problem with understanding the control flow of my code.
This is the code that check if a row is clear.
When the value of row in the outermost loop is 2, what is the value of row inside the innermost loop(the one inside the array with_star)?
Is my code too complicated?

Code:

```for (row=0;row<size1;row++)                 {                         for(col=0,x=0;col<size1;col++)                         {                                 if (mark_off[row][col]==true)                                         ++x;                         }                         if (x==size1)                                 {                                                clear_row=true;                                 if        (clear_row==true)                                 {                                         for (col_star=0;col_star<size1;col_star++)                                                 {                                                         with_star[row][col_star]=true;//mark the entries in the row with star                                                 }                                 }                                                                                 break;//if a row is clear,exit the loop                                 }                         else clear_row=false;                 }```