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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive and Iterator Program. Please HELP!! 1

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello,
I'm having a problem understanding what to do in this program, I would appreciate any suggestions. Thanks in advance for the responses and the help. Here it is:

A Computer graphics image is composed of rectangular points on the screen. In a black and white picture, we can use 0 to represent white and 1 for black. The picture is a 5 x 5 square on the screen as follows:
1 1 0 1 0 1 1 0 0 0
1 0 0 1 1 1 0 0 0 0
1 0 1 1 1 1 0 0 0 0
0 1 1 0 1 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
(initial image) (image after exe of ErasePic function)

Two black pixels are part of the same object if we can get from one to the other with horizontal and vertical moves. Thus, Fig 1(initial image) contains 2 objects.
Write a function ErasePic to erase the object of which the given black pixel is part. Invoking the function for the bold face 1 results in erasing a representation of the rightmost object of Fig 1 (see Fig 2).


Thanks again for the help, and GOD BLESS AMERICA!
 
Sorry, I left one thing out, the C++ function must implement the factorial function recursively and iteratively.
 
Well,
I think for each pixel with a given vertical index (vi) and
horizontal index (hi) you should check the adjacent pixels
with vi-1 and vi+1 as well as hi-1 and hi + 1.
with each new pixel found, you can create an object which
holds it hi and vi and then do the same checking.
Go on through all the pixels until none is found, then set
all the pixels of your objects to 0.
 
the key is how to traverse the whole map.
a gerneral way to do this is to set a mark to the pixel after you visit it. So that you won't revisit it again.
the code look like this,
void FindBlock(int *aBmp, int nSize, int cx,int cy){
if (aBmp[cx][cy] == 0 || aBmp[cx][cy] > 1 ) return;
if (aBmp[cx][cy] == 1) aBmp[cx][cy] += 1000;
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,cx,cy-1);
if (cy<nSize-1) FindBlock(aBmp,nSize,cx,cy+1);
}
ClearBlock(int *aBmp,int nSize){
for (int i=0;i<nSize;i++)
for (int j=0;j<nSize;j++)
if (aBmp[j] > 1) aBmp[j] = 0;
}

ErasePic(int *aBmp,int nSize,int cx,int cy){
FindBlock(aBmp,nSize,cx,cy);
ClearBock();
}

It is not the most efficient, but it works.
 
Something is wrong with the editor. I realized I could not put i between [ ]. If i is between [ ] , the i and [] will be lost!

I rewrite ClearBlock here:

ClearBlock(int *aBmp,int nSize){
for (int m=0;m<nSize;m++)
for (int n=0;n<nSize;n++)
if (aBmp[m][n] > 1) aBmp[m][n] = 0;
}
 
Thanks a lot for the help. I really appreciate it.
A few more things:
Do I have to return anything?
What namespace would I use for this code?
Thanx again.
 
jfhuang, there is nothing wrong with the editor. It is simply that is the &quot;tag&quot; for &quot;italic&quot;. If you don't want any such formatting, uncheck the &quot;Process TGML&quot; option at the bottom.
 
You can add some error check code in the program, then you need a return value to indicate the error.

I don't think any special namespace is needed.

By the way, thank VincentP. I got it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top