1 Replies - 1473 Views - Last Post: 02 December 2011 - 09:15 PM Rate Topic: -----

#1 MainframeHacker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-December 11

Recursive Blob Finder. It's not reading in my 2d array right

Posted 02 December 2011 - 08:26 PM

assignment description so you get the gist of it: "In this assignment you will be working with reading in text files into multi-dimensional arrays, passing 2d arrays as parameters and use recursive functions.
You will write a program to read in a file and found out how many groupings (blobs) of characters there are in the file. The image below is a sample input file. Note that it contains 6 groupings of characters or blobs.
A character is part of a grouping or blob if it touches another character in the grouping on 1 of 8 positions around it. Notice that one of the groupings or blobs is located inside of another.
1. Upper left,
2. Directly above
3. Upper right
4. To the right
5. Lower right
6. Directly below
7. Lower left
8. To the left.

You will open a file called blob.txt which will be provided to you. The file will 20 records each of which is exactly 70 characters long. Your program can make the assumption that both the file name and size of the file (rows and columns) are exact. The characters will either be a space (blank ) or have an upper case X."


what I'm having problems with is clearing the array and then filling it with the lines from the text file. It's not working for some reason. It's not even like it leaves it blank after I clear it, it fills it randomly with a single X and characters that resemble alien writings. lol. Here's my code. Please help me asap as it's due soon. If I don't get help tho it's fine. I've been working on this problem for the past 3 days and still haven't fixed it. I just stumbled upon this website so I thought I'd post and ask for help. anyway. Thanks :)
P.S. ignore my sarcastic comments i put in it. lol.


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// *****************************************************
// * Constants                                         *
// *****************************************************
const int MAXLINESIZE=20;
const int MAXROWSIZE=22;
const int MAXCOLSIZE=72;

// *****************************************************
// * Function Prototypes                               *
// *****************************************************
void cleararray(char ba[][MAXCOLSIZE]);
void killblob(char ba[][MAXCOLSIZE],int ,int );

// *****************************************************
// *               M A I N   R O U T I N E             *
// *****************************************************
int main(void)
{
	int blob=0;
	string line;
	char bigarray[MAXROWSIZE][MAXCOLSIZE];
	char inputline[MAXLINESIZE];
	cleararray(bigarray);
	
	ifstream myfile;
	myfile.open("blob.txt");	//opens file "blob.txt"
	if (myfile.is_open())		//prints out blob.txt to console
	{
		while(myfile.good())
		{
			getline(myfile,line);
			cout<<line<<endl;
		}
	}
	myfile.close();

	myfile.open("blob.txt");
	for(int row=1; row<MAXROWSIZE-1;row++)	//supposed to fill my array but doesn't work too well. Puts an alien language in it.
	{
		myfile.getline(inputline,MAXLINESIZE);
		cout<<inputline<<endl;
		for(int col=1;col<MAXCOLSIZE-1;col++)
		{
			bigarray[row][col]=inputline[col-1];
		}
		
	}


	for(int row=0; row<MAXROWSIZE;row++)	//scans and kills for blobs... atleast it would if the array was right
		for(int col=0; col<MAXCOLSIZE; col++)
		{
			if(bigarray[row][col]=='X')
			{
				killblob(bigarray,row,col);
				blob++;
			}
		}

	myfile.close();
	
	cout<<endl<<"The number of blobs is: "<<blob<<endl; //prints the number of blobs
	

	return 0;
}


// *************************************************************************************
// * Function Name: cleararray(char ba[][MAXCOLSIZE])                                  *
// * Description: Clears the array of anything that might be in it that isn't supposed *
// *	to be																		   *
// * Note: I got this code from class												   *
// *														                           *
// *************************************************************************************
void cleararray(char ba[][MAXCOLSIZE])
{
	for(int row=0; row<MAXROWSIZE;row++)
		for(int col=0; col<MAXCOLSIZE; col++)
			ba[row][col]=' ';
		
}


// *************************************************************************************
// * Function Name: killblob(char ba[][MAXCOLSIZE],int r ,int c)                       *
// * Description: Kills the blob by searching at the spaces around it.				   *
// *																			       *
// *														                           *
// *************************************************************************************
void killblob(char ba[][MAXCOLSIZE],int r ,int c)
{
	ba[r][c]=' ';

		if (ba[r-1][c+1]!=' ')		
			killblob(ba,r-1,c+1);	//top right
		if (ba[r][c+1]!=' ')		
			killblob(ba,r,c+1);		//right
		if (ba[r+1][c+1]!=' ')		
			killblob(ba,r+1,c+1);	//bottom right
		if (ba[r+1][c]!=' ')		
			killblob(ba,r+1,c);		//bottom
		if (ba[r+1][c-1]!=' ')		
			killblob(ba,r+1,c-1);	//bottom left
		if (ba[r][c-1]!=' ')		
			killblob(ba,r,c-1);		//left
		if (ba[r-1][c-1]!=' ')		
			killblob(ba,r-1,c-1);	//top left
		if (ba[r-1][c]!=' ')		
			killblob(ba,r-1,c);		//top
}



Is This A Good Question/Topic? 0
  • +

Replies To: Recursive Blob Finder. It's not reading in my 2d array right

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Recursive Blob Finder. It's not reading in my 2d array right

Posted 02 December 2011 - 09:15 PM

You need to re-think your constants. Is MAXLINESIZE the length of a line or the number of lines?

Is MAXCOLSIZE the length of a column or the number of columns?

I find MAXCOLSIZE and MAXROWSIZE confusing. Most people would use ROWS and COLUMNS instead. Then it would be clear that they mean the number of rows and number of columns respectively.

Sort that out & I think you will be able to make more progress.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1