Question

When outputting the path found in the proposed program, the direction of movement is output. Howe...

When outputting the path found in the proposed program, the direction of movement is output. However, the direction of movement to the last movement, EXIT, is not output. To print this out, please describe how to modify the proposed program.

#include <stdio.h>
#define NUM_ROWS  5
#define NUM_COLS  3
#define BOUNDARY_COLS 5
#define MAX_STACK_SIZE 100
#define FALSE 0
#define TRUE 1
​
​
​
typedef struct {
    short int row;
    short int col;
    short int dir;
  } element;
​
element stack[MAX_STACK_SIZE];     /* global stack declaration */
​
​
typedef struct {
    short int vert;
      short int horiz;
  } offsets;
​
​
offsets move[9];   /* array of moves for each direction */
​
​
static short int maze[][BOUNDARY_COLS] = {{1,1,1,1,1},   /* top boundary  */
          {1,0,0,0,1},
          {1,1,1,0,1},
          {1,0,0,0,1},
          {1,0,1,1,1},
          {1,0,0,0,1},
          {1,1,1,1,1}};  /* bottom boundary */
​
short int mark[][BOUNDARY_COLS] = {{0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0},
          {0,0,0,0,0}};
​
int top;
void init_move();
void add(element);
element delete();
void stack_full();
void stack_empty();
void path();
void print_record(int,int,int);
void print_maze();
​
​
​
int main ()
{/* stack represented as an array */
  init_move();
  print_maze();
  path();
}
​
​
void init_move()
{/* initial the table for the next row and column moves */
   move[1].vert = -1;  move[1].horiz =  0;   /*  N */
   move[2].vert = -1;  move[2].horiz =  1;   /* NE */
   move[3].vert =  0;  move[3].horiz =  1;   /*  E */
   move[4].vert =  1;  move[4].horiz =  1;   /* SE */
   move[5].vert =  1;  move[5].horiz =  1;   /*  S */
   move[6].vert =  1;  move[6].horiz =  0;   /* SW */
   move[7].vert =  0;  move[7].horiz = -1;   /*  W */
   move[8].vert = -1;  move[8].horiz = -1;   /* NW */
}
​
​
void print_maze()
{/* print out the maze */
  int i,j;
  printf("Your maze, with the boundaries is: \n\n");
  for (i = 0; i <= NUM_ROWS+1; i++) {
    for(j = 0; j <= NUM_COLS+1; j++)
      printf("%3d",maze[i][j]);
    printf("\n");
  }
  printf("\n");
}
​
​
void stack_full()
{
   printf("The stack is full.  No item added \n");
}
​
​
void stack_empty()
{
  printf("The stack is empty.  No item deleted \n");
}
​
​
​
void add(element item)
{ /* add an item to the global stack
     top (also global) is the current top of the stack,
     MAX_STACK_SIZE is the maximum size */
​
   if (top == MAX_STACK_SIZE)
     stack_full();
   else
      stack[++top] = item;
}
​
​
​
element delete()
{ /* remove top element from the stack and put it in item */
  if (top < 0)
    stack_empty();
  else
    return stack[top--];
}
​
​
void print_record(int row, int col, int dir)
{ /* print out the row, column, and the direction, the direction
   is printed out with its numeric equivvalent */
       printf("%2d    %2d%5d", dir,row, col);
       switch (dir-1) {
       case 1: printf("    N");
        break;
       case 2: printf("    NE");
         break;
       case 3: printf("    E ");
         break;
       case 4: printf("    SE");
         break;
       case 5: printf("    S ");
         break;
       case 6: printf("    SW");
         break;
       case 7: printf("    W ");
         break;
       case 8: printf("    NW");
         break;
       }
       printf("\n");
}
​
​
void path()
{/*  output a path through the maze if such a path exists,
    the maze is found in positions 1 to NUM_ROWS and 1 to NUM_COLS.
    Rows 0 and NUM_ROWS+1 serve as boundaries, as do Columns
    0 and NUM_COLS+1. */
​
  int i, row, col, next_row, next_col, dir, found = FALSE;
  element position;
​
  mark[1][1] = 1;
  /* place the starting position, maze[1][1] onto the stack
     starting direction is 2  */
  top = 0;
  stack[0].row = 1;  stack[0].col = 1;  stack[0].dir = 2;
  while (top > -1 && !found) {
    /* remove position at top of stack, and determine if
  there is a path from this position */
    position = delete();
    row = position.row;  col = position.col; dir = position.dir;
    while (dir <=  8 && !found) {
    /* check all of the remaining directions from the current
    position */
      next_row = row + move[dir].vert;
      next_col = col + move[dir].horiz;
      if (next_row == NUM_ROWS && next_col == NUM_COLS)
      /* path has been found, exit loop and print it out */
  found = TRUE;
      else if ( !maze[next_row][next_col]
      &&  !mark[next_row][next_col]) {
      /* current position has not been checked, place it
   on the stack and continue */
   mark[next_row][next_col] = 1;
   position.row = row;   position.col = col;
   position.dir = ++dir;
   add(position);
   row = next_row;   col = next_col;   dir = 1;
       }
       else
   ++dir;
     }
   }
   if (!found)
     printf("The maze does not have a path\n");
   else {
   /* print out the path which is found in the stack */
     printf("The maze traversal is: \n\n");
     printf("dir#  row  col  dir\n\n");
     for (i = 0; i <= top; i++)
       print_record(stack[i].row, stack[i].col, stack[i].dir);
     printf("      %2d%5d\n",row,col);
     printf("      %2d%5d\n",NUM_ROWS,NUM_COLS);
   }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Modified the code and cleared the errors. Change the delete function name to deleteElement(). Everything works fine.

Program.cpp:

#include <stdio.h>
#define NUM_ROWS 5
#define NUM_COLS 3
#define BOUNDARY_COLS 5
#define MAX_STACK_SIZE 100
#define FALSE 0
#define TRUE 1
typedef struct {
short int row;
short int col;
short int dir;
}element;
element stack[MAX_STACK_SIZE]; /* global stack declaration */
typedef struct {
short int vert;
short int horiz;
} offsets;
offsets move[9]; /* array of moves for each direction */
static short int maze[][BOUNDARY_COLS] = {{1,1,1,1,1}, /* top boundary */
{1,0,0,0,1},
{1,1,1,0,1},
{1,0,0,0,1},
{1,0,1,1,1},
{1,0,0,0,1},
{1,1,1,1,1}}; /* bottom boundary */
short int mark[][BOUNDARY_COLS] = {{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}};
int top;
void init_move();
void add(element);
element deleteElement();
void stack_full();
void stack_empty();
void path();
void print_record(int,int,int);
void print_maze();
int main ()
{/* stack represented as an array */
init_move();
print_maze();
path();
}
void init_move()
{/* initial the table for the next row and column moves */
move[1].vert = -1; move[1].horiz = 0; /* N */
move[2].vert = -1; move[2].horiz = 1; /* NE */
move[3].vert = 0; move[3].horiz = 1; /* E */
move[4].vert = 1; move[4].horiz = 1; /* SE */
move[5].vert = 1; move[5].horiz = 1; /* S */
move[6].vert = 1; move[6].horiz = 0; /* SW */
move[7].vert = 0; move[7].horiz = -1; /* W */
move[8].vert = -1; move[8].horiz = -1; /* NW */
}
void print_maze()
{/* print out the maze */
int i,j;
printf("Your maze, with the boundaries is: \n\n");
for (i = 0; i <= NUM_ROWS+1; i++) {
for(j = 0; j <= NUM_COLS+1; j++)
printf("%3d",maze[i][j]);
printf("\n");
}
printf("\n");
}
void stack_full()
{
printf("The stack is full. No item added \n");
}
void stack_empty()
{
printf("The stack is empty. No item deleted \n");
}
void add(element item)
{ /* add an item to the global stack
top (also global) is the current top of the stack,
MAX_STACK_SIZE is the maximum size */
if (top == MAX_STACK_SIZE)
stack_full();
else
stack[++top] = item;
}
element deleteElement()
{ /* remove top element from the stack and put it in item */
if (top < 0) {
stack_empty();
}
return stack[top--];
}
void print_record(int row, int col, int dir)
{ /* print out the row, column, and the direction, the direction
is printed out with its numeric equivvalent */
printf("%2d %2d%5d", dir,row, col);
switch (dir-1) {
case 1: printf(" N");
break;
case 2: printf(" NE");
break;
case 3: printf(" E ");
break;
case 4: printf(" SE");
break;
case 5: printf(" S ");
break;
case 6: printf(" SW");
break;
case 7: printf(" W ");
break;
case 8: printf(" NW");
break;
}
printf("\n");
}
void path()
{/* output a path through the maze if such a path exists,
the maze is found in positions 1 to NUM_ROWS and 1 to NUM_COLS.
Rows 0 and NUM_ROWS+1 serve as boundaries, as do Columns
0 and NUM_COLS+1. */
int i, row, col, next_row, next_col, dir, found = FALSE;
element position;
mark[1][1] = 1;
/* place the starting position, maze[1][1] onto the stack
starting direction is 2 */
top = 0;
stack[0].row = 1; stack[0].col = 1; stack[0].dir = 2;
while (top > -1 && !found) {
/* remove position at top of stack, and determine if
there is a path from this position */
position = deleteElement();
row = position.row; col = position.col; dir = position.dir;
while (dir <= 8 && !found) {
/* check all of the remaining directions from the current
position */
next_row = row + move[dir].vert;
next_col = col + move[dir].horiz;
if (next_row == NUM_ROWS && next_col == NUM_COLS)
/* path has been found, exit loop and print it out */
found = TRUE;
else if ( !maze[next_row][next_col]
&& !mark[next_row][next_col]) {
/* current position has not been checked, place it
on the stack and continue */
mark[next_row][next_col] = 1;
position.row = row; position.col = col;
position.dir = ++dir;
add(position);
row = next_row; col = next_col; dir = 1;
}
else
++dir;
}
}
if (!found)
printf("The maze does not have a path\n");
else {
/* print out the path which is found in the stack */
printf("The maze traversal is: \n\n");
printf("dir# row col dir\n\n");
for (i = 0; i <= top; i++)
print_record(stack[i].row, stack[i].col, stack[i].dir);
printf(" %2d%5d\n",row,col);
printf(" %2d%5d\n",NUM_ROWS,NUM_COLS);
}
}

Output:

Select C:WINDOWS SYSTEM321cmd.exe Your maze, with the boundaries is: 1 e 0 0 1 1 1 1 0 1 1 e 0 0 1 1 0 1 1 1 1 e 0 0 1 The ma

Add a comment
Know the answer?
Add Answer to:
When outputting the path found in the proposed program, the direction of movement is output. Howe...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • This is java. Goal is to create a pacman type game. I have most of the...

    This is java. Goal is to create a pacman type game. I have most of the code done just need to add in ghosts score dots etc. Attached is the assignment, and after the assignment is my current code. import java.awt.Color; import java.awt.Dimension; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Maze extends JFrame implements KeyListener { private static final String[] FILE = {...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • I have to write a C program to assign seats on each flight of the airline’s...

    I have to write a C program to assign seats on each flight of the airline’s only plane (capacity: 40 seats, in 10 rows). For the sake of simplicity, assume that each row has 4 seats labeled A, B, C, D. Your program should display the following menu of alternatives: Please type 1 for "first class" Please type 2 for "business class" Please type 3 for “economy class”. If the person types 1, then your program should assign a seat...

  • Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static...

    Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver {    public static String solve(char[][] grid) {        // TODO        /*        * 1. Construct a graph using grid        * 2. Use BFS to find shortest path from start to finish        * 3. Return the sequence of moves to get from start to finish        */               // Hardcoded solution to toyTest        return...

  • Instructions Write a program in Java that implements the A* algorithm to find a path from...

    Instructions Write a program in Java that implements the A* algorithm to find a path from any two given nodes. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan method for calculating the heuristic. Remember: your heuristic function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT