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

* Description: Definition of SporkProfile for storing profile data for a restaua

ID: 3751650 • Letter: #

Question

* Description: Definition of SporkProfile for storing profile data for a restauarant/business

* declaration of functions for reading, processing, and writing Spork profiles.

*

*/

/**************************************************************************************************/

#ifndef SPORKPROFILE_H

#define SPORKPROFILE_H

/**************************************************************************************************/

#include <stdbool.h>

/**************************************************************************************************/

#define MAX_BUSINESSNAME_LEN 100

/**************************************************************************************************/

/*

* Data structure for representing a Spork profile for restauarant/business. businessName is the

* name of the restauarant/business. locX and locY are the x and y coordinates of the business.

* avgRating is the average rating for the business. adLevel is a value ranging from 0 to 2

* describing the business advertising level. isNearby is used to indicate if the business is near

* the user's location. isGood indicates if the business is greater than or equal to

* the user's minimum desired rating.

*

*/

typedef struct SporkProfile_struct {

   char businessName[MAX_BUSINESSNAME_LEN];

   double locX;

   double locY;

   double distMiles;

   double avgRating;

   int adLevel;

   bool isNearby;

   bool isGood;

} SporkProfile;

/**************************************************************************************************/

/* Reads up to maxProfiles Spork profiles from an input file specified by fileName. Functions reads

* the input file line-by-line using the format:

*

* BusinessName X.XX Y.YY R.RR A

*

* BusinessName is the name of the restaurant/business. The business name will not include any

* whitespace characters

*

* X.XX represents the X location in miles using a Cartesian coodinate system

*

* Y.YY represents the Y location in miles using a Cartesian coodinate system

*

* R.RR represents the average rating for the business

*

* A is the advertising level, which should 0, 1, or 2

*

* Alpha submission: ReadSporkDataFromFile() function should initialize isNearby and isGood

* to true and initialize distMiles to 0.0 for all profile entries.

* Note: This requirement is different for the final project submission.

*

* Project submission: ReadSporkDataFromFile() function should initialize isNearby and isGood

* to false for all profile entries.

* Note: This requirement is different for the alpha project submission.

*

* Returns: The number of Sprok profiles read from the file. If the input file could not be opened,

* the function returns -1.

*

*/

int ReadSporkDataFromFile(SporkProfile sporkProfiles[], int maxProfiles,

char *fileName);

/**************************************************************************************************/

/*

* Determines if each business is nearby the user, sets the Spork profile's isNearby flag to

* true (is nearby) or false (not nearby), and stores the distance in miles in the Spork profile.

*

* userLocX, userLocY: Indicates the x and y coordiante of the user's location

* maxDist: Indicates the maxmimum distance between the user and a nearby business

*

*/

void FindNearbyBusinesses(SporkProfile sporkProfiles[], int numProfiles,

double userLocX, double userLocY, double maxDist);

/**************************************************************************************************/

/*

* Determines if each business is good based on the user's minimum required average rating.

* Sets the Spork profile's isGood flag to true if the business' avgRating is greater than or

* equal to minRating, and false otherwise.

*

*/

void FindGoodBusinesses(SporkProfile sporkProfiles[], int numProfiles,

double minRating);

/**************************************************************************************************/

/*

* Returns the index of the Spork profile that is neary, good, and has the highest adLevel. If

* there is a tie, the index of the first entry found with the highest ad level should be returned.

* If no businesses are nearby and good, the function will return -1.

*

*/

int GetIndexMaxSponsor(SporkProfile sporkProfiles[], int numProfiles);

/**************************************************************************************************/

/*

* Writes all good and nearby business to an output file specified by fileName using the format:

*

* BusinessName R.RR D.DD

*

* R.RR is the average rating with exactly two decimal digits of precision.

* D.DD is the distance in miles with exactly two decimal digits of precision.

*

* If maxSponsorIndex is not -1, the cooresponding profile entry will be output first. All other

* nearby and good profiles will be output in the order they are stored in the sporkProfiles array.

*

* Each entry should be separated by a single tab character ( ), and each line should end

* with a single newline ( ).

*

* Returns: -1 if the output file could not be opened, and 0 otherwise.

*

*/

int WriteSporkResultsToFile(SporkProfile sporkProfiles[], int numProfiles,

int maxSponsorIndex, char *fileName);

/**************************************************************************************************/

#endif

/**************************************************************************************************/

Explanation / Answer

I am writing the code for the alpha submission -

Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

#ifndef SPORKPROFILE_H
#define SPORKPROFILE_H
#include<bits/stdc++.h>
#define MAX_BUSINESSNAME_LEN 100
typedef struct SporkProfile_struct {

char businessName[MAX_BUSINESSNAME_LEN];

double locX;

double locY;

double distMiles;

double avgRating;

int adLevel;

bool isNearby;

bool isGood;

} SporkProfile;

/* Reads up to maxProfiles Spork profiles from an input file specified by fileName. Functions reads

* the input file line-by-line using the format:

*

* BusinessName X.XX Y.YY R.RR A

*

* BusinessName is the name of the restaurant/business. The business name will not include any

* whitespace characters

*

* X.XX represents the X location in miles using a Cartesian coodinate system

*

* Y.YY represents the Y location in miles using a Cartesian coodinate system

*

* R.RR represents the average rating for the business

*

* A is the advertising level, which should 0, 1, or 2

*

* Alpha submission: ReadSporkDataFromFile() function should initialize isNearby and isGood

* to true and initialize distMiles to 0.0 for all profile entries.

* Note: This requirement is different for the final project submission.

*

* Project submission: ReadSporkDataFromFile() function should initialize isNearby and isGood

* to false for all profile entries.

* Note: This requirement is different for the alpha project submission.

*

* Returns: The number of Sprok profiles read from the file. If the input file could not be opened,

* the function returns -1.

*

*/

