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

C++ In this program, I had to read in a text file from a judging competition whe

ID: 3707347 • Letter: C

Question

C++

In this program, I had to read in a text file from a judging competition where there were 10 players with 6 scores each. The highest and lowest score for each player was thrown out and the remaining four are averaged to calculate their score. Then, first place, second place, third, and so on are displayed with their average score next to them. I have functions for lowest and highest (I think), but I don't know how to pass data into them. The text file and my code are below.

To reiterate, my problem is that I need to eliminate the highest and lowest score for each player using functions and then display, in order, their place in the competition.

Text File:

"Competition.txt"

Squidward 5.3 4.5 3.2 3.5 3.7 4.3

Sandy 6.0 6.0 6.0 6.0 6.0 6.0

Spongebob 3.0 5.5 4.5 6.0 3.9 4.4

Patrick 2.0 1.7 1.5 2.6 2.6 4.1

Larry 3.7 4.6 2.9 4.5 4.1 3.4

Plankton 2.5 2.7 2.7 2.1 3.0 3.1

Krabs 4.5 4.3 3.5 3.7 4.0 3.3

Gary 3.5 2.6 3.4 1.5 1.8 3.0

Fred 5.5 2.3 4.3 2.1 2.9 3.1

Pearl 2.6 4.6 5.6 3.2 2.3 3.5

Source Code:

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

struct Diver

{

float score;

char name[25];

};

int const Scores = 6;

int const Names = 10;

double lowestScore(const double comp[Names][Scores]);

double highestScore(const double comp[Names][Scores]);

int numCompare(const void *a, const void *b);

int main()

{

string inText;

Diver competitors[10];

double compScores[6];

double total = 0;

system("chdir");

ifstream inComp("Competition.txt");

if (inComp.is_open())

{

cout << "File Opened" << endl;

}

else

{

cout << "File Not Found" << endl;

}

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

{

inComp >> inText;

strcpy_s(competitors[i].name, inText.c_str());

cout << setw(25) << left << inText << " ";

for (int j = 0; j < 6; j++)

{

inComp >> inText;

compScores[j] = atof(inText.c_str());

total += compScores[j];

cout << fixed << showpoint << setprecision(1) << compScores[j] << " ";

}

qsort(compScores, 6, sizeof(float), numCompare);

competitors[i].score = total / 6.0F;

cout << " " << competitors[i].score << endl;

}

qsort(competitors, 6, sizeof(float), numCompare);

cout << endl << endl;

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

{

cout << setw(25) << left << competitors[i].name << " " << competitors[i].score;

cout << endl;

}

system("pause");

return 0;

}

double highestScore(const double comp[Names][Scores])

{

double highest = comp[0][0];

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

{

for (int j = 0; j < Scores; j++)

{

if (comp[i][j] > highest)

{

highest = comp[i][j];

}

}

}

return highest;

}

double lowestScore(const double comp[Names][Scores])

{

double lowest = comp[0][0];

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

{

for (int j = 0; j < Scores; j++)

{

if (comp[i][j] < lowest)

{

lowest = comp[i][j];

}

}

}

return lowest;

}

int numCompare(const void *a, const void *b)

{

return ((*(int *)b - (*(int *)a)));

}

Explanation / Answer

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

struct Diver

{

float score;

char name[25];

};

int const Scores = 6;

int const Names = 10;

double lowestScore(const double comp[Scores]);

double highestScore(const double comp[Scores]);

int numCompare(const void *a, const void *b);

int main()

{

string inText;

Diver competitors[10];

double compScores[6];

double total = 0;

system("chdir");

ifstream inComp("Competition.txt");

if (inComp.is_open())

{

cout << "File Opened" << endl;

}

else

{

cout << "File Not Found" << endl;

}

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

{

inComp >> inText;

strcpy_s(competitors[i].name, inText.c_str());

cout << setw(25) << left << inText << " ";

for (int j = 0; j < 6; j++)

{

inComp >> inText;

compScores[j] = atof(inText.c_str());

total += compScores[j];

cout << fixed << showpoint << setprecision(1) << compScores[j] << " ";

}

qsort(compScores, 6, sizeof(float), numCompare);

competitors[i].score = (total-(lowestScore(compScores)+highestScore(compScores) )/ 4.0F;

cout << " " << competitors[i].score << endl;

}

qsort(competitors, 6, sizeof(float), numCompare);

cout << endl << endl;

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

{

cout << setw(25) << left << competitors[i].name << " " << competitors[i].score;

cout << endl;

}

system("pause");

return 0;

}

double highestScore(const double comp[Scores])

{

double highest = comp[0];

for (int j = 0; j < Scores; j++)

{

if (comp[j] > highest)

{

highest = comp[j];

}


}

return highest;

}

double lowestScore(const double comp [Scores])

{

double lowest = comp[0];

for (int j = 0; j < Scores; j++)

{

if (comp[j] < lowest)

{

lowest = comp[j];

}

}

return lowest;

}

int numCompare(const void *a, const void *b)

{

return ((*(int *)b - (*(int *)a)));

}