maze in an array

Here is the problem once i initialise the n x n area of maze in array ,i cannot correctly specific the exact location of the robot starting point to move

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
#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

int main()
{
    int n,column,row;
    char square[n][n];
    
    cout <<"Set the value of n x n squares area:"<<endl;
    cout <<"Enter the value for first n:";
    cin >> n;
    for(int x=0;x<n;x++)
    {
            for(int y=0;y<n;y++)
            {
            square[x][y]='#';
            }
            }
    for(int x=0;x<n;x++)
    {cout<<setw(7);
    for(int y=0;y<n;y++)
    {
    cout << square[x][y];
    }
    cout<<endl;
    }
    
    cout <<"Now specific the robot starting point to move:"<<endl;
    cout <<"Enter which column:(starting from 0)";
    do{
    cin >> column;
    if(column > (n-1))
    cout<<"The column size is more than the square areas,please enter again:";
    }while(column>(n-1));
    
    cout <<"Enter which row:(starting from 0)";
    do{
    cin >> row;
    if(row > (n-1))
    cout<<"The column size is more than the square areas,please enter again:";
    }while(row >(n-1));
    
    cout<<"The row is "<<row<<endl;
    cout<<"The column is "<<column;
    
    square[column][row]='*';
    for(int x=0;x<n;x++)
    {cout<<setw(7);
    for(int y=0;y<n;y++)
    {
    cout << square[x][y];
    }
    cout<<endl;
    }
    
    
    
            
            
            
    
    
    
    
    
    getch();
    return 0;
    
}


the final output did not correctly match the location of starting point of column and row.For example when i key in the value of 2 for both row and column,
the * symbol which use as a robot would emerge in several places.Why?
The major problem that I see with your code is the way how you are declaring your char array. n is un-initialized, that means n will have some garbage value in it at the time when you declare char square[n][n];. And you cannot change size of array once you have declared it. If you want to declare an array according to the user input either declare it dynamically or use vectors.
Could you please show me how to declare the 2 dimensional vector?i'm really running out of idea even refered several information online.The most critical problem right now is that the array do not let me change the value in it unless initialize it.Any alternate path like clear the value in a specific element of the array ?
- screw multidimentional arrays.
- screw nested vectors (such as vector< vector<char> >)

Both of these are incredibly hard to work with and are a huge pain. I don't know why so many people flock to them.

Use a resizable 1D array (or simple vector) and resize it appropriately. For example if you want an array that's 5x6, resize your array to be 5*6 elements in size, and access elements with (y*width)+x:

1
2
3
4
5
6
7
8
9
10
11
12
13
vector<char>  myMap;

// put in dimensions:
int width = 8;
int height = 10;

myMap.resize( 10*8 );

// to get an element at x=3, y=5:
int x = 3;
int y = 5;

char el = myMap[ (y*width) + x ];



Or, to make it even easier, you can put this all in a class:

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
// I use 'char' as the data type here, but ideally it's probably best to
//  template it.  But to keep it simple I didn't make it a template

class Array2D
{
public:
    // constructor
    Array2D(int wd = 0,int ht = 0)
      : nWd(wd)
      , nHt(ht)
      , mVec(wd*ht)
    { }

    // resizing
    void Resize(int width,int height)
    {
        nWd = width;
        nHt = height;
        mVec.resize( width*height );
    }

    // accessing an element at x,y coords
    inline char& operator () (int x,int y)
    {
        return mVec[ (y*nWd) + x ];
    }

    inline const char& operator() (int x,int y) const
    {
        return mVec[ (y*nWd) + x ];
    }

    // getting current dimensions:
    inline int GetWidth() const     { return nWd; }
    inline int GetHeight() const    { return nHt; }

private:
    // private data members
    int                nWd;   // width
    int                nHt;   // height
    std::vector<char>  mVec;  // the actual data
};


//----------------------------------------------------
//  Then to use the class:

Array2D myMap(80,60);  // create a 80x60 array

myMap(5,10) = 'b';     // set column 5, row 10 to 'b' 
Topic archived. No new replies allowed.