Public feed #893583

Anonymous

public text v1 · immutable
Raw Edit
·published 2008-02-06 07:23 UTC
rendered paste body
// File: blobfinder.cpp
// What it is
//
// Input: 
// Output: 
//
// Class: CS 215								Instructor: Dr. Deborah Hwang
// Assignment: Project 2					Date assigned: January 30, 2008
// Programmer: Luanne Benson-Lender		Date completed: February 6, 2008

#include <iostream>
#include<fstream>

using namespace std;

const int MAX_ROWS=52;
const int MAX_COLS=52;

void ProcessBlob(char array[MAX_ROWS][MAX_COLS], int row, int col, char blob, int num);
void PrintGrid(char array[MAX_ROWS][MAX_COLS], int dim);

int main(int argc, char * argv[])
{
	ifstream inStream(argv[1]);
	char blobArray[MAX_ROWS][MAX_COLS];
	int dim, num=0;
	char blob='a';

	inStream >> dim;
	
	for (int i=0; i < dim+2; i++)
		for (int j=0; j < dim+2; j++)
			blobArray[i][j]='-';

	for (int i=1; i <= dim; i++)
		for (int j=1; j <= dim; j++)
			inStream >> blobArray[i][j];

	for (int i=1; i <= dim; i++)
		for (int j=1; j <= dim; j++)
			ProcessBlob(blobArray, i, j, blob, num);

	cout << "There are " << num << "blob(s) in this file." << endl << endl;
	
	PrintGrid(blobArray, dim);

	inStream.close();
	
	return 0;
}  // end main

// Function: ProcessBlob
// Checks a cell for its inclusion in a blob, then checks the surrounding cells recursively
void ProcessBlob(char array[MAX_ROWS][MAX_COLS], int & row, int & col, char & blob, int & num)
{
	if (array[row][col]=='*')
	{
		array[row][col]=blob;

		ProcessBlob(array, row + 1, col, blob, num);
		ProcessBlob(array, row, col + 1, blob, num);
		ProcessBlob(array, row - 1, col, blob, num);
		ProcessBlob(array, row, col - 1, blob, num);

		blob++;
		num++;
	}
	return;
}  // end ProcessBlob

// Function: PrintGrid
// Prints out an array
void PrintGrid(char array[MAX_ROWS][MAX_COLS], int dim)
{
	for (int i=1; i <= dim; i++)
	{		
		for (int j=1; j <= dim; j++)
			cout << array[i][j];
		cout << endl;
	}
}  // end PrintGrid