Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make this program recursive? Please Help!

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hi, I have a program that executes, but it is not recursive. I would appreciate any help on this. Thanks in advance. (Thanks to DavesTips for his previous help on this program). The program is supposed to implement the factorial function recursively. You can execute it to see the output. n! needs a value as well. Here it is:

Code:
  #include <iostream>
#include <iomanip>
using namespace std;  //Standard namespace is being used

void FindBlock(int aBmp[][5], int nSize, int cx, int cy){ //An array 
	//is being initialized in order to create the picture
 

    if ((aBmp[cx][cy] == 0) || aBmp[cx][cy] > 1) return;
    if (aBmp[cx][cy] == 1) aBmp[cx][cy] = 2;
    if (cx > 0) FindBlock(aBmp, nSize, cx - 1, cy);
    if (cx < nSize-1) FindBlock(aBmp, nSize, cx+1, cy);
    if (cy > 0) FindBlock(aBmp, nSize, cy, cy - 1);
    if (cy < nSize-1) FindBlock(aBmp, nSize, cx, cy+1);
}
 
void ClearBlock(int aBmp[][5], int nSize) //The objects that 
//have been visited in the picture will be cleared
{
    for (int m=0; m < nSize; m++)
        for (int n=0; n < nSize; n++)
            if (aBmp[m][n] > 1) aBmp[m][n] = 0;
}
 
void ErasePic(int aBmp[][5], int nSize, int cx, int cy)
//The object will be erased which the given black pixel is part 
 
{
    FindBlock(aBmp, nSize, cx, cy);
    ClearBlock(aBmp, nSize);
}



void printBlock(int aBmp[][5], int nSize) 
//Original picture will be printed first, then new picture
//will be printed
{
	for (int m=0; m < nSize; m++)
	{
		for (int n=0; n < nSize; n++)
			cout << setw(3) << aBmp[m][n];
		cout << endl;
	}
	
	cout <<&quot;  *************&quot; << endl;;  //Asterisk is used 
//to separate original picture from new picture

	
}

int main(int argc, char* argv[])
{
cout << &quot;Program Assignment 2: ErasePic Funtion\n&quot; << endl;
//Title of project
cout <<&quot;  Initial Image&quot; << endl;  //Initial picture will apear underneath
cout <<&quot;  *************&quot; << endl;  
	int block[5][5] =   //Prints picture in a 5x5 block
	{{1,1,0,1,0},
	{1,0,0,1,1},
	{1,0,1,1,1},
	{1,0,1,1,0},
	{0,0,1,0,0}};
	printBlock(block,5);
	ErasePic(block,5,2,2);
	printBlock(block,5);
cout <<&quot;  After Execution\n\n&quot; << endl; //New picture will appear underneath
	return 0;
	}

Thanks again for any responses...
 
Frankly I wonder if you have any idea what your doing. This program has nothing whatsoever to do with factorials, and it is in fact already recursive. It sounds like you want a new and completely different program. I think if you're learning (as I suspect) you need at some point to try writing some code yourself. :) Hope that this helped! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top