Process Results of a Competition

Process Results of a Competition

 In this assignment you are asked to develop a menu driven program that will be used to analyze and display the results of a C++ programming contest.

Your program will first read information about the different teams participating in the contest into a dynamically allocated array of type struct (called teams). Each element of this array contains the id of the team (integer), the name of the team(c-string), and the first and last names of the three members in the team (c-strings). The information is stored in a text file called teams.txt, as shown in 

Figure.1. Note that the number of teams is not known.

Your program will then read from another text file called performance.txt the results obtained by the different teams participating in the contest and store a summary of their performance in the previously allocated array teams.

Each line of the file performance.txt contains the team id (integer number the same as the one in teams.txt), the number of problems solved by the team, followed by the time spent in solving each problem. The time is saved in the form of a pair of integer numbers hr, mn, where hr represents the number of hours and mn represents the number of minutes.

Figure_2 shows a sample contents of performance.txt file.

The performance summary saved in the array teams consists of the number of problems solved by the team, and the total time in minutes needed to solve those problems.

Finally, your program will display the following 3-item menu:

D —————–> Display final Results

F ——————> Find results of a specific team

Q ——————> Quit the program

Your main() function should call the following functions:

……readTeams(…): that reads the information stored in teams.txt into one dynamic array called teams.

……readResults(…): that reads the information stored in performance.txt into the dynamically allocated array teams. An error message is displayed if the team ID is not known (i.e. not read from teams.txt).

……sortTeams(): that sorts the array teams in decreasing order of their performance.

The performance of a team is measured by the number of problems solved and then the time needed to solve them. For example, if team A, team B, and team C have solved 2, 3 and 2 problems respectively and it took them 1h 5min, 2h 10min and 55min, respectively to solve the problems, the sorted array teams will contain team information in the following order:

  1. Team B because it solved the maximum number of problems (3 problems)
  2. Team C because it solved 2 problems like team A but needed less time (55 min instead of 65 min)
  3. The last team in the array will be Team A.

……displayResults(…): Called when Option D is chosen by the user; it displays the sorted list of teams, where for each team, it displays its id, name and the first name and last name of each one of its members, the number of problems solved and the time (in minutes) it took them to solve the problems

……findResults(…):Called when Option F is chosen by the user; it asks the user to enter a Team name and displays for that team, if found, its ID, name, the number of problems it solved, the time (in minutes) needed to solve those problems and its final rank among the participants of the contest. If the team is not found an error message is displayed.

1111 Geeks Ahmed Al-Barri Saad Al-Bahri Riyad Al-Sahili

2222 smarties Badria Al-Alamia Hind Al-Sumaria Hajer Al-Masria

3333 Fighters Salma A-Umaria Najat Al-Hadharia NoorElAin Al-Majdoubiya

4444 Samurais Aladine Al-Mahmoudi Nassereddine Al-Kuwari Haitham Al-Sallami

Figure.1: Sample contents of teams.txt file.

4444 1 0 35

1111 2 1 10

3333 2 0 55

2222 3 2 10

Figure.2: Sample contents of performance.txt file.

Other requirements:

  1. You should make sure that adequate error messages are displayed whenever it is needed.
  2. You should allocate only the needed amount of memory
  3. You should use only C-strings to hold your string data.

Solution 

#include <iostream>

#include <fstream>

#include <cstring>

#define MAX_LEN 32

using namespace std;

struct Team

{

int id;

char name[MAX_LEN];

char member1[MAX_LEN];

char member2[MAX_LEN];

char member3[MAX_LEN];

int numProblems;

int solvingTime;

};

Team *readTeams(int &nTeams)

