Hey Guys,

I had a question earlier about some bounds and i've got it all figured out after some intense debugging and some help. I just have one more simple question and I think I should finally be on my way. If I have a recursive function that returns an int, and have say
someRandomInt = recursiveFunction(params),
is this allowable?

My question is because I am doing this operation and the int is just getting the value 0, not what is (or at least what I believe) being returned.

You'll find what i'm talking about at test = shortestPath(param) towards the bottom third of the program. LINE 228.
Any help is appreciated, thanks!

/*
 *  grid.cpp
 *  Homework4
 *
 *  Created by Alec on 10/31/10.
 *
 */
using namespace std;

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
//functions
int convertStreet(string street, int size);
bool checkStreet(string street);
int findPath(string matrix[16][16],int sStreet, int sAvenue, int eStreet, int eAvenue,int size, string counter[],int n, int lowest);


int main(){

	int size; // holds size of grid
	int ud; // up down
	int lr; // left right
	int aHolder; // avenue holder
	int temp1; // various temps to hold values while inserting into grid
	int temp2; // temp
	int temp3; // temp
	int startAve;
	int startStreet;
	int endAve;
	int endStreet;
	string word; // used in the stringstream for file reading
	string alphabet[15] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"};
	string numbers[35]  = {"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"};
	string line;
	string info;
	ifstream gridIn;
	ofstream paths;
	gridIn.open("grid.in");
	if(!gridIn)
	{
		cout << "There was a problem opening/reading the grid file. Please check for file";
		return(1);
	}
	//beginning to read from file
	gridIn >> size;
	int sizeMat = size+1;
	//cout << sizeMat;
	string matrix[16][16];// plus one because we want row column to hold street avenue
	//            ROW     COLUMN
	
	int k;
	int j;
	
	for (int j=0; j <= size; j++) {
		
		for (k = 0; k <= size; k++) {
			
			if(k == 0 && j != size){ // stop one short to avoid array out of bounds but put in correct letters
			
				matrix[j][k] = alphabet[(size - j)-1]; // a, b, c, d for the streets
							 
			}
		
			else {
				matrix[j][k] = "";

			}

		}
	}
	
	int row = size;
	
	for (int column = 1; column <= size; column++) { // putting numbers into the matrix correctly
		matrix[row][column] = numbers[column-1];
	}
	

	while (getline(gridIn,line))
    {
		int i = 0;
		stringstream stream(line);
		string type = "";
		while( getline(stream, word, ' ') )
		{
			
			//manipulation of the code here
			//checking to see if it is start end or road block (first letter)
			if( i==0 && word=="s")
			{
				type = "start";
			}
			else if( i==0 && word =="e")
			{
				type = "end";
			}
			else if(i == 0 && checkStreet(word)){
				type = "street";
				temp1 = convertStreet(word, size); // holds the street matrix value
				
			}
			else if(i == 0 && !checkStreet(word)){
				type = "avenue";
				temp2 = convertStreet(word, size); // holds the avenue matrix value
				
			}
			
			// after already reading from first word, now check the remaining two
			if(type == "start")
			{
				if(i==1)
				{
					string temp1 = word;
					lr = convertStreet(temp1, size);
				}
				else if(i==2) {
					string temp = word;
					
					ud = convertStreet(temp, size);					
					matrix[ud][lr] = "s";
					startStreet = ud;
					startAve = lr;
					
				}

			}
			
			else if(type =="end")
			{
				if(i==1)
				{
					string temp1 = word;
					lr = convertStreet(temp1, size);
				}
				else if(i==2) {
					string temp = word;
					
					ud = convertStreet(temp, size);					
					matrix[ud][lr] = "e"; // variables backwards here because of movement based on variables
					endStreet = ud;
					endAve = lr;
					
				}

				
			}
			
			else if(type == "street")
			{
				if(i==1)
				{
					string temp1 = word;
					//cout << word;
					lr = convertStreet(temp1, size);
				}
				else if(i==2) {
					string temp = word;
					ud = convertStreet(temp, size);	
					
					for (lr; lr <= ud; lr++) {
						matrix[temp1][lr] = "x";

					}
					
				}
			} 
				
			else if(type == "avenue")
			{
				if(i==1)
				{
					string tempA = word;
					aHolder = convertStreet(tempA, size); // aHolder. 4 then 2 in test case
				}
				else if(i==2) {
					string temp = word;
					temp3 = convertStreet(temp, size); // temp 1. 1	test case
					
					for (temp3; temp3 <= aHolder; temp3++) {
						matrix[temp3][temp2] = "x";
						
					}
					
				}
			} 
			
			i++;
        } // end of getword
		
				
    } // end of getline
	//matrix[size][0] = "-";	
	
	// print out the matrix
	int i = 0;
	j = 0;
	cout << "The given matrix is:\n" ;
	for( i=0; i <= size; i++)
	{
		cout<<"\n";
		
		for( j=0; j<= size; j++)
		{
			cout<< setw(2) <<matrix[i][j];
        }
    }   
	
	gridIn.close();
	
	// end print
	// call recursive function!
	int low;
	int c = 0;
	cout << " \nPlease enter in how long the maximum path will be, and all paths that length or shoter will be returned: ";
	cin >> low;
	int shortestPath = 100;
	int test;
	
	//if (shortestPath < ) {
	//	
	//}
	
	
	test = findPath(matrix, startStreet, startAve, endStreet, endAve, size, numbers, c, low);
	cout << test;
		//if (shortestPath > test) {
		//	shortestPath = test;
		//}
	
	//cout << shortestPath << " jkdhfdkjhfdkjfhdkjfhdkjfhdjk";
	
}
	
			
//// RECURSIVE FUNCTION ////////////////
int findPath(string matrix[16][16],int sStreet, int sAvenue, int eStreet, int eAvenue,int size, string counter[], int n, int lowest){

	//PRINT
	//going down
	if(matrix[sStreet+1][sAvenue] == "e" && sStreet <= size && n < 34 && n <= lowest)
	{
	//
		
		cout << n << endl;
		return n;
		//lowest = n;
		//cout << lowest;
		//if (lowest != 32) {
		//	cout << lowest;
		//}		
		int m = 0;
		int b = 0;
		cout << "\nThe Fastest path is:\n" ;
		for( m=0; m <= size; m++)
		{
			cout<<"\n";
			
			for( b=0; b<= size; b++)
			{
				cout<< setw(3) <<matrix[m][b];
			}
		} 
	}
	//PRINT
	//Going Up
	if (sStreet != 0 && sAvenue != (size-1) && matrix[sStreet-1][sAvenue] == "e" && n < 34 && n <= lowest) {

		cout << n << endl;
		return n;
		int m = 0;
		int b = 0;
		cout << "\nThe given matrix is:\n" ;
		for( m=0; m <= size; m++)
		{
			cout<<"\n";
			
			for( b=0; b<= size; b++)
			{
				cout<< setw(3) <<matrix[m][b];
			}
		} 		
	}
	
	//END PRINT
	
	//PRINT
	//Going Left
	if (matrix[sStreet][sAvenue-1] == "e" && n < 34 && n <= lowest) {

		cout << n << endl;
		return n;
		int m = 0;
		int b = 0;
		cout << "\nThe given matrix is:\n" ;
		for( m=0; m <= size; m++)
		{
			cout<<"\n";
			
			for( b=0; b<= size; b++)
			{
				cout<< setw(3) <<matrix[m][b];
			}
		} 
	}
	
	//PRINT
	//Going Right
	if ( matrix[sStreet][sAvenue+1] == "e" && n < 34 && sAvenue < size && n <= lowest) {
		
		cout << n << endl;
		return n;
		int m = 0;
		int b = 0;
		cout << "\nThe given matrix is:\n" ;
		for( m=0; m <= size; m++)
		{			
			cout << "\n";
			
			for( b=0; b<= size; b++)
			{
				cout<< setw(3) <<matrix[m][b];
			}
		}
	}
	
	
	// going down
	if (matrix[sStreet+1][sAvenue] == "" && n < 34 && sStreet <= size && n <= lowest ) {
		//cout << lowest; 
		string newMatrix[16][16];
		//BEGIN COPYING INTO NEW MATRIX
		int i = 0;
		int j = 0;
		for( i=0; i <= size; i++)
		{
		
			for( j=0; j<= size; j++)
			{
				newMatrix[i][j] = matrix[i][j];
			}
		} 
		//END COPYING
		newMatrix[sStreet+1][sAvenue] = counter[n];
		//cout << "down     " << endl;
		findPath(newMatrix,sStreet+1,sAvenue,eStreet,eAvenue, size, counter, n+1, lowest);
		//}
	}
	
	 //going up
	if (sStreet != 0  && matrix[sStreet-1][sAvenue] == "" && n < 34 && n <= lowest) {
		//cout << "up    " << endl;
		
		string newMatrix[16][16];
		//BEGIN COPYING INTO NEW MATRIX
		int i = 0;
		int j = 0;
		for( i=0; i <= size; i++)
		{
			
			for( j=0; j<= size; j++)
			{
				newMatrix[i][j] = matrix[i][j];
			}
		} 
		//END COPYING
		
		newMatrix[sStreet-1][sAvenue] = counter[n];		
		findPath(newMatrix,sStreet-1,sAvenue,eStreet,eAvenue,size, counter, n+1, lowest);
	}
	
	//going left
	if (matrix[sStreet][sAvenue-1] == "" && n < 34 && n <= lowest) {
		//cout << "left    " << endl;
		string newMatrix[16][16];
		//BEGIN COPYING INTO NEW MATRIX
		int i = 0;
		int j = 0;
		for( i=0; i <= size; i++)
		{
			
			for( j=0; j<= size; j++)
			{
				newMatrix[i][j] = matrix[i][j];
			}
		} 
		//END COPYING		
		newMatrix[sStreet][sAvenue-1] = counter[n];
		findPath(newMatrix,sStreet,sAvenue-1,eStreet,eAvenue,size, counter, n+1, lowest);
	}
	
	//going right
	if ( matrix[sStreet][sAvenue+1] == "" && n < 34 && sAvenue < size && n <= lowest) {
		//cout << "right   " << endl;
		//cout << sAvenue;
		string newMatrix[16][16];
		//BEGIN COPYING INTO NEW MATRIX
		int i = 0;
		int j = 0;
		for( i=0; i <= size; i++)
		{
			
			for( j=0; j<= size; j++)
			{
				newMatrix[i][j] = matrix[i][j];
			}
		} 
		//END COPYING
		newMatrix[sStreet][sAvenue+1] = counter[n];
		
		findPath(newMatrix,sStreet,sAvenue+1,eStreet,eAvenue,size,counter,n+1, lowest);
	}
						  
}
////////////////////////////////////

