Thread: 2 dimensional array question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Question 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.
    Last edited by Roy01; 10-15-2006 at 03:10 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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];

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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'.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.
    Last edited by twomers; 10-15-2006 at 03:39 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    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?

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Make a very big array, and only use some of it. Have you been given a maximum size?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    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.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    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.
    Last edited by Roy01; 10-17-2006 at 12:44 AM.

  11. #11
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    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.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    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.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thank you all of you.
    jafet, you are brilliant. Your suggestion inspired me.
    Now the code is almost done.
    Last edited by Roy01; 10-22-2006 at 06:44 AM.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    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;
    		}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  2. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  3. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  4. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  5. Create Array size Question
    By popohoma in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2002, 03:04 AM