In: Computer Science
This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please have a screenshot of output)
In competitive diving, each diver makes dives of varying degrees of difficulty. Nine judges score each dive from 0 through 10 in steps of 0.5. The difficulty is a floating-point value between 1.0 and 3.0 that represents how complex the dive is to perform. The total score is obtained by discarding the lowest and highest of the judges’ scores, adding the remaining scores, and then multiplying that total by the degree of difficulty. Write a program to score each of the dives, using the following input and output specifications, and determine the winner of the competition.
Input:
Create the file m7dive.txt for input and the output must go to an output file named mp7output.txt.
The first line contains an integer for the number of divers in
the competition and subsequent lines contain:
Diver’s name (10 characters max, no blanks included), difficulty
(double), and judges’ ratings (nine doubles). There is one line of
data for each diver.
Example file:
2 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
Anne | 2.0 | 9.0 | 8.5 | 8.5 | 8.5 | 9.0 | 9.0 | 8.0 | 9.5 | 9.5 |
Sarah | 1.6 | 7.5 | 7.0 | 8.0 | 8.0 | 8.0 | 8.5 | 8.5 | 8.5 | 9.0 |
Output:
The name and difficulty, followed by the scores sorted into increasing order, in tabular form with appropriate headings along with the earned total points for that diver.
Example for sample output of the input file data above
NAME | DIFF | SORTED SCORES | POINTS | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Anne | 2.0 | 8.0 | 8.5 | 8.5 | 8.5 | 9.0 | 9.0 | 9.0 | 9.5 | 9.5 | 124.0 |
Sarah | 1.6 | 7.0 | 7.5 | 8.0 | 8.0 | 8.0 | 8.5 | 8.5 | 8.5 | 9.0 | 91.2 |
The winner is Anne with the points of 124.0
At the end of the table, print out the name of the winner of the
competition (the person with the highest points) and his/her final
score.
Hint: You must use functions to modularize your program. Here is the full data of your input file mp7dive.txt, please send the output to an output file mp7output.txt.
The first line is 7 which is the number of divers in the file. The rest of the lines are as described above.
7 Anne 2.0 8.5 8.5 9.0 9.0 9.0 9.5 8.5 8.0 9.5 Sarah 1.6 7.5 8.5 8.0 8.0 7.0 9.0 8.5 8.5 8.0 Deborah 2.3 9.0 9.0 9.5 10.0 10.0 9.5 9.5 9.5 9.5 Kathryn 2.4 9.0 9.0 9.0 9.5 9.5 9.5 9.0 8.0 8.5 Jacquelin 2.7 9.0 9.0 9.5 9.5 9.0 8.5 8.5 8.5 9.5 Elizabeth 2.9 8.0 8.0 7.5 8.5 8.5 8.0 8.0 7.5 8.5 Tina 2.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// m7dive.txt
7
Anne 2.0 8.5 8.5 9.0 9.0 9.0 9.5 8.5 8.0 9.5
Sarah 1.6 7.5 8.5 8.0 8.0 7.0 9.0 8.5 8.5 8.0
Deborah 2.3 9.0 9.0 9.5 10.0 10.0 9.5 9.5 9.5 9.5
Kathryn 2.4 9.0 9.0 9.0 9.5 9.5 9.5 9.0 8.0 8.5
Jacquelin 2.7 9.0 9.0 9.5 9.5 9.0 8.5 8.5 8.5 9.5
Elizabeth 2.9 8.0 8.0 7.5 8.5 8.5 8.0 8.0 7.5 8.5
Tina 2.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5
____________________________
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
struct Diver
{
string name;
double diffScore;
double scores[9];
};
// Function Declarations
void readData(ifstream& dataIn,struct Diver divers[],int
size);
void calcAvgs(struct Diver divers[],int size,double tots[]);
void sort(struct Diver divers[],int size);
void display(struct Diver divers[],double tots[],int size);
int findWinner(double tots[],int size);
int main() {
int size;
//Declaring variables
ifstream dataIn;
dataIn.open("m7dive.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
dataIn>>size;
// Creating array dynamically
struct Diver* divers = new Diver[size];
double* tots=new double[size];
readData(dataIn,divers,size);
calcAvgs(divers,size,tots);
sort(divers,size);
display(divers,tots,size);
int indx=findWinner(tots,size);
cout<<"The Winner is
"<<divers[indx].name<<endl;
}
return 0;
}
void readData(ifstream& dataIn,struct Diver divers[],int
size)
{
//Reading the data from the file
for(int i=0;i<size;i++)
{
dataIn>>divers[i].name>>divers[i].diffScore;
for(int j=0;j<9;j++)
{
dataIn>>divers[i].scores[j];
}
}
dataIn.close();
}
void calcAvgs(struct Diver divers[],int size,double tots[])
{
double min,max,avg=0.0,sum=0;
for(int i=0;i<size;i++)
{
sum=0;
min=divers[i].scores[0];
max=divers[i].scores[0];
for(int j=0;j<9;j++)
{
if(min>divers[i].scores[j])
{
min=divers[i].scores[j];
}
if(max<divers[i].scores[j])
{
max=divers[i].scores[j];
}
sum+=divers[i].scores[j];
}
sum-=(min+max);
tots[i]=sum*divers[i].diffScore;
}
}
void sort(struct Diver divers[],int size)
{
double temp;
for(int i=0;i<size;i++)
{
//This Logic
will Sort the Array of elements in Ascending order
for (int k = 0; k < 9; k++)
{
for (int m = k + 1; m < 9; m++)
{
if (divers[i].scores[k] > divers[i].scores[m])
{
temp = divers[i].scores[k];
divers[i].scores[k] = divers[i].scores[m];
divers[i].scores[m] = temp;
}
}
}
}
}
void display(struct Diver divers[],double tots[],int size)
{
//setting the precision to two decimal places
std::cout << std::setprecision(1) <<
std::fixed;
for(int i=0;i<size;i++)
{
cout<<setw(10)<<left<<divers[i].name<<setw(5)<<right<<divers[i].diffScore;
for(int j=0;j<9;j++)
{
cout<<setw(5)<<right<<divers[i].scores[j];
}
cout<<setw(8)<<right<<tots[i]<<endl;
}
}
int findWinner(double tots[],int size)
{
double max=tots[0];
int pos=0;
for(int i=0;i<size;i++)
{
if(max<tots[i])
{
max=tots[i];
pos=i;
}
}
return pos;
}
_______________________________
Output:
___________________________
mp7output.txt (Output file)
_______________Could you plz rate me well.Thank You