Thread: Help with my program

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    361

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Doesn't adding +1 at the end of rand() make it 1 to 4?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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
    Last edited by swoopy; 09-12-2003 at 07:52 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    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.

  6. #6
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    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.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    But points and nextpoint are [5][4].

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Here is the updated code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM