Blob Help!

This is what I have so far, not really sure where to go with it. Apparently I'm not allowed to use recursion...Here is what my professor wants..

). Write a C++ program to count pixels on a grid. The data are in a two-dimensional grid of cells,
each of which may be empty (0) or filled (1). The filled cells are connected to form a blob (an object). A
blob can consist of one or more ones connected horizontally, vertically, or diagonally. Thus a file
consisting of all ones represents a single (very large) blob. The file begins with a line containing a single
non-negative integer N that indicates the (square) dimensions of the grid (N x N). The value of N will
never exceed 50. The next N lines will contain N characters. Output will consist of the file read in, the size
of each blob in ascending order and the total number of blobs in the picture. If a grid contains no blobs,
output the phrase: No blobs exist. Input the file name from the keyboard and check for valid files.
Output should look similar to 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
Sample Input File: Sample Run:
15
000001111010000
000000100010000
100000010010001
000000011100000
000000000000000
000011000000000
000000000000000
001111000001000
000110000001000
000000100001000
000001100001100
000000100001111
000001111111111
000010000001111
000100000001111

Enter the file name: blob.txt
000001111010000
000000100010000
100000010010001
000000011100000
000000000000000
000011000000000
000000000000000
001111000001000
000110000001000
000000100001000
000001100001100
000000100001111
000001111111111
000010000001111
000100000001111
There are 6 blobs on the grid.
Sizes: 1, 1, 2, 6, 12, 33




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
 include<iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;





int main()
{
	
	ifstream inputFile;
	string FileName;
	int blob_size;
	string line;
	char num;

	cout << "Enter the file name: ";
	cin >> FileName;


	inputFile.open(FileName);

	if (inputFile)
	{

		while (getline(inputFile, line))
		{
			char dash;

			// Use a stringstream help parse each line.
			stringstream iss(line);

			iss >> num ;
			

		}

	}
	else
	{

		cout << "The file could not be opened." << endl;

	}

	
	cin.get();
	return 0;

}


Last edited on
Hello Unisaurus, in such way I would progress this exercise:

First I would break the whole program into seperate tasks.

1) Making a class representing a blob. I suggest that it holds a container which holds all positions of adjacent 1s (or it could hold the coordinate of the first 1 and values representing the relative position of all adjacent 1s).

2) Making a class representing the whole file. It should hold all blobs in a container.

3) Making a function which reads from data file and creates from this a class object containing the blobs.

4) Writing an output function.

This is the updated code!

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
82
83
84
85
86
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int total_size;
int blob_check(int[50][50], int, int);    /* Checks for picture blob size in the grid */

int main(void)
{
	/* Declare variables */
	int grid[50][50],      /* The picture grid */
		x_count,          /* Counts the rows in the grid */
		y_count,         /* Counts the columns in the grid */
		blob_size;        /* The cell number size of the picture blob */
	ifstream InputFile; 
	string fileName;
	string line;
	char num [225];
	int a = 0;

	cout << "Enter the file name: ";
	cin >> fileName;

	InputFile.open(fileName);

	if (InputFile)
	{
		while (getline(InputFile, line))
		{

			if(line.begin() != line.end())
			
			{ 

				if (a <= 15)
				{
					cout << line;
					cout << endl;
					a++;
				}

			}

		}
	}
	else 
	{

		cout << "Error: This file could not open." << endl;

	}
	cout << ("\n\n\t      ");
	
	/* Count the size of the picture blob */
	blob_size = blob_check(grid, 0, 1);

	/* Display the size number */
	cout << "\nThe picture blob has a cell sizes of \n" << total_size << endl;

	cin.ignore();
	cin.get();
	return 0;
}


int blob_check(int pic_grid[50][50], int x, int y)
{
	                          /* The cell number size of the picture blob */

											  /* Count size of picture blob */
	if (pic_grid[x][y] == 0 || (x < 0 || x > 4) || (y < 0 || y > 4))
		total_size = 0;
	else
	{
		pic_grid[x][y] = 0;
		total_size = 1 + blob_check(pic_grid, x, y) + blob_check(pic_grid, x + 1, y + 1) + blob_check(pic_grid, x - 1, y - 1) +
			blob_check(pic_grid, x + 1, y) + blob_check(pic_grid, x - 1, y) + blob_check(pic_grid, x, y + 1) + blob_check(pic_grid, x, y - 1) +
			blob_check(pic_grid, x + 1, y - 1) + blob_check(pic_grid, x - 1, y + 1);
	}

	return total_size;
}
Topic archived. No new replies allowed.