# Reversing output in this function

• 02-06-2008
rogster001
Reversing output in this function
I have posted a couple of threads relating to this program, Astar pathifinding, it is now complete and i just need to sort out this final function.

It works fine at the moment, this function plots the shortest path found, backwards from the target square to the start square, and at present draws it too with circles, what i need is to somehow save this path back and then draw it from the start square to the target square.

in the earlier functions on each loop the current best path square is stored in ParentSQ[] the value stored is the number of the grid on the board found by 'down * maxacross + across'
then the adjacent 8squares are filled with this value in ParentGrid[cgY][cgX]so they 'point' to the centre square.
so reversing back from target i take the last value in ParentSQ[] (PlotXY) and turn it into cgY and cgX co-ordinates with
cgY = PlotXY / MXDWN , cgX = PlotXY - MXACR * cgY;
then i see what is stored in ParentGrid[cgY][cgX] then i take this value (PlotXY) and turn it into cgY, cgX etc etc till i get to start square.

Code:

```ShowPath(int PathSearch,int ParentGrid[MXDWN][MXACR],int ParentSQ[MXDWN * MXACR],int cgY,int cgX) { int count = 0; int PlotXY = 0; char nextgo;               PlotXY = ParentSQ[PathSearch];               cgY = PlotXY / MXDWN;               cgX = PlotXY - MXACR * cgY;               setfillstyle(1,15);                 for(count = PathSearch; count > -1; count--)                 {                   if(ParentGrid[cgY][cgX] == 0) count = 0;                   circle((cgX * MXACR)+ 10,(cgY * MXDWN)+ 10,8);                   PlotXY = ParentGrid[cgY][cgX];                   cgY = PlotXY / MXDWN;                   cgX = PlotXY - MXACR * cgY;                                     nextgo = getch();                 } return(0); }```
• 02-06-2008
oogabooga
If you are just looking to reverse your list of coords being used to draw the circles, just put a push(x,y) instruction right after the circle() call. Then after the for loop, have another loop to pop() back the coords. Something like:
Code:

```#define MAX_STACK 100   typedef struct Coord {     int x, y; } Coord; Coord stack[MAX_STACK];   int push( int x, int y ) {     // save in stack, return 0 if array exceeded, otherwise 1 }   int pop( int *x, int *y ) {     // store top of stack into *x, *y     // return 0 when empty, 1 otherwise }```
• 02-06-2008
rogster001
hey thanks, i have no idea what the push and pop means but will look them up, how is it that x & y can both be stored seperately in the same array element tho? If i had known that was possible i could have saved myself some hassle!
• 02-06-2008
oogabooga
Look up the "stack" data structure.
Push adds elements to the top of the stack.
Pop removes them in reverse order (last in, first out).

You can use a struct to gather x and y together, or you could use two separate arrays if you wish.
• 02-07-2008
rogster001
cheers buddy, that will open a few doors in general methinks!
• 02-07-2008
Instead of an array holding just an integer in each element, it's now holding a struct which contains one x and one y number.

Array's of structs are very useful, indeed. Access to each part of the struct can be done using the dot operator.
• 02-07-2008
rogster001
hokay, is this similar to an array element containing another array? which was something i had forgotten about incidentally, i will have to look up this dot operator malarkey... is it a bit like in objects .... cat.meow etc? i recall this as an tutorial example... cat object with meow attribute or something.
• 02-07-2008
laserlight
Quote:

is this similar to an array element containing another array?
In a way, yes, though there is a layer of abstraction not present with a two dimensional array. Instead of thinking "I have an array of two element arrays" you now think "I have an array of Coords".

Quote:

i will have to look up this dot operator malarkey... is it a bit like in objects .... cat.meow etc?
Yes, structs are a (the?) way by which one can do object based programming in C.
• 02-07-2008
rogster001
man, this sounds just the ticket, i think this will let me rewrite a fair chunk of code into a much clearer and efficient method... meant to get into structures and start on more C++ object orientated stuff before now, but have always had other options, this project at the moment is throwing problems at me that are making me learn new material, which is fine by me... one step closer to launching my world changing operating system that sweeps all before it... hee!!!! ;->