A
Alexander
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 [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;
}
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 [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;
}