bool checkStreet(string street){
	
	string checks[15] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"};
	
	for (int i = 0; i < 15; i++) {
		
		if(street == checks[i]) {
			return 1;
		}
		   
	}
	
	return 0;
	
}

int convertStreet(string street, int size){
	int corrNum;
	
	if (street == "A") {
		corrNum = size - 1;
	}
	else if(street == "B"){
		corrNum = size - 2;
	}
	else if(street == "C"){
		corrNum = size -3;
	}
	else if(street == "D"){
		corrNum = size -4;
	}
	else if(street == "E"){
		corrNum = size -5;
	}
	else if(street == "F"){
		corrNum = size -6;
	}
	else if(street == "G"){
		corrNum = size -7;
	}
	else if(street == "H"){
		corrNum = size - 8;
	}
	else if(street == "I"){
		corrNum = size - 9;
	}
	else if(street == "J"){
		corrNum = size -10;
	}
	else if(street == "K"){
		corrNum = size -11;
	}
	else if(street == "L"){
		corrNum = size -12;
	}
	else if(street == "M"){
		corrNum = size -13;
	}
	else if(street == "N"){
		corrNum = size -14;
	}
	else if(street == "O"){
		corrNum = size -15;
	}
	
	string nums[15]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
	for (int j = 0; j<15; j++) {
		
	 if(street == nums[j]){
		corrNum = (j +1);
		}
	  }
	
	
	return corrNum;
}

Edited 6 Years Ago by Alec0905: n/a

This is too much code for me to look at... but just a quick run through your findPath() function shows that you have no return statement after a recursive call. Normally, you would use the result of the recursive call to either return it directly or use it to compute the returned value. You are doing neither of those things. I don't understand why your compiler allows this (you should turn on all the warnings, and if you get something like "function xxxx might not return any value", then you have a problem, and in this case, you have a problem either way).

BTW: it doesn't seem like a recursion is a particularly good idea here... usually a large number of parameters is a sign that recursion might not be the best alternative.

hey Mike, thanks for the response and i'll work on making correct return statements. What I ultimately want to have happen is to find and store the smallest n that is in the first if statement checking to see if the "person" is at "e". Then I could call the function knowing the number of moves to take and just cout those. The problem is that I can't get the number, which is "lowest" in my code, to hold the actual lowest value, it is simply overwritten with each know recursive call to it.

This article has been dead for over six months. Start a new discussion instead.