# Thread: arrays

1. ## arrays

using
Code:
```int ExecuteCommand(int com[], int max)
{
int Tfloor[FLOORSIZE][FLOORSIZE];

int i, move, penup, pendown, n, s, e=1, w;
for(i=0;i< max;i++)
{
switch (com[i]) {
case 1:
cout << "penup\n";
penup=1;
break;

case 2:
cout << "pendown\n";
pendown =1;
break;

case 3:
cout << "right\n";
w=1;
break;

case 4:
cout << "left\n";
e=1;
break;

case 5:
// case 5 .. uses
//com[i + 1] to get the value of how many spaces to move..
// assuming i know wat direction i wanna go..
// i put an if statement.. and then how do i fill an array..
// i have to convert pendown to 1s and pen up to 0..
// do i make sense....
Tfloor[FLOORSIZE][FLOORSIZE]=; // how do i fill an array..
DisplayFloor(Tfloor, FLOORSIZE)
break;
}

}
}```
how do i convert the above to something like this and put it into
int Tfloor[FLOORSIZE][FLOORSIZE] = {1, 1, 1, 1, 1,
0, 0, 0, 0, 0};
until the end of the loop..

its similar to turtle graphics

ta..

2. i dont quite understand. does tFloor[][] represent a geometric plan, a 2D space, with 2 dimensions? what is the purpose of this routine? you should be more specific.

3. tFloor is a 2d array..
when u combine the pen down and movement of the turtle.. for eg..

6x5 array grid.. the ones represent the movement of the turtle.
011110
010010
010010
010010
011110

4. i think im beginning to understand. you have a 2 dimensional area represented by a 2d array. you have a number of variables which tell you the current status of movement, drawing, etc.

if im right, your routine reads in a list of commands from com[], and based on those commands records movement in the form of ones and zeros.

assuming that's correct, my personal opinion would be to leave it in the command format, since that will tell you the complete set of movements, whereas your array wont, because it cant tell how many times the 'turtle' has moved onto a particular square.

but if you're really fussed, ill give it a shot

ill post it when im done

5. actually, i realized while making the program that i had no idea of what i was trying to make really. give us a hand here!

6. lol @ chizzy.

Your not at Uni are you? Doing PP2 by any chance?

7. fry - yes
benny..
umm..
all im having trouble with is filling the array..
lets say one command is for the turtle to move 15 spaces across..

using an if statement i will be able to determine whther the pen is up or down..(whether a 1 or 0 is needed) the only trouble i have is filling the 2d array...

thanks

8. You have to keep track of the x and y coordinates of the turtle.
And you use these to change the array values.

For example (this is simplified to be more general):
Code:
```if(pen_down && need_to_move){
Tfloor[current_x][current_y] = 1;
}```
It really depends on how you have implemented the code so far. Another option would be to store in the current x and y, as well as the destination. Then you use a loop such as this:

Code:
```for(int offset=0; offset < spaces_to_move; offset++){
if(move_left)
Tfloor[current_x - offset][current_y] = 1;
if(move_right)
Tfloor[current_x + offset][current_y] = 1;
if(move_up)
Tfloor[current_x][current_y - offset] = 1;
if(move_down)
Tfloor[current_x][current_y + space] = 1;
}```
There are several other ways to do this as well, i have just shown two, and you should be able to see how it works. Not too difficult. (Btw, i haven't actually started the assignment yet, but feel free to send a PM if you have any more questions )

9. thanx. im goin try that out ...

Popular pages Recent additions