int ReadSporkDataFromFile(SporkProfile sporkProfiles[], int maxProfiles,char *fileName)
{
ifstream file; //To read from a file
file.open(fileName);
number_of_profiles=0;
if(file.is_open())
{
int A;
double X,Y,R;
char *name;
int index,prev; //To store index of whitespace in file
string line; //To store each line from the file
string temp; //For temporary purpose
stringstream ss; //conversion from string to numeral

while(getline(file,line))
{
index = line.find(" ");
temp = line.substr(0,index); //temp contains business name
strcpy(sporkProfiles[number_of_profiles].businessName,temp);

prev = index;
index = line.find(prev+1," ");
temp = line.substr(prev+1,index-prev); //temp contains X location in string form
ss<<temp;
ss>>X;
ss.clear();
sporkProfiles[number_of_profiles].locX = X;

prev = index;
index = line.find(prev+1," ");
temp = line.substr(prev+1,index-prev); //temp contains Y location in string form
ss<<temp;
ss>>Y;
ss.clear();
sporkProfiles[number_of_profiles].locY = Y;

prev = index;
index = line.find(prev+1," ");
temp = line.substr(prev+1,index-prev); //temp contains average rating in string form
ss<<temp;
ss>>R;
ss.clear();
sporkProfiles[number_of_profiles].avgRating = R;

prev = index;
temp = line.substr(prev+1); //temp contains advertising level in string form
ss<<temp;
ss>>A;
ss.clear();
sporkProfiles[number_of_profiles].adLevel = A;

sporkProfiles[number_of_profiles].isGood = true;
sporkProfiles[number_of_profiles].isNearby = true;
sporkProfiles[number_of_profiles].distMiles =0.0;
  
number_of_profiles++;
}
file.close(); //close the file
return number_of_profiles;
}
return -1; //file could not be opened
}

/*

* Determines if each business is nearby the user, sets the Spork profile's isNearby flag to

* true (is nearby) or false (not nearby), and stores the distance in miles in the Spork profile.

*

* userLocX, userLocY: Indicates the x and y coordiante of the user's location

* maxDist: Indicates the maxmimum distance between the user and a nearby business

*

*/

void FindNearbyBusinesses(SporkProfile sporkProfiles[], int numProfiles,double userLocX, double userLocY, double maxDist)
{
double dist; //Stores distance from user to business place
for(int i=0;i<numProfiles;i++)
{
dist = sqrt ( pow((sporkProfiles[i].locX - userLocX),2) + pow((sporkProfiles[i].locY - userLocY),2) );
if(dist < maxDist) //compare distances
sporkProfiles[i].isNearby = true;
}
}

/*

* Determines if each business is good based on the user's minimum required average rating.

* Sets the Spork profile's isGood flag to true if the business' avgRating is greater than or

* equal to minRating, and false otherwise.

*

*/

void FindGoodBusinesses(SporkProfile sporkProfiles[], int numProfiles,double minRating)
{
for(int i=0;i<numProfiles;i++)
{
if(sporkProfiles[i].avgRating < minRating) //compare distances
sporkProfiles[i].isGood = true;
}
}

/*

* Returns the index of the Spork profile that is neary, good, and has the highest adLevel. If

* there is a tie, the index of the first entry found with the highest ad level should be returned.

* If no businesses are nearby and good, the function will return -1.

*

*/

int GetIndexMaxSponsor(SporkProfile sporkProfiles[], int numProfiles)
{
int temp = -1;
for(int i=0;i<numProfiles;i++)
{
if(sporkProfiles[i].isGood == true && sporkProfiles[i].isNearby == true)
{
if(temp < sporkProfiles[i].adLevel)
{
temp = sporkProfiles[i].adLevel;
}
}
}
if(temp == -1)
return -1; //None of the business is nearby and good

for(int i=0;i<numProfiles;i++)
{
if(sporkProfiles[i].isGood == true && sporkProfiles[i].isNearby == true)
{
if(temp == sporkProfiles[i].adLevel)
{
return i;
}
}
}
}
/*

* Writes all good and nearby business to an output file specified by fileName using the format:

*

* BusinessName R.RR D.DD

*

* R.RR is the average rating with exactly two decimal digits of precision.

* D.DD is the distance in miles with exactly two decimal digits of precision.

*

* If maxSponsorIndex is not -1, the cooresponding profile entry will be output first. All other

* nearby and good profiles will be output in the order they are stored in the sporkProfiles array.

*

* Each entry should be separated by a single tab character ( ), and each line should end

* with a single newline ( ).

*

* Returns: -1 if the output file could not be opened, and 0 otherwise.

*

*/

int WriteSporkResultsToFile(SporkProfile sporkProfiles[], int numProfiles,int maxSponsorIndex, char *fileName)
{
ofstream output_file; //To write in a file
output_file.open(fileName);
if(output_file.is_open())
{
for(i=0;i<numProfiles;i++)
output_file<<sporkProfiles[i].businessName<<" "<<sporkProfiles[i].avgRating<<" "<<sporkProfiles[i].distMiles<<" ";
output_file.close();
return 0;
}
return -1; //File could not be opened
}
#endif

Hope i have answered your question satisfactorily.Leave doubts in comment section if any