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!

Recommended Answers

All 6 Replies

From just looking at the code I would say yes that return is the problem because it tells the function to return (do not run anything past this point) before it can recall itself. Other than that I think the code is fine.

can you think of any way to change or rearrange the code so that it works?

I feel like I've rearranged the function every possible way and I just can't determine what to do with that return statement because every arrangement I do (besides the current one) crashes the program.

Post your whole code and I'll try to get it working.

alright.

This is the header file (Color_Grid.h)

//declaration file--Implementation can be found in Color_Grid.cpp


#ifndef Color_Grid_h
#define Color_Grid_h

#include<cstdlib>
#include<fstream>

using namespace std;

class Color_Grid {
      public:
             Color_Grid(char* gridFile); // Constructor – reads in the grid
                                         // from the infile
             void displayGrid(); // Displays the color grid
             void paintRegion(int row, int col, char oldColor, char newColor);
                                // changes the color for connected region for the grid at
                                // location[row][col]
      private:
              char **grid;
              int rows;
              int cols;
};

#endif

Here is the implementation for the header (Color_Grid.cpp)

//implementation file for Color_Grid.h


#include<cstdlib>
#include<iostream>
#include<fstream>
#include "Color_Grid.h"

using namespace std;

//class functions

//constructor
Color_Grid::Color_Grid(char* gridFile){
      ifstream infile;
      infile.open("input.dat");//opens file that gridFile points to
          if(!infile){//if !infile is true, the file was not successfully opened
                  cerr<<"File could not be opened"<<endl;
                  exit(1);
                  }
      
      if(!infile.eof()){//make sure the file doesn't end
          infile >> rows; //get first number from file and use for rows
          infile >> cols; //get second number from file and use for columns
          }
      
      //allocate grid using values 'rows' and 'cols' given by the file
      grid = new char*[rows];
      for(int r=0; r<rows; r++){
              grid[r] = new char[cols];
              }

	  //Read contents of file into the 2D array
		for(int i=0; i<rows; i++){
			for(int j=0; j<cols; j++){
				infile >> grid[i][j];
			}
		}
}

void Color_Grid::displayGrid(){
	for(int i=0; i<rows; i++){
		for(int j=0; j<cols; j++){
			cout << grid[i][j] << " ";
		}
		cout<<endl;
	}     
	cout<<endl;
	cout<<endl;
}

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;

	//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
}

Here is the main function file given to me by my professor (useColorGrid.cpp):

#include <cstdlib>
#include <fstream>
#include <iostream>
#include "Color_Grid.h"

using namespace std;

int main(){
    
	Color_Grid g("input.dat");
	g.displayGrid();
	g.paintRegion(2, 5, 'b', 'y');
	g.displayGrid();
	g.paintRegion(1, 4, 'r', 'g');
	g.displayGrid();

	
	system("PAUSE");
	return 0;
    
}

here is the input file (input.dat)--
The numbers tell how big the 2D array should be.

4 6
rrbgyy
brbyrr
ggrrrb
yrryrb

The first time the paintRegion function is called the text should change to be:

r r b g y y
b r b y r r
g g r r r y
y r r y r y

The second time it should look like:

r r b g y y
b r b y g g
g g g g g y
y g g y g y

Is this the output you want or that you are just getting? I removed the return that you were having problems with and it matchs those outputs.

Is this the output you want or that you are just getting? I removed the return that you were having problems with and it matchs those outputs.

lol yea I removed the return too (just to see what would happen) and got the correct output. I could have sworn I tried that before (I can't tell you how many times I cut and pasted things around in that function).

Thanks for your help though. It never fails that as soon as I ask for help I end up stumbling upon the answer myself lol.

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.