1

In this program, there is a 6 x 6 map constructed using 2 dimensional array. The aim of this program is to navigate an object through the obstacles. Each coordinate contains a value, each value represents the distance between the object and its destination if it is to be the object's new coordinate. The initial coordinate of the object is (0,5) and its destination coordinate is (5,0). Value 50 means an obstacle. The object should choose the next coordinate that contains the smallest value. The value of the destination coordinate is 0. This is the map:

   0 | 1 | 2 | 3 | 4 | 5
0| 7 | 50| 3 | 2 | 1 | 0
1| 6 | 5 | 4 | 3 | 2 | 1
2| 7 | 6 | 5 | 50| 50| 50
3| 8 | 7 | 6 | 7 | 8 | 9
4| 50| 50| 50| 50| 50| 10
5| 0 | 15| 14| 13| 12| 11 

The program is suppose to output a series of coordinates from (0,5) to (5,0).

#include<stdio.h>
#include<stdlib.h>

//These are the functions to move the object through the map.
void moveForward(int new_c[1][2]);
void moveBackward(int new_c[1][2]);
void moveLeft(int new_c[1][2]);
void moveRight(int new_c[1][2]);

int main()
{   
    int map[6][6]={{7,50,3,2,1,0},
                   {6,5,4,3,2,1},
                   {7,6,5,50,50,50},
                   {8,7,6,7,8,9},
                   {50,50,50,50,50,10},
                   {0,15,14,13,12,11}};

    int coordinate[1][2]={{0,5}};
    int x = 0;
    int y = 5;

    printf("(%d,%d),",x,y);

    while(x!=5&&y!=0)
    {           
        if(map[y-1][x] < map[y+1][x] && //Forward < Backward
           map[y-1][x] < map[y][x-1] && //Forward < Left
           map[y-1][x] < map[y][x+1] && //Forward < Right
           y - 1 >= 0 && 
           y + 1 <= 5 && 
           x - 1 >= 0 && 
           x + 1 <= 5)
           {
               moveForward(coordinate);
               y = y - 1;
            }
            else
            {     
                if(map[y+1][x] < map[y-1][x] && //Backward < Forward
                   map[y+1][x] < map[y][x-1] && //Backward < Left
                   map[y+1][x] < map[y][x+1] && //Backward < Right
                   y - 1 >= 0 &&
                   y + 1 <= 5 &&
                   x - 1 >= 0 &&
                   x + 1 <= 5)
                   {
                       moveBackward(coordinate);
                       y = y + 1;
                   }
                   else
                   {      
                       if(map[y][x-1] < map[y][x+1] && //Left < Right
                          map[y][x-1] < map[y+1][x] && //Left < Backward
                          map[y][x-1] < map[y-1][x] && //Left < Forward
                          y - 1 >= 0 &&
                          y + 1 <= 5 &&
                          x - 1 >= 0 &&
                          x + 1 <= 5)
                          {
                              moveLeft(coordinate);
                              x = x + 1;
                          }
                          else
                          {     
                              if(map[y][x+1] < map[y][x-1] && //Right < Left
                                 map[y][x+1] < map[y+1][x] && //Right < B
                                 map[y][x+1] < map[y-1][x] && //Right < F
                                 y - 1 >= 0 &&
                                 y + 1 <= 5 &&
                                 x - 1 >= 0 &&
                                 x + 1 <= 5)
                                 {
                                     moveRight(coordinate);
                                     x = x - 1;
                                 }
                           }
                   }
            }
    }
return 0;
}

void moveForward(int new_c[1][2])
{   //This modifies the y coordinate.
    new_c[0][1] = new_c[0][1] - 1;
    printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}

void moveBackward(int new_c[1][2])
{   //This modifies the y coordinate.
    new_c[0][1] = new_c[0][1] + 1;
    printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}

void moveLeft(int new_c[1][2])
{    //This modifies the x coordinate.
    new_c[0][0] = new_c[0][0] - 1;
    printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}

void moveRight(int new_c[1][2])
{   //This modifies the x coordinate.
    new_c[0][0] = new_c[0][0] + 1;
    printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}



Output:
(0,5),

Then the cursor just blinking like usual. There's no statement on the bottom telling returned value or execution time.

7
  • if(map[y][x+1]<map[y][x-1]&&map[y][x+1]<map[y+1][x]&&map[y][x+1]<map[y-1][x]&&y-1>=0&&y+1<=5&&x-1>=0&&x+1<=5) That looks pretty wrong to me. Do you actually know what this line of code is supposed to do? I sure don't, and I'd delete it and start over. Commented May 6, 2016 at 17:01
  • 1
    did you try stepping through the code with a debugger? Commented May 6, 2016 at 17:04
  • Your loop is gone into infinite loops somewhere -- you are probably not meeting any of the conditions in the if-statements -- try to singlestep the program using a debugger and see what is wrong. Commented May 6, 2016 at 17:05
  • maybe trying to refactor your code a little bit will help. Also, what is the program supposed to output? be more explicit... Commented May 6, 2016 at 17:05
  • The object should choose the next coordinate that contains the smallest value. What if there are more than 2 small values? Commented May 6, 2016 at 17:08

1 Answer 1

1

Your code accesses parts outside the map. For example, if x is 0, you access map[y][x-1]. This is undefined behavior and probably leads to the infinite loop you're experiencing.

You have && x - 1 >= 0 in your conditions. So if x == 0, all conditions will always be false and therefore coordinate is never changed. As x starts with 0, nothing ever happens.

Another edit: And while I'm at it, while(x!=5&&y!=0) is also wrong. The loop will stop as soon as you reach the correct column or row. You want while(x != 5 || y != 0).

Sign up to request clarification or add additional context in comments.

9 Comments

What about the condition "&& x - 1 >= 0" that I had imposed in the if statement? Is it wrong? In terms of syntax or anything?
Well I didn't see it because your code is awfully formatted. But even with that, your code accesses memory areas it shouldn't, so it's still undefined behavior.
@Hiew: Not wrong, but your code lacks formating to enhance readability. Also for complex expressions, it is often good to add parenthesis, even iff they are not required.
I added explanation for the >= you mentioned.
Do not try to do too much things in one if statement. Rewrite your code.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.