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!=maxr ow-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!=maxc ol-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!=maxr ow-1)&&(col+1!=max col-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!=maxr ow-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<ma xcol-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;
} 2 1296
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
On Thu, 13 May 2004 11:39:23 +0200 in comp.lang.c++, "Alexander"
<ar*****@mail.r u> 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; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Peri |
last post by:
I'm trying to create Python parser/interpreter using ANTLR.
Reading grammar from language refference I found:
or_expr::= xor_expr | or_expr "|" xor_expr
For me it looks like infinite recursion. And so it says ANTLR. Maybe I
don't understand EBNF notation. For me it should look like this.
or_expr::= xor_expr | xor_expr "|" xor_expr
and in ANTLR grammar file like this:
|
by: da Vinci |
last post by:
Greetings.
I want to get everyone's opinion on the use of recursion.
We covered it in class tonight and I want a good solid answer from
people in the "know" on how well recursion is accepted in modern day
applications.
Should we readily use it when we can or only when absolutly forced to
use it?
|
by: un[real] |
last post by:
Like I said in the title, I have to calculate the sum of the numbers of
an interger with a recursive fonction.
Exemple: n=1255, sum= 1 + 2 + 5 + 5= 13
Do I have to use a string on can I use a simple cin ??
Thanks for your help !!
And sorry for my english :)
|
by: Rafia Tapia |
last post by:
Hi all
This is what I have in mind and I will appreciate any suggestions.
I am trying to create a xml help system for my application. The schema of
the xml file will be
<helpsystem>
<help id="unique id">
<text>text data</text>
<link
href="optional tag which will hold a reference to a uri"
|
by: Joel Finkel |
last post by:
Folks,
I am confused as to how to implement the following solution.
I have a series of processing steps, each of which contains similar features
(forms, etc). Therefore, I create a base class, Step, and subclass from that
for specific steps. The Step class has a method, Execute(), which can return
either Success or Failure.
I have a Step Driver, which instantiates the first Step, calls its Execute()
| |
by: Sathyaish |
last post by:
Can every problem that has an iterative solution also be expressed in
terms of a recursive solution?
I tried one example, and am in the process of trying out more examples,
increasing their complexity as I go. Here's a simple one I tried out:
#include<stdio.h>
/* To compare the the time and space cost of iteration against
|
by: MTD |
last post by:
Hello all,
I've been messing about for fun creating a trial division factorizing
function and I'm naturally interested in optimising it as much as
possible.
I've been told that iteration in python is generally more
time-efficient than recursion. Is that true?
Here is my algorithm as it stands. Any suggestions appreciated!
|
by: pereges |
last post by:
Hello I need some ideas for designing a recursive function for my ray
tracing program.
The idea behind ray tracing is to follow the electromagnetic rays from
the source, as they hit the object.The object is triangulated. The
rays can undergo multiple reflections, diffractions etc of the same
object i.e. a ray hits a surface of the object, undergoes reflection
resulting in a reflected ray which can again hit a surface, corner or
edge...
|
by: Muzammil |
last post by:
int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}
can any help me ??
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |