Star Search (C++) A particular talent competition has five judges, each of whom
ID: 3574968 • Letter: S
Question
Star Search (C++)
A particular talent competition has five judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3, are allowed. A performer’s final score is determined by dropping the highest and the lowest score received, then averaging the three remaining scores. Write a program that calculates the score of each performer and determines the winner. Assume there’s only one winner!
Input: Read data from an input file named performers.txt. First create the input file: copy and paste the following data into a new text file: performers.txt
John 7.0 7.8 7.1 7.9 7.5
David 8.3 2.9 9.8 9.2 9.7
Mary 8.3 8.0 8.9 7.9 8.5
Andy 9.1 8.9 9.0 8.7 9.1
Ann 9.0 8.8 9.1 9.7 9.3
Lucy 8.2 8.4 8.9 9.3 8.5
Dan 9.0 5.6 8.9 9.9 7.3
Sue 7.9 8.2 7.6 8.1 8.0
Write a program that reads and calculates performers’ data from a file into two parallel arrays, a name array and a score array:
The program writes the sorted arrays to another file, named out_1.txt.
It sorts the parallel arrays in descending order (from highest score to the lowest score)
then writes the sorted arrays to another file, named out_2.txt.
Finally, it sorts the parallel arrays in alphabetical and saves the sorted arrays to out_3.txt.
Explanation / Answer
// C++ code
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <iomanip> // std::setw
using namespace std;
void sortscore(string name[], double score[])
{
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 8; ++j)
{
if(score[i] > score[j])
{
double t = score[i];
score[i] = score[j];
score[j] = t;
string nme = name[i];
name[i] = name[j];
name[j] = nme;
}
}
}
}
void sortname(string name[], double score[])
{
for (int i = 0; i < 8; ++i)
{
for (int j = 0; j < 8; ++j)
{
if(name[i] > name[j])
{
double t = score[i];
score[i] = score[j];
score[j] = t;
string nme = name[i];
name[i] = name[j];
name[j] = nme;
}
}
}
}
int main ()
{
double number;
string name[8];
double score[8];
double total;
int i =0;
ifstream inputFile ("performance.txt");
if (inputFile.is_open())
{
while(true)
{
total = 0;
inputFile >> name[i];
for (int j = 0; j < 5; ++j)
{
inputFile >> number;
total = total + number;
}
score[i] = total/5;
i++;
if(inputFile.eof())
break;
}
}
else cout << "Unable to open file";
inputFile.close();
cout << "Input arrays ";
for (int j = 0; j < 8; ++j)
{
cout << name[j] << " " << score[j] << endl;
}
sortscore(name,score);
ofstream outFile ("out_2.txt");
if (outFile.is_open())
{
for (int j = 0; j < 8; ++j)
{
outFile <<left << setw(10) << name[j] << " " << score[j] << endl;
}
}
else cout << "Unable to open file";
sortname(name,score);
ofstream outtFile ("out_3.txt");
if (outtFile.is_open())
{
for (int i = 0; i < 8; ++i)
{
outtFile <<left << setw(10) << name[i] << " " << score[i] << endl;
}
}
else cout << "Unable to open file";
return 0;
}
/*
performance.txt
John 7.0 7.8 7.1 7.9 7.5
David 8.3 2.9 9.8 9.2 9.7
Mary 8.3 8.0 8.9 7.9 8.5
Andy 9.1 8.9 9.0 8.7 9.1
Ann 9.0 8.8 9.1 9.7 9.3
Lucy 8.2 8.4 8.9 9.3 8.5
Dan 9.0 5.6 8.9 9.9 7.3
Sue 7.9 8.2 7.6 8.1 8.0
output:
Input arrays
John 7.46
David 7.98
Mary 8.32
Andy 8.96
Ann 9.18
Lucy 8.66
Dan 8.14
Sue 7.96
out_2.txt
Ann 9.18
Andy 8.96
Lucy 8.66
Mary 8.32
Dan 8.14
David 7.98
Sue 7.96
John 7.46
out_3.txt
Sue 7.96
Mary 8.32
Lucy 8.66
John 7.46
David 7.98
Dan 8.14
Ann 9.18
Andy 8.96
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.