C++ program. plz write in formal indentation and format, while follow the instru
ID: 3910258 • Letter: C
Question
C++ program. plz write in formal indentation and format, while follow the instruction on the program written by the teacher. Thank you very much.
Project Performers 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 Dan Mary 8.3 8.0 8.97.9 8.5 Andrew 9.1 8.9 9.0 8.7 9.1 Sue Linda 8.2 8.4 8.9 9.3 8.5 David 9.0 5.6 8.9 9.9 7.3 Ann 8.3 2.9 9.89.2 9.7 9.0 8.8 9.1 9.7 9.3 7.9 8.2 7.6 8.1 8.0 You may assume that a performer is identified by its unique first name (no two performers have the same first name) Output: Write the number of participants, the name of the winner and the winner's score to the screen. Write to a file named results.txt the names of the performers and their final score /NOTE: You must write documentation for each function - see next pageExplanation / Answer
Hello, I have answered similar question before, so I’m referring it here. Below is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks.
//code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//structure to represent a performer
struct performer{
string name;
double scores[5];
double finalscore;
};
//updated method prototypes
void printInfo(void);
bool getScores(ifstream&,performer&);
double calcScore(performer p);
double findLowest(performer p);
double findHighest(performer p);
void writeScore(ofstream &outFile,performer current);
void printEnd(void);
int main()
{
// declare the variables needed in main()
int count=0;
performer p,winner;
printInfo();
// open the input file + validation
ifstream inFile("performers.txt");
if(!inFile){
cout<<"input file not found"<<endl;
return 1;
}
// open the output file + validation
ofstream outFile("results.txt");
if(!inFile){
cout<<"output file cannot be created"<<endl;
return 1;
}
//looping through all records
while (getScores(inFile,p))
{
//calculating final score
double score=calcScore(p);
//setting final score to the struct object
p.finalscore=score;
// determine the winner so far
if(count==0){
//first entry
winner=p;
}else if(score>winner.finalscore){
winner=p;
}
//writing current player's data to the file
writeScore(outFile,p);
count++;
}
// close the input file
inFile.close();
// close the output file
outFile.close();
// display the number of participants
cout<<" There are "<<count<<" participants"<<endl;
// display the winner and the winner's score
if(count!=0){
cout<<"Winner is "<<winner.name<<" with final score of "<<winner.finalscore<<endl;
}
printEnd();
return 0;
}
/*
method to print a description about the program
*/
void printInfo(void)
{
cout << "The talent competition has five judges, each of whom awards a score between 0 and 10 to each performer"<<endl;
cout<<"Fractional scores, such as 8.3, are allowed."<<endl;
cout <<"A performer's final score is determined by dropping the highest and the lowest score received,"<<endl;
cout<<"then averaging the three remaining scores."<<endl;
cout<<"This program calculates the score of each performer and determines the winner." << endl<<endl;
}
/*
method to check if there are any more performers left on the inFile file object to be opened
if yes, get the performer details to the performer variable p and return true, else false
*/
bool getScores(ifstream &inFile,performer &p)
{
string name;
if(inFile>>name){
p.name=name;
//reading 5 scores
for(int i=0;i<5;i++){
inFile>>p.scores[i];
}
return true;
}
return false;
}
/*
* method to calculate and return the final score of a player
*/
double calcScore(performer p)
{
//finding highest and lowest scores
double low=findLowest(p);
double high=findHighest(p);
double total=0;
//finding total excluding highest and lowest scores
for(int i=0;i<5;i++){
if(p.scores[i]!=low && p.scores[i]!=high){
total+=p.scores[i];
}
}
//averaging the results
double average=total/3.0;
return average;
}
/*
* method to calculate and return the lowest score of a player
*/
double findLowest(performer p)
{
double low;
for(int i=0;i<5;i++){
if(i==0){
low=p.scores[i];
}else if(p.scores[i]<low){
low=p.scores[i];
}
}
return low;
}
/*
* method to calculate and return the highest score of a player
*/
double findHighest(performer p)
{
double high;
for(int i=0;i<5;i++){
if(i==0){
high=p.scores[i];
}else if(p.scores[i]>high){
high=p.scores[i];
}
}
return high;
}
/*
* method to write a player's name and score to output file
*/
void writeScore(ofstream &outFile,performer current)
{
outFile<<current.name;
outFile<<" ";
outFile<<current.finalscore;
outFile<<endl;
}
/*
* method to display a goodbye message
*/
void printEnd(void)
{
cout << " Thank you for using this program. Bye!" << endl;
}
/*OUTPUT*/
The 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.
This program calculates the score of each performer and determines the winner.
There are 5 participants
Winner is Alice with final score of 8.5
Thank you for using this program. Bye!
//performers.txt which I have used
Alice 8.5 7.8 3.4 9.4 9.2
Bob 8.6 7.9 3.2 7.4 7
Chris 5.5 7.3 4.4 10 9.2
Dave 9 4.4 3.8 8.4 8.2
Emily 10 7 7 9.4 9.5
//results.txt
Alice 8.5
Bob 7.43333
Chris 7.33333
Dave 7
Emily 6.3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.