Help with maze/robot program

Hey everyone, I'm having a problem with my maze/robot program and i would appreciate if anyone could help me figure out why I'm getting the errors I'm getting. I've looked up the error codes and tried to figure this out on my own but i cant get it to even run yet. the code isn't finished yet, but im having trouble with passing by reference the array i initialized in void createMaze into my main()

here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE=10;
void createMaze(int maze[][SIZE], int& robotRow, int& robotColumn);
void displayMaze(int maze[][SIZE], int robotRow, int robotColumn);
void randomMove(int maze[][SIZE], int& robotRow, int& robotColumn);
void smartMove(int maze[][SIZE], int& robotRow, int& robotColumn);

int main()
{
int robotRow, robotColumn;
int maze[SIZE][SIZE];
createMaze(maze, robotRow, robotColumn);


}

void createMaze(int maze[][SIZE], int& robotRow, int& robotColumn)
{
robotRow=2;
robotColumn=1;
maze[SIZE][SIZE]=
{
{1,1,1,1,1,1,1,1,1,1},
{3,0,0,0,0,0,0,0,0,1},
{1,1,0,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,0,1,0,1,0,1},
{1,0,0,0,0,1,0,1,1,1},
{1,0,1,0,1,1,0,0,0,1},
{1,0,1,0,0,1,1,1,1,1},
{1,0,0,1,0,0,0,0,0,2},
{1,1,1,1,1,1,1,1,1,1}
};
}
void displayMaze(int maze[][SIZE], int robotRow, int robotColumn)
{
for(int row=0;row<SIZE;row++)
{
for(int col=0;col<SIZE;col++)
{
switch (maze[row][col])
{
case 0:cout << '#';
break;
case 1:cout << ' ';
break;
case 2:cout << '&';
break;
case 3:
};
cout <<maze[row][col] << " ";
};
cout << endl;
}
//void randomMove(int maze[][SIZE], int& robotRow, int& robotColumn)
//{
//
//}
//void smartMove(int maze[][SIZE], int& robotRow, int& robotColumn)
//{
//
//}
You had a few ; too many and one } was at the wrong place.
You also cant assign values to an array like you tried in createMaze. This synthax works only when you create the array. The code compiles and runs, now you need to fix the display.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>

using namespace std;

const int SIZE = 10;
void createMaze(int maze [][SIZE], int& robotRow, int& robotColumn);
void displayMaze(int maze [][SIZE], int robotRow, int robotColumn);
void randomMove(int maze [][SIZE], int& robotRow, int& robotColumn);
void smartMove(int maze [][SIZE], int& robotRow, int& robotColumn);

int main()
{
  int robotRow, robotColumn;
  int maze[SIZE][SIZE];
  createMaze(maze, robotRow, robotColumn);
  displayMaze(maze, robotRow, robotColumn);

}

void createMaze(int maze [][SIZE], int& robotRow, int& robotColumn)
{
  robotRow = 2;
  robotColumn = 1;
  int temp[SIZE][SIZE] =
  {
    { 1,1,1,1,1,1,1,1,1,1 },
    { 3,0,0,0,0,0,0,0,0,1 },
    { 1,1,0,1,1,1,1,1,1,1 },
    { 1,0,0,0,0,0,0,0,0,1 },
    { 1,1,1,1,0,1,0,1,0,1 },
    { 1,0,0,0,0,1,0,1,1,1 },
    { 1,0,1,0,1,1,0,0,0,1 },
    { 1,0,1,0,0,1,1,1,1,1 },
    { 1,0,0,1,0,0,0,0,0,2 },
    { 1,1,1,1,1,1,1,1,1,1 }
  };

  for (int row = 0; row < SIZE; row++)
    for (int col = 0; col < SIZE; col++)
      maze[row][col] = temp[row][col];
}
void displayMaze(int maze [][SIZE], int robotRow, int robotColumn)
{
  for (int row = 0; row < SIZE; row++)
  {
    for (int col = 0; col < SIZE; col++)
    {
      switch (maze[row][col])
      {
        case 0:
          cout << '#';
          break;
        case 1:
          cout << ' ';
          break;
        case 2:
          cout << '&';
          break;
      }
      cout << maze[row][col] << " ";
    }
    cout << endl;
  }
}
void randomMove(int maze [][SIZE], int& robotRow, int& robotColumn)
{

}
void smartMove(int maze [][SIZE], int& robotRow, int& robotColumn)
{

}
Awesome! thank you so much! i have a question though about how you fixed it.
1. how did the maze array i declared in line 15 know that you wanted to print the temp array in line 25 if it wasn't the same name?

Is it okay if i reply to this thread if i bump into more problems? i feel like the robot is going to give me trouble?
how did the maze array i declared in line 15 know that you wanted to print the temp array in line 25 if it wasn't the same name?


Line 15: maze is an uninitialized array.

Line 16: createMaze() is called passing maze as an argument. Lines 39-41 copy the temp array to maze. Upon exit from createMaze(), maze has been initialized to a copy of temp. At line 42, temp goes out of scope and no longer exists.

Is it okay if i reply to this thread if i bump into more problems? i feel like the robot is going to give me trouble?

Yes, it is preferred that you keep related problems in the same thread.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.