How can I set-up a 2-dimensional Array?

For my simple robot I want to store direction-and-distance in an array:
//Pseudo-code:
int move[dir][dist]; // dir and dist will both be integers

but I can't figure how to set it up.
Whatever I have tried, I get this error message from IDE:
Simple_robot:31: error: declaration of 'move' as multidimensional array must have bounds for all dimensions except the first

which I just don't understand.

Can anyone guide me, please.

Geoff

It would help if you posted the offending code, although I suspect perhaps that you are trying to define a 2D array with variables declaring the size of the dimensions, which the compiler does not like. This does not compile:

int a=5;
int b=6;

int move[a][b];

This does:

const int a=5;
const int b=6;

int move[a][b];

Thanks, Wildbill,

What I hope to do is read a direction from a keypad, and a distance from a (second) keypad, and store them as a 'move'. Repeat for the next move, etc. Then 'playback' the series of moves.

At first I tried:

int move[][] = {0,1,2,3},{999,999,999,999};
representing the known directions 0,1,2 or 3, and some arbitrary numbers in the 2nd dimension.

Error message, as in my first post,
Simple_robot:31: error: declaration of 'move' as multidimensional array must have bounds for all dimensions except the first

In your code, what do 'a' and 'b' represent? I don't really want constants, do I?

And to make it worse, I've just realised I shall need 3-dimensions as in move[1][dir][dist] etc.

Geoff

int move[][] = {0,1,2,3},{999,999,999,999};

The compiler needs to know at least the "stride" (row length) of an array.
Besides it would have to be

int move[][4] = {{0,1,2,3},{999,999,999,999}};

(extra braces)

Brilliant AWOL,
That did it!
Kop khun mag krab, merci bien and thanks as well,

Geoff

Whoops! I shouted too soon. Whilst I can now create the array-object, I'm stuck with entering data to it.

// array [moveNr][dir][distance]
int move[][1][1] = {{},{0},{999}};

And now I want to set the 'dist' into the last part of array-item number 'moveNr'

void how_far()
{
 if(readNumPadNow ==1)
 	{
 		num_key_in = read_numberPad(x);
		do
		{
			x ++;						// x==0 is already taken by the LCD keypad
			num_key_in = read_numberPad(x);  		// read the numeric pad
			if(x>3)
				{x=0;}
	 	} while( num_key_in <=180 );
	 		readNumPadNow =0;
	 		dist = calculate_Number(x, num_key_in);
	 		move[moveNr][][2] = dist;  // Which will not compile
	 		x =0;
 	}
}

The error I get is this

Simple_robot.cpp: In function 'void how_far()':
Simple_robot:87: error: incompatible types in assignment of 'int' to 'int [1]'

Then somewhere else I want to set the 'middle' part of the same item to 'dir' which can be 0 or 1 or 2 or 3 (as N, E, S, W)
Geoff

Of course it won't compile - you're missing the second array index

You can't set an entire row/column/plane of an array except at initialization time. After initialization is complete, you can only assign values one element at a time, unless you really know what you are doing (and the array is organized correctly for your purposes).

I think a 2D array will do for your purposes, given that you just want for each move to store direction and distance:

int Moves[10][2];

You can store ten moves in this case, direction for move 5 will be stored in Moves[4][0], distance in Moves[4][1].

Hi Paul,
I think that is exactly what I want to do ...

 // initially, move[] is empty as in
 move[0][0]0;
 move[1][0][0];
.
.
 move[whatever][0][0];
// later to be filled, one dollop at a time
 so move[0].. becomes move[0][not-yet-set][42];
// later, when I have ascertained it from the keypads, it becomes move[0][3][42];

but I can't work out how to enter the (new) data?

Geoff

// initially, move[] is empty as in
move[0][0]0;

should be as in:

move[0][0][0] = 0;

// later to be filled, one dollop at a time
so move[0].. becomes move[0][not-yet-set][42];

You can't do that. If not-yet-set means you don't know where to store the value, you can't store it.

Perhaps, it's time to back up and explain what you want to do, not how you think you need to do it.

I'm attempting to make a robot, which young, non-geek children can 'program' to navigate wherever. Each move will be entered e.g. Forward 42; Left 90; Forward 8 ... and so on. The direction part will come from the keypad on the DFRobot LCD-keypad Shield, and the distance part from my own, home-made, numeric pad.
If all the moves are 'stored' in an array, they can be 'played-back' by stepping through it:-
e.g.
moveNr, dir, dist
First Move : Right, 42 (stored as 0 2 42)
Second move: Fwd, 19 (stored as 1 0 19)

(Much) later, I hope to code so that the child/user can Edit their moves, i.e. Move#13 should have been Left, Distance is OK.

Geoff

That's a two dimensional array at most - or a vector or one-dimensional if you use an array of "struct"s instead of the second dimension.

Thanks, All of you. Despite AWOL's advice

or a vector or one-dimensional if you use an array of "struct"s instead of the second dimension.

whatever it meant, I think I've got my head around it at last:

 at appropriate places I 'learn' what values are wanted:-
    tempDir = 0;  // (or 1, or 2, or 3)
    tempDist = 42;
....
 then, when user is happy, they press 'select' (or maybe I'll call it 'OK'), which triggers
   Moves[moveNr][0] = tempDir;
   Moves[moveNr][1] = tempDist;
   moveNr ++;

Now I can move on to the next task in this project.

Thanks again,

Geoff