Member Avatar for format_c

can any one identify any problems in my code?
it keeps crashing once it reaches the else condition.

void path(char paths[][70], int sROW, int sCOL, int fROW , int fCOL) //recursive path function 
{
	
	if (paths[sROW][sCOL]=='E')	
		{
		for(int i=0; i<20; i++)
		{
			cout<<endl;
			for(int j=0;j<70; j++)
			{
				cout<<paths[i][j]; // display final maze with path
			}
		}
	return;
	}
	
	else 
	
		if (paths[sROW][sCOL]==' ' && paths[sROW+1<=19][sCOL])//if path is blank charater move up once space
			paths[sROW+1][sCOL]='+'; //mark position
			path(paths,sROW+1,sCOL,fROW,fCOL); // call path function
		

		if (paths[sROW][sCOL+1]==' ' && paths[sROW][sCOL+1<=69])//if path is blank charater move one space to the left
			
			paths[sROW][sCOL+1]='+'; //mark position
			path(paths,sROW,sCOL+1,fROW,fCOL); // call path function
		

		if (paths[sROW-1][sCOL]==' ' && paths[sROW-1>=0][sCOL])//if path is blank charater move down space to the left
		
			paths[sROW-1][sCOL]='+'; //mark position
			path(paths,sROW-1,sCOL,fROW,fCOL); // call path function
		

		if (paths[sROW][sCOL-1]==' ' && paths[sROW][sCOL-1>=0])//if path is blank charater move one space to the right
		
			paths[sROW][sCOL-1]='+'; //mark position
			path(paths,sROW,sCOL-1,fROW,fCOL); // call path function
		
	
	
	

	
} //end function

"Crashing" how? What line number?

Lines 21, 27, 33, 39 are indented, yet not part of any if statement. Are they supposed to be? If so, you need some brackets.

why are you putting a conditional inside the array index?

