Recursion help please.

Hello. I know I'm new, but could really use some help. I am almost done with my code, but my problem persists that I can not figure out how to terminate a recursion once the conditions are met. Any help would be great.

Code of the project is provided below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

const int MAXROW=22;
const int MAXCOL=72;
const int MAXLINESIZE=80;
void cleararray (char ba[] [MAXCOL]);
void find (char ba[] [MAXCOL], int row, int col);

int main()
{
	ifstream myfile;
	
	char blobarray[MAXROW] [MAXCOL];
	char inputline[MAXLINESIZE];
	int counter;

	cleararray (blobarray);
	
	myfile.open("blob.txt");
	
	for (int row = 1; row < MAXROW-1; row++)
	{
		myfile.getline(inputline,MAXLINESIZE);
		cout << inputline << endl;
		for (int col = 1; col < MAXCOL-1; col++)
			blobarray[row] [col] = inputline[col-1];
	}

	for (int row = 1; row <= MAXROW-1; row++)
		for (int col = 1; col <= MAXCOL-1; col++)
	{
		if (blobarray[row] [col] == 'X')
			cin >> counter;
			counter++;
		find (blobarray, row, col);
	}

	myfile.close();
	cout << "\nThere are " << counter << " BLOBS.\n\n";
	return 0;
}

void cleararray (char ba[] [MAXCOL])
{
	for (int row = 0; row<MAXROW; row++)
		for (int col = 0; col<MAXCOL; col++)
			ba[row] [col] = ' ';
}

void find (char ba[] [MAXCOL], int row, int col)
{
	ba[row] [col] = ' ';

	if (ba[row+1] [col-1] == 'X')
		ba[row+1] [col-1] = ' ';

	if (ba[row+1] [col] == 'X')
		ba[row+1] [col] = ' ';

	if (ba[row+1] [col+1] == 'X')
		ba[row+1] [col+1] = ' ';

	if (ba[row] [col+1] == 'X')
		ba[row] [col+1] = ' ';

	if (ba[row-1] [col+1] == 'X')
		ba[row-1] [col+1] = ' ';

	if (ba[row-1] [col] == 'X')
		ba[row-1] [col] = ' ';

	if (ba[row-1] [col-1] == 'X')
		ba[row-1] [col-1] = ' ';

	if (ba[row] [col-1] == 'X')
		ba[row] [col-1] = ' ';
}
What recursion are you talking about? Normally you just make sure you don't call the function when the conditions is met and the recursion will stop.
I don't see any problem in the code, it runs fine. And there is no recursion used in this code.
Because you don't understand what recursion is.

It's a function calls itself.

In the find() , there's no call to find(). => no recursion sir.
Yeah, I'm not the brightest when it comes to coding. I just know it is supposed to scan a file to find "blobs" of characters and count how many blobs there are. It runs all fine and dandy until it wants to count them. In the sample text file, there are 6 blobs, and it outputs in my program -8125678 (some random negative number). That's the part I'm stuck with.
@M031291 : I'm 100% self-taught,and I've already supported people.Going to class isn't the only way to be a programmer. :)

@OP : If you've put recursion and it doesn't terminate there'll be a stack overflow and terminates the problem (sometimes worse) but it didn't have any recursion so I couldn't figure out your problem.
my program is pretty much identical to yours, I'm still having trouble incorporating recursion as well...I'm sure that would resolve the hideous negative number result...
@hentaiw : Of course, the most effective way to learn is through trial and error on your own time. Wish I was as brilliant as you though! Lol. I'm having trouble and going to class helps me. I'm pretty sure the OP has the same assignment from the same professor as me so I asked.
@M031291 : The problem with this is the problem description is not what I'm supposed to find and correct for OP.There's no recursion here.

Again recursive function is :

1
2
3
4
void someFunction()
{
      someFunction();
}


It calls itself.
I'm not exactly sure because I'm pretty new at this. But I think the hard part is incorporating a double array into the recursion function?
I would eschew from using the term recursion where it does not apply.

Let me try to make the matter clear to you, this is recursion:

1
2
3
4
5
6
7
int factorial(int x)
{
    if (x > 0)
        return factorial(x-1) * x;
    else
        return 1;
}


This is the normal way of doing it:

1
2
3
4
5
6
7
int factorial(int x)
{
    int ans = 1;
    for (int i = 1; i <= x; ++i)
        ans *= i;
    return ans;
}


The term recursion does not apply to arrays, it applies to a function which calls itself, to form a loop.
Most loops and recursions are interchangeable.
it outputs in my program -8125678 (some random negative number)
You didn't initialize counter
Wait
1
2
3
		if (blobarray[row] [col] == 'X')
			cin >> counter; // ¿?
		counter++;


¿what is the 'find' function supposed to do? Its name is misleading
Topic archived. No new replies allowed.