Help with Recursive Function

Hi I am trying to fill up an area from a rectangle. I have operator overloading function that returns this number. Question is how do I create a recursive function that will take in the number from the operator overload function and divide that by tiles. Each tile = 2.1*3.2

I understand for a regular simple function, the code would look like this, but I am lost with the operator overload function.

void simple()
{
cout << "this is a simple recursive function" << endl;
simple();
}

I know this will create a continuous loop but Its a good starting point.

Thanks

1
2
3
4
5
6
7
8
	//Operator overloading function
	Rectangle Rectangle::operator+(const Rectangle& b)
	{
		Rectangle rect;
		rect.width = this->width + b.width;
		rect.height = this->height + b.height;
		return rect;
	}
I'm not entirely sure what you need recursion for. The operator function you provided is not recursive.

Recursion is useful as an alternative to looping. You can often make looping much simpler by utilizing recursion. However, whenever you use it, you need to ensure that you have an exit condition that leads you to a path which does not invoke another recursive call (similar to a break statement or exit condition in a while loop).
according to the practice problem this is what it shows:

Please add a public recursion function that calculates how many tiles needed to fill up the area of the Rectangle object. Each tile is 2.1*3.2. The result should be an integer (ceiling). Please do NOT use division calculation to get the result (too trivial), but use recursion technique to resolve the issue.

I came up with this so far, get am errpr redifinition of formal parameters 'tile'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	//Recursion function
	double Rectangle::recursiveTile(double rect, double tile, int x)
	{
		double tile = 2.1*3.2;

		if (x==1)
		{
			return 1;
		}
		else
		{
			double tileArea = tile/rect;
			return x*recursiveTile(rect, tile, x);
			cout << tileArea << endl;
		}
	}
tile is defined on line 2 and again on line 4.

Your problem statement says not to use divsion, but that's exactly what you're doing on line 12.

The statement at line 14 will never be reached.

The problem statement says to return an integer, however you're returning a double.

Last edited on
Thanks, yeah I am having trouble logically how would I not use division for this?

updated code:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
	//Recursion function
	int Rectangle::recursiveTile(double rect, double tile, int x)
	{
		tile = 2.1*3.2;

		if (x==0)
		{
			return 1;
		}
		else
		{			
			return x * recursiveTile(rect, tile, x-1);
		}
	}
Still not sure what you're trying to do, however rect and tile are always set to the orignal value and 6.72 respectively. Therefore you're really just returning x! (x factoral). That's likely to explode with any x greater than 13. What are you trying to do with rect and tile?
Thanks for the response, I don't have the code with me but what I did was have the area of the rectangle subtract the tile. The amount of loops the tile did is the amount of tiles it would take to fill up the area.
In which case your function should take two arguments, AreaOfRectagle and NumberOfTiles. Each recursion will subtract from AreaOfRectangle and add to NumberOfTiles just like you said. Once AreaOfRectangle becomes less than or equal to zero return NumberOfTiles.
Topic archived. No new replies allowed.