Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have some issues/problems with my code that I'm not quit get it. I don't know what do boolean isStar do in this code? Is

I have some issues/problems with my code that I'm not quit get it. I don't know what do boolean isStar do in this code? Is there any things that I am missing? Please help me out and give me an explanation of it. Below is information and my code. I'll thank you so much for your help.

==============================================================================

Input data: The input to the program will come from a pure ASCII text data file. Download a copy of the attached file, or create your own variant using any basic text editing program. The data set will consist of rows of numbers separated by a delimiter (such as a space or comma). The first line will consist of two integers (R and C) for the number rows and columns in the data set. R and C will be in the range of 10 to 20.

While the first two numbers tell the number of rows and columns in the illumination data set, the rest of the numbers are an array of relative brightness.

Go ahead and use the data I included as an attachment to this assignment as your first data set. Right click on the rawdata.txt file and save it to your computer. It can also serve as a template for the other two data sets you need to create for this assignment.

Analysis: For the purposes of this project, a "star" is a cell in the array whose brightness is at least 4 greater than each of the adjacent cells. A cell in general position has 8 adjacent cells. An edge cell has 5 adjacent, and a corner cell has 3 adjacent.

Output: Your program will echo the input data, then show an array with asterisks "*" where there are stars, and spaces where there are not stars, then a list of coordinates where there are stars. Send the output to a file for the final test. Run at least three sets of numbers through the program for testing purposes. One of them must be the following example, so go ahead and use its contents to create your second data set. The third data set should consist of values you created yourself.

Example:

Input data: 
 
10 10 
1 1 2 1 3 2 7 2 1 6 
2 1 6 2 6 2 1 2 1 1 
1 2 1 2 1 2 1 2 1 2 
7 1 2 4 1 2 1 7 1 1 
2 1 2 7 1 2 1 1 2 1 
1 2 1 2 1 5 1 2 7 1 
1 2 1 2 1 1 7 1 1 2 
2 1 1 2 1 1 1 1 2 1 
1 1 9 5 1 1 2 1 1 1 
1 1 2 1 2 1 2 1 2 1 
 
Output: 
 
Here is the data: (the top and left hand rows are not required output, they are there to make it easier to read the array data) 
 1 2 3 4 5 6 7 8 9 10 
 1 1 1 2 1 3 2 7 2 1 6 
 2 2 1 6 2 6 2 1 2 1 1 
 3 1 2 1 2 1 2 1 2 1 2 
 4 7 1 2 4 1 2 1 7 1 1 
 5 2 1 2 7 1 2 1 1 2 1 
 6 1 2 1 2 1 5 1 2 7 1 
 7 1 2 1 2 1 1 7 1 1 2 
 8 2 1 1 2 1 1 1 1 2 1 
 9  1 1 9 5 1 1 2 1 1 1 
 10 1 1 2 1 2 1 2 1 2 1 
 
Here are the stars: (the top and left hand rows are not required output, there are they to make it easier to read the array data) 
 1 2 3 4 5 6 7 8 9 10 
 1 * * 
 2 * 
 3 
 4 * * 
 5 
 6 * 
 7 
 8 
 9 * 
 10 
 
The stars are located at: 
 1 ( 1, 7) ( 1,10) 
 2 ( 2, 3) 
 3 
 4 ( 4, 1) ( 4, 8) 
 5 
 6 ( 6, 9) 
 7 
 8 
 9 ( 9, 3) 
 10 

=========================================================================================

#include

#include

using namespace std;

const int MAPSIZE = 20;

typedef int StarMap[MAPSIZE][MAPSIZE];

class StarFinder

{

public:

void clear();

void readIn();

void printOut();

void findStars();

private:

StarMap map;

ifstream fin;

ofstream fout;

int stopX, stopY;

bool brighter(int, int, int);

bool isStar(int, int);

};

//This function clears the array so that extraneous data if not present when created

void StarFinder::clear()

{

for (int x = 0; x < MAPSIZE; x++)

for (int y = 0; y < MAPSIZE; y++)

map[x][y] = '.';

}//end clear

//This function reads in the file with the star data

void StarFinder::readIn()

{

fin.open("map1.txt");

fin >> stopX >> stopY;

for (int x = 0; x < stopX; x++)

{

for (int y = 0; y < stopY; y++)

fin >> map[x][y];

}

fin.close();

}//end readIn

//This function prints out the contents of the file to ensure it was read in correctly

void StarFinder::printOut()

{

for (int x = 0; x < stopX; x++)

{

for (int y = 0; y < stopY; y++)

cout << map[x][y] << " ";

cout << endl;

}

cout << endl;

}//end printOut

//This function will make sure you stay in the bounds of your array

//It also returns true or false whether a given location qualifies as a star

bool StarFinder::brighter(int x, int y, int location)

{

if (x < 0 || y < 0 || x >= stopX || y >= stopY)

return true;

else

return (location >= map[x][y] + 4);

}// end brighter

void StarFinder::findStars()

{

fout.open("map1done.txt");

for (int x = 0; x < stopX; x++)

for (int y = 0; y < stopY; y++)

{

if (isStar(x,y) == true)

{

fout << "* ";

}

else

fout << ". ";

{

fout << endl;

}

fout.close();

}

}//end findStars

//This function checks each of the right directions around a location on the map to

//determine if that location qualifies as a star by calling the brighter function

bool StarFinder::isStar(int x, int y)

{

return (brighter(x - 1, y - 1, map[x][y]) && (brighter(x + 1, y + 1, map[x][y]));

}

int main()

{

StarFinder sf;

sf.clear();

sf.readIn();

sf.printOut();

sf.findStars();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions