Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There\'s issue in this code, but I don\'t know where is it. Please help me find

ID: 3591650 • Letter: T

Question

There's issue in this code, but I don't know where is it. Please help me find the issues/problems in the code. Below is an sample output of the code and my code.

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

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

#include <iostream>

#include <fstream>

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");

   cout << "The stars are located at:" << endl;

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

   {

       cout << x + 1 << "/t";

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

       {

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

       {

           fout << "* ";

           cout << "(" << (x+1) << "," << (y+1) << ")";

       }

       else

       {

           fout << ". ";

       }

}

           fout << endl;

           cout << 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)

{

   // check brighter for all 8 adjacent cells

   bool bright1 = brighter(x - 1, y, map[x][y]);

   bool bright2 = brighter(x - 1, y - 1, map[x][y]);

   bool bright3 = brighter(x, y - 1, map[x][y]);

   bool bright4 = brighter(x + 1, y-1, map[x][y]);

   bool bright5 = brighter(x + 1, y , map[x][y]);

   bool bright6 = brighter(x + 1, y + 1, map[x][y]);

   bool bright7 = brighter(x, y + 1, map[x][y]);

   bool bright8 = brighter(x - 1, y + 1, map[x][y]);

   return (bright1 && bright2 && bright3 && bright4

&& bright5 && bright6 && bright7 && bright8);

}

int main()

{

   StarFinder sf;

   sf.clear();

   sf.readIn();

   sf.printOut();

   sf.findStars();

   return 0;

}

Explanation / Answer

#include <iostream>
#include <fstream>
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");

cout << "The stars are located at:" << endl;

for (int x = 0; x < stopX; x++)
{
cout << x + 1 << " ";

for (int y = 0; y < stopY; y++)
{
if (isStar(x,y) == true)
{
fout << "* ";
cout << "(" << (x+1) << "," << (y+1) << ")";
}
else
{
fout << " ";
}
}
fout << endl;
cout << 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)
{
// check brighter for all 8 adjacent cells

bool bright1 = brighter(x - 1, y, map[x][y]);

bool bright2 = brighter(x - 1, y - 1, map[x][y]);

bool bright3 = brighter(x, y - 1, map[x][y]);

bool bright4 = brighter(x + 1, y-1, map[x][y]);

bool bright5 = brighter(x + 1, y , map[x][y]);

bool bright6 = brighter(x + 1, y + 1, map[x][y]);

bool bright7 = brighter(x, y + 1, map[x][y]);

bool bright8 = brighter(x - 1, y + 1, map[x][y]);

return (bright1 && bright2 && bright3 && bright4
&& bright5 && bright6 && bright7 && bright8);
}


int main()
{
StarFinder sf;

sf.clear();
sf.readIn();
sf.printOut();
sf.findStars();

return 0;
}

There were two minor issues like tab charcter is and not /t, and in file you were putting . but it should not be put as per example

So only thing remain is based on requirement, do you want to save * in file or print on console apart from that overall logic is right.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote