Your assignment is to modify the program so that the aimless walker stumbles around in a 2-dimensional grid, such as the streets of Manhattan. Your program should prompt the user to enter the number of rows and columns in the 2-d grid. Again, have the walker start in the middle of the grid, then have him stumble randomly in 4 directions, and end the simulation when he falls off the end of the grid.

Thats my assignment, and this is what ive done so far, but i keep getting array errors and im not sure what to do. can anyone help?

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
void display(char grid[]);


int rowSize;
int colSize;
int curRow=rowSize/2;
int curCol=colSize/2;

void makeNextmove (int curRow , int curCol , int newRow , int newCol)
{
	int coin=rand()%4;
		if (coin==1)
			(newRow=curRow-1,newCol=curCol);
		if (coin==2)
			(newRow=curRow,newCol=curCol+1);
	return;
}

int main(){

char x; cin>>x;
char street;

cout << "Please enter the number of rows: ";
	cin >> rowSize;
cout << "Now please enter the number of columns: ";
	cin >> colSize;


bool done=false;
while (!done)
{
	makeNextmove(curRow,curCol,newRow,newCol);
	street[curRow][curCol]= " ";
	street[newRow][newCol]= "D";
	if (newRow==0||newRow==rowSize||newCol==0||newCol==colSize)
		done=true;
	curCol=newCol; curRow=newRow;
}

Your program should prompt the user to enter the number of rows and columns in the 2-d grid.

since you will have the user define the dimensions of your 2d array, you should use a dynamic 2d array:

int rows = 0;
int cols = 0;

cout << "Enter rows: ";
cin >> rows;
 
cout << "Enter cols: ";
cin >> cols;

//Dynamic 2D array
//1st dimension memory allocation:
char** grid = new char*[rows];

for(int i=0; i<rows; i++)
{
     //2nd dimension memory allocation:
     grid[i] = new char[cols];
}

//Now you have grid[][] of dimensions determined by the user

You can either statically use arrays of an arbitrary amount, or you can dynamically allocate array memory.. but you can't do both:

int rows = 0;
int cols = 0;

cout << "Enter rows: ";
cin >> rows;
 
cout << "Enter cols: ";
cin >> cols;

//No bueno   :'(
grid[rows][cols];

street is not declared as an array. However, even if you were to declare it as such, you must declare the rows and columns at compile time. Some compilers may allow you to declare street on line 33 (technically after the time when row and column should have values) but this is a non-standard behavior
The alternative is to allocate the array dynamically (See: http://www.codeproject.com/KB/cpp/arrayDinamic.aspx). It just takes a few lines to do so.

EDIT: CP beat me to it ^^^^^^

I fixed up my code some and now the program runs, but it crashes after entering the rows and columns. Im not sure why, why is it crashing and not finding the number of steps?

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
void display(char grid[]);

int rowSize;
int colSize;
int curRow=rowSize/2;
int curCol=colSize/2;
int newRow;
int newCol;



void makeNextmove (int curRow , int curCol , int &newRow , int &newCol)
{
	int coin=rand()%4;
		if (coin==1)
			(newRow=curRow-1,newCol=curCol);
		if (coin==2)
			(newRow=curRow,newCol=curCol+1);
	return;
}

int main(){

int rowSize = 0;
int colSize = 0;
int stepNum = 0;


cout << "Please enter the number of rows: ";
	cin >> rowSize;
cout << "Now please enter the number of columns: ";
	cin >> colSize;

char** grid = new char*[rowSize];

for (int i=0; i<rowSize; i++)
{ 
	grid[i] = new char[colSize];
}

bool done=false;

while (!done)
{
	
	makeNextmove(curRow,curCol,newRow,newCol);
	grid[curRow][curCol]= ' ';
	grid[newRow][newCol]= 'D';
	if (newRow==0||newRow==rowSize||newCol==0||newCol==colSize)
		done=true;
	curCol=newCol; curRow=newRow;
	
	

}
	cout<< "Steps to finish: "<< stepNum; 
	cout<< "\n";
    return 0; 
	
}
void display(char grid[]){
    for (int i=0;i<rowSize;i++) cout<<grid[i];
    cout << endl;
}

I fixed up my code some and now the program runs, but it crashes after entering the rows and columns. Im not sure why, why is it crashing and not finding the number of steps?

It crashes? Then it doesn't run, does it? :icon_wink:
You should know by now that details are important -- what kind of crash?

void makeNextmove (int curRow , int curCol , int &newRow , int &newCol)
{
	int coin=rand()%4;
		if (coin==1)
			(newRow=curRow-1,newCol=curCol);
		if (coin==2)
			(newRow=curRow,newCol=curCol+1);
	return;
}

1) comma format is confusing. Use full statements.

void makeNextmove (int curRow , int curCol , int &newRow , int &newCol)
{
    int coin=rand()%4;
    if (coin==1)
    {
        newRow=curRow-1;
        newCol=curCol;
    }
    if (coin==2)
    {
        newRow=curRow;
        newCol=curCol+1;
    }
    return;
}

2) if curRow = rowsize-1 and you get 2, what value is newRow? Is that a valid position?

My teacher told us that we had to reference newRow and newCol and so i did. But then the program wont run. A windows dialog box appears saying the program must end now. However when i remove the references the program does not crash, but it also doesnt simulate the drunk walking or count the steps.

Put some cout statements in strategic places and figure out exactly where it is crashing. Also, reread what WaltP has written about going out of bounds on your array. I would pull in those variables from global scope on 8-13 and work them into main. Global scope causes too much confusion for that to be helpful. Pass what you need to between functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.