#include #include #include "Golfer.h" using namespace std; const int MAX_PLAYERS = 250; void getData(Golfer players[],int &numPlayers); double getHandicapAverage(int numPlayers, Golfer const players[]); int findBestGolfer(Golfer players[], int numPlayers) { int best = 0; for (int i = 1; i < numPlayers; i++) //if (players[i].getHandicap() < players[best].getHandicap()) if (players[i] > players[best]) best = i; return best; } void swap(Golfer &p1, Golfer &p2) { Golfer temp = p1; p1 = p2; p2 = temp; } int main() { Golfer players[MAX_PLAYERS]; int numPlayers = 0; getData(players,numPlayers); for (int i = 0; i < numPlayers; i++) cout << i << " - " << players[i].getName() << " - " << players[i].getHandicap() << endl; int best = findBestGolfer(players, numPlayers); cout <<"best is " << players[best].getName() << endl; //swap(players[0],players[best]); players[0].swap(players[best]); /* cout << "all the players better than " << players[0].getName() << endl; for (int i = 1; i < numPlayers; i++) if (players[i] > players[0]) cout << players[i].getName() <<" is better." << endl; */ Golfer g; g.setName("Tiger"); g.setHandicap(0); g.setEMail("tiger@gmail.com"); cout << g.getName() << endl; players[numPlayers] = g; numPlayers++; cout <<"average = " << getHandicapAverage(numPlayers, players) << endl; for (int i = 0; i < numPlayers; i++) cout << i << " - " << players[i].getName() << endl; /* int handicap; cout << "Enter a handicap: "; cin >> handicap; cout << "The players with a handicap less than " << handicap < handicaps[i]) cout << players[i] << endl; double average = findAverage(handicaps, numPlayers); cout << "The players with a handicap less than " << average < handicaps[i]) cout << players[i] << endl; string target = "Frank"; int position = findPlayer(players, target, numPlayers); if (position != -1) cout << target << " at position " << position << endl; else cout << target << " not found" << endl; */ return 0; } void getData(Golfer players[],int &numPlayers) { ifstream inFile; inFile.open("golf.txt"); string player; int handicap; string eMail; inFile >> player; while (inFile) { inFile >> eMail; inFile >> handicap; cout << player << " " <> player; } } double getHandicapAverage(int numPlayers, Golfer const players[]) { int total = 0; for (int i = 0; i < numPlayers; i++) { total = total + players[i].getHandicap(); } return static_cast(total) / numPlayers; }