if (paths[sROW][sCOL]==' ' && paths[sROW+1<=19 /*this doesn't make sense*/][sCOL]
Member Avatar for format_c

i made a few changes and now im receiving a stackover flow exception

here are the changes to the recursion

void path(char paths[][70], int sROW, int sCOL, int fROW , int fCOL) //recursive path function 
{
	//if ((sROW==fROW)&&(sCOL==fCOL)) //if start == finish
	if (paths[sROW][sCOL]=='E')	
		{
		//for(int i=0; i<20; i++)
		//{
		//	cout<<endl;
		//	for(int j=0;j<70; j++)
		//	{
		//		cout<<paths[i][j]; // display final maze with path
		//	}
		//}
		cout<<"\nSUCCESS!!";
		return ;
	}
	
	else {
	
		if (paths[sROW][sCOL]==' ' && sROW+1<20)//if path is blank charater move up once space
		{sROW++;
		paths[sROW][sCOL]='+';}
		//paths[sROW+1][sCOL]='+'; //mark position
			//path(paths,sROW+1,sCOL,fROW,fCOL); // call path function
		
		if (paths[sROW][sCOL+1]==' ' && sCOL<70)//if path is blank charater move one space to the left
		{sCOL++;
		paths[sROW][sCOL]='+';}
		//paths[sROW][sCOL+1]='+'; //mark position
			//path(paths,sROW,sCOL+1,fROW,fCOL); // call path function
		

		if (paths[sROW-1][sCOL]==' ' && sROW-1>=0)//if path is blank charater move down space to the left
		{sROW--;
		paths[sROW][sCOL]='+';}
			//paths[sROW-1][sCOL]='+'; //mark position
			//path(paths,sROW-1,sCOL,fROW,fCOL); // call path function
		

		if (paths[sROW][sCOL-1]==' ' && sCOL-1>=0)//if path is blank charater move one space to the right
		{sCOL--;
		paths[sROW][sCOL]='+';}
			//paths[sROW][sCOL-1]='+'; //mark position
			//path(paths,sROW,sCOL-1,fROW,fCOL); // call path function
	
		//paths[sROW][sCOL]=='+';
	path(paths,sROW,sCOL,fROW,fCOL);
	}
	
	

	
} //end function

Stick some debugging statements in there. You need to find out how many times this function is being called before returning, exactly where you crash, variable values, etc.

Get any random number generator seeds based on time out of there. If you are using a RNG, seed it with a constant. You want the ability to reproduce results exactly.

Member Avatar for format_c

this is the complete code

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

using namespace std;

void coord();
void startfinish( char arr[][70], int startx, int starty, int finshx , int finishy ); //start finish function prototype 
void path(char paths[][70], int sROW, int sCOL, int fROW , int fCOL); //path recursion prototype

int main()// begin program
{
	srand( (unsigned)time( NULL ) );// seed generator

	char maze[20][70]={}; // declare and initiliaze 20x70 maze array
	

	int row=20; //column variable initilized to 20 
	int col=70; //row variable initilized to 70
	int rowstart; //variable to hold path start x coordinate
	int rowfinish;// variable to hold path finish x coordinate
	int colstart; //variable to hold path start y coordinate
	int colfinish; //variable to hold path finish y coordinate

	int i; //i counter
	int j; //j counter


	for (i= 0; i<row; i++) //column for loop
	{
		cout<<endl;
		for( j =0; j<col; j++) //row for loop
		{
			int x=rand()%2; //random number generator devided by 2 and remainder passed to x
				
			if (x==0) //if remainder = 0
			
				maze[i][j]=' '; //pass blank charater to current position in array
			
			else
			
				maze[i][j]='#'; //else pass hash charater to current position in array
				
				cout<<maze[i][j]; //display maze to screen

		
			
		}
		
	}
	
	cout<<endl;
	cout<<"-------------------------------------------------------------------------\n";
	
	
	
	cout<<"Enter path starting and finishing points x and y:"<<endl; //enter paths begining and end
	cout<<"Starting row (accross): "<<endl; 
	cin>>rowstart; //get start point x from user
	
	cout<<endl;
	
	cout<<"Starting col (down): "<<endl;
	cin>>colstart; // get start point y from user
	
	cout<<endl;
	
	cout<<"Finish row: "<<endl;
	cin>>rowfinish; // get finish point x from user
	
	cout<<endl;
	
	cout<<"Finish col: "<<endl;
	cin>>colfinish; // get finish point y from user
	cout<<endl;
	
			
	startfinish(maze,rowstart,colstart,rowfinish,colfinish); //call start finish function holding array and start and finish coordinates
	
	for(int i=0; i<20; i++)
		{
			cout<<endl;
			for(int j=0;j<70; j++)
		{
				cout<<maze[i][j]; // display final maze with path
			}
		}
	cout<<endl;
	
	return 0;

}// end program


void startfinish( char arr[][70], int startrow, int startcol, int finishrow , int finishcol ) //start finish fucntion
{
	
	if (arr[startrow][startcol]==' ' && arr[finishrow][finishcol]==' ') //if start and finish = blanck charater
	{
		arr[startrow][startcol]='B'; //let start hold B
		arr[finishrow][finishcol]='E'; // let finish hold E
		
		for(int x=0; x<20; x++)
		{
			cout<<endl;
			for(int y=0; y<70; y++)
			{
				cout<<arr[x][y];//display array with Begin and Finish points
			}
		
		}
		cout<<endl;
		path(arr,startrow,startcol,finishrow,finishcol); //call path function holding array and coordiantes

	}
	else //(arr[startrow][startcol]=='#' && arr[finishrow][finishcol]=='#'); //if start and finish = #
	{
		
		cout<<"Error no start point found please rerun program to try again"<<endl; //exit 
	
	}	
	
	return ;
	

}	// end function



void path(char paths[][70], int sROW, int sCOL, int fROW , int fCOL) //recursive path function 
{
	//if ((sROW==fROW)&&(sCOL==fCOL)) //if start == finish
	if (paths[sROW][sCOL]=='E')	
		{
		for(int i=0; i<20; i++)
		{
			cout<<endl;
			for(int j=0;j<70; j++)
			{
				cout<<paths[i][j]; // display final maze with path
			}
		}
		cout<<"\nSUCCESS!!";
		return ;
	}
	
	else 
	{
		if (paths[sROW][sCOL]==' ' && sROW+1<20)//if path is blank charater move up once space
		paths[sROW+1][sCOL]='+';
		//paths[sROW+1][sCOL]='+'; //mark position
		path(paths,sROW+1,sCOL,fROW,fCOL); // call path function
		
		if (paths[sROW][sCOL+1]==' ' && sCOL<70)//if path is blank charater move one space to the left
		paths[sROW][sCOL+1]='+';
		//paths[sROW][sCOL+1]='+'; //mark position
		path(paths,sROW,sCOL+1,fROW,fCOL); // call path function
		

		if (paths[sROW-1][sCOL]==' ' && sROW-1>=0)//if path is blank charater move down space to the left
		paths[sROW-1][sCOL]='+';
			//paths[sROW-1][sCOL]='+'; //mark position
		path(paths,sROW-1,sCOL,fROW,fCOL); // call path function
		

		if (paths[sROW][sCOL-1]==' ' && sCOL-1>=0)//if path is blank charater move one space to the right
		//sCOL--;
		paths[sROW][sCOL-1]='+';
			//paths[sROW][sCOL-1]='+'; //mark position
		path(paths,sROW,sCOL-1,fROW,fCOL); // call path function
	
		//paths[sROW][sCOL]=='+';
		//path(paths,sROW,sCOL,fROW,fCOL);
	
	
	}

	
} //end function

>> this is the complete code

I reiterate. You need to stick debugging code in there and find out how many consecutive recursive calls there are before returning. There's a memory limit and if you can't guarantee it will be below that limit, you can't use recursion.

Member Avatar for format_c

>> this is the complete code

I reiterate. You need to stick debugging code in there and find out how many consecutive recursive calls there are before returning. There's a memory limit and if you can't guarantee it will be below that limit, you can't use recursion.

thats the problem... i MUST to use recursion to solve.

>> thats the problem... i MUST to use recursion to solve.

Then you need to fix the recursion. Right now it looks to me like you have too many calls and/or not enough return statements. It's a theory. I don't know for sure. You also need to add brackets and formatting to clarify your if statements because I'm almost positive that they are not what you are intending.

But you need to see for yourself how many times this function is called before returning, so stick this at the top of the function:

static int numTimesCalled = 0;
numTimesCalled++;
cout << "Number of times = " << numTimesCalled << endl;

If this number gets to be hundreds, thousands, whatever, that's an excellent clue that the recursion is bad. Somewhere you have a stack overflow and you need to figure out where. My best guess is that you're recursively calling the function too many times without a return. Find out for sure.

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.