recursive blob counter

I've been working on this program that is supposed to count the number of blobs in a text file using a recursive function and print out the number of total blobs. In lecture my teacher suggested we read the Xs into a character array then erase them as we count. My code is in miserable shape right now since I missed the lecture on recursion and I could use some very serious help getting this program to produce proper output.
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
//
//  main.cpp
//  blobcounter
//
//  Created by Robert Timpone on 8/1/12.
//  Copyright (c) 2012 TAK Media LLC. All rights reserved.
//

#include <iostream>
#include <iomanip>
#include <fstream>

const int MAXCHAR = 80;

using namespace std;

int BlobCount(int r, int c);

int main (void){
    int x, size, y;
    x = 0;
    size = 74;
    ifstream myfile;
    
    char inputline[MAXCHAR];
    myfile.open("blob.txt");
    //myfile.getline(inputline, MAXCHAR-1);
    
    while(!myfile.eof()){
        x++;
        for( y = 0; y < size; y++){
        //put data in 2d array here
        //cout<<inputline<<endl;
        myfile.getline(inputline, MAXCHAR-1);
        }
        cout<<BlobCount(x, y);
    }
    
    //use for loop to count Xs and erase them after counting
    myfile.close();
    


    return 0;
}

int BlobCount(int r, int c){
    
    int dubarr[24][74];
    if ( ((r >= MAXCHAR) || (r <0)) || ((c >=MAXCHAR) || (c <0)) )
		return 0;
	else if (dubarr[r][c] == MAXCHAR)
		return 0;
    return  ( 1 
			+ BlobCount(r ,c-1 ) 
			+ BlobCount(r ,c+1 ) 
			+ BlobCount(r+1, c) 
			+ BlobCount(r-1, c) 
			+ BlobCount(r+1, c+1)
			+ BlobCount(r+1, c-1)
			+ BlobCount(r-1, c-1)
			+ BlobCount(r-1, c+1));

}
I'm working on the same problem right now. My recursive function looks very different from yours (Posted below). So far my program can count the number of X's in the file but not the # of groups. If I posted my code as well would someone be able to help me?I've debugged my program many times and I'm almost certain that the problem lies in the counting function *(again, posted below).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void COUNTBLOB(char grid[][MAXCOL])
{
int counter = 0;
int blobnum;
	for(int r=1; r<MAXROW-1; r++)
		{
			for(int c=1; c<MAXCOL-1; c++)
				{

					if (grid[r][c]='X');
					DBLOB(grid, r, c);
					counter++;
					
					
					
				}
		}


cout << "# of blobs: " <<counter<< endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void DBLOB(char grid[][MAXCOL], int r, int c)
{
if(grid[r][c]=' ') return;


if(grid[r][c]!=' ')DBLOB(grid,r,c);
if(grid[r][c+1] != ' ') DBLOB(grid, r, c+1);
if(grid[r-1][c+1] != ' ') DBLOB(grid, r-1, c+1);
if(grid[r-1][c] != ' ') DBLOB(grid, r-1, c);
if(grid[r-1][c-1] != ' ') DBLOB(grid, r-1, c-1);
if(grid[r][c-1] != ' ') DBLOB(grid, r, c-1);
if(grid[r+1][c-1] != ' ') DBLOB(grid, r+1, c-1);
if(grid[r+1][c] != ' ') DBLOB(grid, r+1, c);
if(grid[r+1][c+1] != ' ') DBLOB(grid, r+1, c+1);
}
Topic archived. No new replies allowed.