8 Replies - 1042 Views - Last Post: 20 January 2010 - 11:59 AM Rate Topic: -----

#1 Guitartripp   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 20-May 09

Problem initializing array

Posted 20 January 2010 - 09:52 AM

I have these two functions:

void tictactoe( char game[][2] )
{
	x = false;
	move = 0;

	int n = 49;
	for( int r = 2; r >= 0; r-- )
	{
		for( int c = 0; c <= 2; c++ )
		{
			game[r][c] = n;
			n++;
		}
	}
}

And

void drawInterface( char game[][2] )
{
	cout << " --- --- --- " << endl
		<< "| " << game[0][0] << " | " << game[0][1]
		<< " | " << game[0][2] << " |" << endl;

	cout << " --- --- --- " << endl
		<< "| " << game[1][0] << " | " << game[1][1]
		<< " | " << game[1][2] << " |" << endl;
	cout << " --- --- --- " << endl

		<< "| " << game[2][0] << " | " << game[2][1]
		<< " | " << game[2][2] << " |" << endl
		<< " --- --- --- " << endl;
}


Basically, the board SHOULD look like
 --- --- ---
| 7 | 8 | 9 |
 --- --- ---
| 4 | 5 | 6 |
 --- --- ---
| 1 | 2 | 3 |
 --- --- ---


But for some reason, the 1 is a 6, and the 4 is a 9. I can't figure out why this is happening.

It looks like the first column is replaced with the last column of the row before it.

Could anybody help me out as to why this is happening?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem initializing array

#2 Paul-   User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 260
  • Joined: 11-December 09

Re: Problem initializing array

Posted 20 January 2010 - 10:31 AM

The error must be in a different place. According to the first function, the "game" array should be filled with numbers from 49 and up.
Was This Post Helpful? 0
  • +
  • -

#3 Guitartripp   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 20-May 09

Re: Problem initializing array

Posted 20 January 2010 - 10:34 AM

But game[][] is a char, so when 49 goes in, it converts to 9.
Was This Post Helpful? 0
  • +
  • -

#4 Linkowiezi   User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: Problem initializing array

Posted 20 January 2010 - 11:00 AM

I'd guess, your 'game' array is to small.

Try 'game[][3]' instead.

...although, I'd probably rather do '*game' since I kinda dislike multidimensional arrays, but that's just me...

The last usable spot in that array will then be 2, now it's 1, but you can still access whatever you want(which you do), although you might end up changing something you don't want to change, like a "4 into a 9" or a variable some other program on your computer is using.

But if that's not the problem then you have to cough up some more code.
Was This Post Helpful? 0
  • +
  • -

#5 Guitartripp   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 20-May 09

Re: Problem initializing array

Posted 20 January 2010 - 11:07 AM

That didn't work either.

The functions are actually from a class. I'll post the code for my 3 files so somebody can check those.

tictactoe.h
class tictactoe
{
private:
	char game[2][2];
	bool x;
	int move;
public:
	tictactoe(); // DEFAULT CONSTRUCTOR
	void drawInterface( tictactoe game[][2] );
	char playerChoice();
	void updateArray( tictactoe game[][2] );
	void searchArray( tictactoe game[][2], bool x, int move );
	int winCheck( tictactoe game[][2] );
};


tictactoe.cpp
#include "tictactoe.h"
#include <iostream>
using namespace std;

// CONSTRUCTOR - INITIALIZES VARIABLES AND GAME ARRAY
tictactoe::tictactoe()
{
	x = false;
	move = 0;

	int n = 49;
	for( int r = 2; r >= 0; r-- )
	{
		for( int c = 0; c <= 2; c++ )
		{
			game[r][c] = n;
			n++;
		}
	}
}

void tictactoe::drawInterface( tictactoe arr[][2] )
{
	cout << " --- --- --- " << endl
		<< "| " << game[0][0] << " | " << game[0][1]
		<< " | " << game[0][2] << " |" << endl;

	cout << " --- --- --- " << endl
		<< "| " << game[1][0] << " | " << game[1][1]
		<< " | " << game[1][2] << " |" << endl;
	cout << " --- --- --- " << endl

		<< "| " << game[2][0] << " | " << game[2][1]
		<< " | " << game[2][2] << " |" << endl
		<< " --- --- --- " << endl;
}


hwk1.cpp
#include "tictactoe.h"
#include <iostream>

using namespace std;

int main()
{
	tictactoe game[2][2];
	tictactoe tictactoe;

	tictactoe.drawInterface( game );
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Problem initializing array

Posted 20 January 2010 - 11:18 AM

Your board's size is 2 x 2 but you start your row count at 2. Array indexes go from 0 -size-1. So you need to start at 1, the only viable indexes are 1 and 0 for size 2.
Was This Post Helpful? 0
  • +
  • -

#7 Linkowiezi   User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: Problem initializing array

Posted 20 January 2010 - 11:48 AM

So, with all of the code I believe my guess was correct, your 'game' array is to small.

You have to declare your array 'game[3][3]' if you're gonna use the 'game[2][2]' section of it, otherwise it doesn't exist in this array.

'game[3][3]' says you can use 'game[0][0]' up to 'game[2][2]' always one less than the number you have allocated from the start and this is because you start counting from 0, so you actually have 3, you have 0, 1 & 2 this equals 3 slots of information.

Hope this was of any help. :)
Was This Post Helpful? 0
  • +
  • -

#8 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Problem initializing array

Posted 20 January 2010 - 11:50 AM

you create an array [2][2] but you example shows that you want a tic_tac_toe board of a [3][3]; you dont have enough rows and columns to create a tic tac toe board.

anyways if you passing your board to a function to initialize it your only creating an initialized board with in the scope of that function. You need to send a pointer to your function so your changing the actual value of the data at that memory location.

btw: using vectors would make this more simple

look at code below
		


This post has been edited by ImaSexy: 20 January 2010 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#9 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Problem initializing array

Posted 20 January 2010 - 11:59 AM

sorry this may be a better example, passing the pointer to the function rather then returning a pointer to a local variable

#include <iostream>

using namespace std;

void create_board(char **game_board)
{
	char i = '1';
	for(int row = 2; row>=0; row++)
	{
		game_board[row] = new char[3]; //create a new column
		
		for(int col = 0; col<3; col++)
		{
			game_board[row][col] = i;
			i++;	 //incriment by 1
		}
	}
}

void delete_board(char **board)
{
	for(int i=0; i<3; i++)
		delete[]board[i];
	delete[]board;

}
int main()
{
	char **board = new char*[3]; //create new board
	create_board(board);
	
	//output new board
	for(int row = 2; row>=0; row++)
	{	
		for(int col = 0; col<3; col++)
		{
			cout<<game_board[row][col];
		}
		cout<<endl;
	}
	
	cin.ignore();
	cin.get();
	return 0;
}
		


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1