By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

need help with recursion

P: n/a
Hello All,

I have array 5 x 5 with 'x' and spaises '-'.
If I give coordinates of point on the array and i on the 'x'
i must count any 'x' caracters that have shared sides (right, left,down,
up,... 8 directions)

I make that, but program not work and i can't find mistake:

///////////////////////////////////////////////////

#include <iostream.h>
const int maxrow = 5;
const int maxcol = 5;

int find_and_count_x( char s[maxrow][maxcol], int row, int col);

void main()
{
int num;
int x;
int y;
char square[maxrow][maxcol] =
{'-','x','-','-','x','x','-','-','x','x','-',
'-','x','x','-','x','-','-','-','-','x','x','x','-','-'};
for(int i=0; i<maxrow; i++ )
{
for(int j=0; j<maxcol; j++ ) // initilaze array 5x5 and print it

cout << square [i][j] ; // with tab every column

cout << '\n';

}
cout << '\n';

cout << "Please give position on that arrow: \n";
cin >> x >> y;
cout << "The :" << x << "," << y << " position have: ";
num = find_and_count_x(square, x, y);
cout << num;
}

int find_and_count_x( char s[maxrow][maxcol], int row, int col)
{

int count;
if(s[row][col]=='-')
{
count=0;
}

else if(((row>=0) || (row<=maxrow-1)) || ((col>=0) || (col<=maxcol-1)))
{
if((row+1!=maxrow-1)&&(s[row+1][col]=='x'))
{ // row+1, col
cout << " row+1, col" << endl;
count=+1;
find_and_count_x(s, row+1, col);
}
if((col+1!=maxcol-1)&&(s[row][col+1]=='x'))
{ //row, col+1
cout << " row, col+1" << endl;
count=+1;
find_and_count_x(s, row, col+1);
}
if((row+1!=maxrow-1)&&(col+1!=maxcol-1)&&(s[row+1][col+1]=='x'))
{ //row+1, col+1
cout << " row+1, col+1" << endl;
count=+1;
find_and_count_x(s, row+1, col+1);
}
if((row+1!=maxrow-1)&&(col-1>0)&&(s[row+1][col-1]=='x'))
{ //row+1, col-1
cout << " row+1, col-1" << endl;
count=+1;
find_and_count_x(s, row+1, col-1);
}
if((col-1>0)&&(s[row][col-1]=='x'))
{ //row, col-1
cout << " row+, col-1" << endl;
count=+1;
find_and_count_x(s, row, col-1);
}
if((row-1>0)&&(col-1>0)&&(s[row-1][col-1]=='x'))
{ //row-1, col-1
cout << " row-1, col-1" << endl;
count=+1;
find_and_count_x(s, row-1, col-1);
}
if((row-1>0)&&(s[row-1][col]=='x'))
{
cout << " row-1, col" << endl;
count=+1; //row-1, col
find_and_count_x(s, row-1, col);
}
if((row-1>0)&&(col+1<maxcol-1)&&(s[row-1][col+1]=='x'))
{ //row-1, col+1
cout << " row-1, col+1" << endl;
count=+1;
find_and_count_x(s, row-1, col+1);
}

}

return count;
}
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alexander wrote:

Hello All,

I have array 5 x 5 with 'x' and spaises '-'.
If I give coordinates of point on the array and i on the 'x'
i must count any 'x' caracters that have shared sides (right, left,down,
up,... 8 directions)

I make that, but program not work and i can't find mistake:


Please define 'does not work'.

Anyway.
I see you have inserted some tracing output to follow the
programs behaviour. But unfortunately this output is pretty
useless, because it doesn't show the most important information:
On which array element the program is working right now.

Adding:

int find_and_count_x( char s[maxrow][maxcol], int row, int col)
{

cout << "Search at " << row << " " << col << endl;
and entering a coordinate of 0 4, shows pretty fast
what is going on.

When the program finally reaches the position 3 6, it searches
its neighbors. It does so by checking the field 3 7. To test
that, it again searches its neighbors and does so by checking
the field 3 6, which leads to a check of 3 7, which leads to
a check of 3 6, ....

One possible solution is:
mark the field in the array as beeing already tested.
So when a neighbor is checked, you first test if it has
already been tested and do nothing in this case.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
On Thu, 13 May 2004 11:39:23 +0200 in comp.lang.c++, "Alexander"
<ar*****@mail.ru> wrote,
I make that, but program not work and i can't find mistake:
Define "not work" ?

Some random comments below;
void main()
{
According to C++ standard, return type of main() must ALWAYS be int.
int find_and_count_x( char s[maxrow][maxcol], int row, int col)
{

int count;
Uninitialized variables are evil. Should be:
int count(0);

Note that the variable count is local specific to this activation of
find_and_count_x(), and that each time you call find_and_count_x()
recursively you will get a new unique count variable that has nothing to
do with any other activation of find_and_count_x(). Is that what you
want?
count=+1;


Once upon a time the above meant something special (add 1 to count), but
today there is no operator=+ and it is just confusing. In order to be
clear about what you mean, choose one of the following instead:
count = 1;
or
count += 1;
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.