{

int id;

char ch;

char name[MAX_LEN];

char first1[MAX_LEN], last1[MAX_LEN], first2[MAX_LEN],

last2[MAX_LEN], first3[MAX_LEN], last3[MAX_LEN];

ifstream infile(“teams.txt”);

// make sure the file is readable

if (!infile.good())

{

cout << “Cannot read the input file teams.txt” << endl;

return NULL;

}

nTeams = 0;

// count the number of lines

while (infile.get(ch))

{

nTeams += ch == ‘\n’;

}

// allocate memory

Team *teams = new Team[nTeams];

// back to the start of file

infile.clear();

infile.seekg(0);

int i = 0;

// read teams into array

while (infile >> id >> name >> first1 >> last1 >> first2 >> last2 >> first3 >> last3)

{

teams[i].id = id;

sprintf(teams[i].name,  “%s”, name);

sprintf(teams[i].member1, “%s %s”, first1, last1);

sprintf(teams[i].member2, “%s %s”, first2, last2);

sprintf(teams[i].member3, “%s %s”, first3, last3);

teams[i].numProblems = 0; // initialize with

teams[i].solvingTime = 0; // zero so far

i++;

}

 

infile.close();

return teams;

}

void readResults(Team teams[], int nTeams)

{

int id, nProblems, hours, minutes;

ifstream infile(“performance.txt”);

// make sure the file is readable

if (!infile.good())

{

cout << “Cannot read the input file performance.txt” << endl;

return;

}

while (infile >> id >> nProblems >> hours >> minutes)

{

bool found = false;

for (int i = 0; i < nTeams; i++)

{

if (teams[i].id == id)

{

found = true;

teams[i].numProblems = nProblems;

teams[i].solvingTime = 60 * hours + minutes; // time in minutes

break; // no need to continue

}

}

if (!found)

{

cout << “Unknown Team with ID ” << id << endl;

}

}

infile.close();

}

void sortTeams(Team teams[], int nTeams)

{

for (int i = 0; i < nTeams; i++)

{

Team t = teams[i];

int j = i – 1;

while ( j >= 0)

{

if (teams[j].numProblems > t.numProblems)

break;  // t has more problems solved

else if (teams[j].numProblems == t.numProblems && teams[j].solvingTime < t.solvingTime)

break; // t solved the same number of problems faster

teams[j + 1] = teams[j];

j = j – 1;

}

teams[j + 1] = t;

}

}

void displayResults(Team teams[], int nTeams)

{

for (int i = 0; i < nTeams; i++)

{

cout << “Team ID: ” << teams[i].id << “, Name: ” << teams[i].name << endl;

cout << “Team Members: ” << teams[i].member1 << “, ” << teams[i].member2 << “, ” << teams[i].member3 << endl;

cout << teams[i].numProblems << ” Problems Solved in ” << teams[i].solvingTime << ” min” << endl;

cout << endl;

}

}

void findResults(Team teams[], int nTeams)

{

char name[MAX_LEN];

cout << “Enter a Team name: “;

cin >> name;

bool found = false;

for (int i = 0; i < nTeams; i++)

{

if (strcmp(teams[i].name, name) == 0)

{

found = true;

cout << “Team ID: ” << teams[i].id << “, Name: ” << teams[i].name << endl;

cout << teams[i].numProblems << ” Problems Solved in ” << teams[i].solvingTime << ” min” << endl;

cout << “The final rank among the participants: ” << i + 1 << endl;

cout << endl;

break; // no need to continue

}

}

if (!found)

{

cout << “Team with the name  ” << name << ” does not exist” << endl;

cout << endl;

}

}

int main(int argc, char **argv)

{

char choice;

int nTeams = 0;

Team *teams = readTeams(nTeams);

readResults(teams, nTeams);

sortTeams(teams, nTeams);

bool quit = false;

while (!quit)

{

cout << “[D]isplay final Results” << endl;

cout << “[F]ind results of a specific Team” << endl;

cout << “[Q]uit the program” << endl;

cout << “>> “;

cin >> choice;

if (choice == ‘d’ || choice == ‘D’)

{

displayResults(teams, nTeams);

}

else if (choice == ‘f’ || choice == ‘F’)

{

findResults(teams, nTeams);

}

else if (choice == ‘q’ || choice == ‘Q’)

{

quit = true;

}

else

{

cout << “Invalid menu option” << endl;

cout << endl;

}

}

delete[] teams;

return 0;

}