How to transfer variables from a 1d array to a 2d?

As topic asks, how do I transfer the varaibles located in my 1d array to a 2d array? Currently, my code does not seem to transfer the codes (either that or my cout is wrong). I am sure that my 1d array has variables and stuff, but it is not passing it into the 2d array.

My buffer variables are not here, but rest assured, they have values in them.

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
35
36
void main (void)
{
  int cols =61;
  int rows =20;
  char buffer [1220];

  char ** maze = new char * [rows];	 //create array using pointers and double pointer
  for (int r = 0; r < rows; r ++)		//ditto
	{
	  maze[r] = new char [cols];	//same
	}

  int y=0;
  int x=0;

  for (int i=0;i<1220;i++)	//transfer buffer values to maze
  {
	  if (x != 61)
		{
		  maze[y][x] = buffer[i];
		  x++;
		}
		else
		{
		  y++;
		  x=0;
                }
  }
  for (rows=0; rows<0;rows++)
	  {
		for (cols = 0; cols <20; cols++)
		{
			cout <<maze [rows][cols];
		}
	  }
}

Last edited on
Line 29 rows<0?

Note: cols and rows hold size of the arrays, but then you overwrite them in 29-35.
Note: you don't set any maze[y][0], where 0<y.

Consider also division and modulo int row = i / 61; int column = i % 61;, or use the nested loops for transfer too.
Poor me :( Line 29 was a mistake by me.

So I rectified it, as well as put new intergers for cols and rows when using cout.

I also don't understand your second note. I did set y=0 right before I set x. I don't see any 0<y conditions anywhere either.

However, I get a bunch of gibberish when I try to cout.

My buffer's variable is actually taken from an external text file using get(). I have a suspicion that it could be because it also takes in the newline character, but I'm not sure if it'll mess with my 2d array like so.

My entire code is as follows
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{
	
	int cols =61;
	int rows =20;
	char buffer[1220];
	ifstream Maps;
	Maps.open ("Map01.txt");  //open file
	if(!Maps){	//check if file is open
		cout<< "error";
	}
	
		Maps.get(buffer, sizeof(buffer), '!');	//Retrieve character value and give it tobuffer
		


	char ** maze = new char * [rows];	 //create array using pointers and double pointer
	for (int r = 0; r < rows; r ++)		//ditto
	{
		//maze[r] = new char [cols];	//same
	}

	int y=0;
	int x=0;

	for (int i=0;i<1220;i++)	//transfer buffer values to maze
	{
		if (x != 61)
		{
			maze[y][x] = buffer[i];
			x++;
		}
		else
		{
			y++;
			x=0;
		}
	}
	for (int i=0; i<60;i++)
	{
		for (int o = 0; o <20; o++)
		{
			cout <<maze [i][o];
		}
	}

	cout<<endl;

	//cout<<buffer<<endl;

	system ("pause");
}
Last edited on
On the second note:
1. Lets be at i==60, y==0, x=60. x!=61, so maze(0,60) = buffer(60) and both i and x increment to 61.

2. Now x==61, y increments to 1, x resets to 0, and i increments to 62.

3. i==62, y==1, x==0, so x!=61 and maze(1,0) = buffer(62).

Now, did you notice how on step 2 the buffer(61) was not stored anywhere?


Why do you use magical numbers on your lines 38 and 40? In line 17 you had rows elements in maze. In line 42 maze seems to have 60 elements. Does it? No.
Topic archived. No new replies allowed.