I have to make a recursive function that is passed a location in a previously allocated 2D array, and changes the "color" (indicated by a letter) from the "oldColor" to the "newColor." The function then changes any adjacent elements containing the "oldColor" to the "newColor." (If you are confused by this just let me know. It took my professor 3 pages to explain the color grid to us lol).
Here is what I have.:
void Color_Grid::paintRegion(int row, int col, char oldColor, char newColor){
//make sure everything stays in bounds
if(row < 0 || row >= rows){ return; }//go back if out of bounds because there is nothing to change anyway
if(col < 0 || col >= cols){ return; }//go back if out of bounds because there is nothing to change anyway
//Base Cases
if(grid[row][col] != oldColor){ return; }//if the character is not the old color, there is nothing to do
//if the character IS the old color, change it to the new color
grid[row][col] = newColor;
return;//POSSIBLE PROBLEM RETURN STATEMENT!!
//Check up, down, left, and right from current location
paintRegion(row-1,col,oldColor,newColor);//Check above location
paintRegion(row+1,col,oldColor,newColor);//Check below location
paintRegion(row,col-1,oldColor,newColor);//Check left of location
paintRegion(row,col+1,oldColor,newColor);//Check right of location
}
I believe the problem lies in the return statement I have indicated in my code above causing the function to return back to main after the first call without calling its self. I've moved everything around though and any other arrangement causes the program to crash. Any help would be appreciated!