# Help with my program

• 09-12-2003
Glirk Dient
Help with my program
First, I will explain what this program does.

This program is meant to find the quickest route around a "maze"

The maze is a grid 3 columns, 4 rows.

The object is to go to each point, without going the same way, but some points you can go through twice.

The problem with my program is it is making illegal jumps(ie. its at 1,1 and goes to 3,2..skipping many numbers.) and when it does that is messed up coords.

coords[cx][cy] is a double array that keeps track of where it can move, and where it cant. cx = points[x][y](cuurent point) and cy = nextpoint[ax][ay].

I will attach my program so hopefully someone can figure out why it is making illegal jump and can fix it.
• 09-12-2003
quzah
You have an array 3x4 for your maze, that's fine. How come you're giving it out of bounds starting points?
Code:

```int startingpoint(int &x,int &y) {                 x = (rand()%4)+1;         y = (rand()%3)+1;                 return x,y; }```
rand()%4 gives you 0 to 3. That's what you want. Your arrays start at zero and go to size-1. Thus, for your "4" size, you want to just use rand()%4. For your "3" size, you just want rand()%3.

Second, since you're passing by reference, why are you bothering to return anything?

Third, you cannot return more than one value. This should have been one of the very first things you learned when learning about funtions. So stop doing it.

Fix all of that first, and see what it gets you.

Quzah.
• 09-12-2003
Glirk Dient
Doesn't adding +1 at the end of rand() make it 1 to 4?
• 09-12-2003
swoopy
When I run your code, startingpoint returns x=3, y=2, so this is where it starts. Then in directionfunct, direction = 3. However ax and ay don't change, because the if() statement:
if(coords[cx][cy] == 0 || coords[cy][cx])
is always true, because cx and cy are always 1, and coords[1][1] is probably 0 (although you never initialized it, so it could be anything). Then
cx becomes points[3][2] = 7
cy becomes nextpoint[3][2] = 8
• 09-13-2003
Glirk Dient
Thanks for pointing out the if() statement error.

The other stuff is meant to happen.

The way I have the grid set up for me is 1,1 is the lower right, and it counts left to right, bottom to top, so the bottom left is 1, and the top right is 12.

That is what cx and cy mean, they are the point its at, and the point it is moving to.

So when it goes a direction, it takes away from coords[cs][cy] so it cannot go that way again.
• 09-13-2003
HybridM
Quote:

Doesn't adding +1 at the end of rand() make it 1 to 4?
Yes, but if you're using them as array co-ordinates you have a problem. Arrays start at 0, so if you had a four element array its elements are 0-1-2-3 not 1-2-3-4, so if you generate a 4 and use it on a 4 element array, you're going out of bounds and chaos will ensue.
• 09-13-2003
Glirk Dient
But points and nextpoint are [5][4].
• 09-13-2003
Glirk Dient
Here is the updated code.