You are working for a company that is responsible for determining the winner of a prestigious international event, Men’s Synchronized Swimming. Scoring is done by eleven (11) international judges. They each submit a score in the range from 0 to 100. The highest and lowest scores are not counted. The remaining nine (9) scores are averaged and the median value is also determined. The participating team with the highest average score wins. In case of a tie, the highest median score, among the teams that are tied for first place, determines the winner.
Scoring data is be provided in the form of a text file. The scoring data for each team consists of two lines; the first line contains the team name; the second line contains the eleven (11) scores. You will not know ahead of time how many teams will participant. Your program will:
A sample data file has been provided to allow you to test your program before submitting your work. Records in the sample data file look like this:
Sea Turtles
11 34 76 58 32 98 43.5 87 43 23 22
Tiger Sharks
85 55 10 99 35 5 65 75 8 95 22
The Angry Piranhas
30 10 80 0 100 40 60 50 20 90 70
The average score for the Sea Turtles was 46.5. The Angry Piranhas and the Tiger Sharks both had an average score of 50.0. The tiger sharks had a median value of 55.0, making them the winners of the competition.
You will define and use at least one function in your solution. As a suggestion, you should consider calling a function that sorts the array of judge’s score values into ascending order (lowest to highest). The advantage of doing this is that after being sorted, the lowest value is the first entry in your array and the highest value is the last entry in your array.
The average or arithmetic mean is a measure of central tendency. It is a single value that is intended to represent the collection of values. For this problem, you will only use nine of the values to determine the average. You will not include the highest score and the lowest score.
The median is another measure of central tendency. The median is often referred to as the middle value. In a set of values, half the values are less than the median and the other half are greater than the median. In a sorted array of five elements, the median is the third element. There are two values that are less then it and there are two values that are greater than it. In a sorted array of six values, the median is the simple average of the third and fourth values. In some cases, the median is a better representative value than the average.
You are encouraged to read the article entitled “Mixing getline with cin”.
To receive full credit for this programming assignment, you must:
Grading Guideline:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
// Function to swap two variables.
void swap(double *xp, double *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(double arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place, so iterate till n-i-1 only.
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
int main(){
string score_file;
// Take input of score data file path
cout<<"Enter the path of the scoring data file : ";
cin>>score_file;
ifstream infile(score_file);
// Declare appropriate variables.
int numberOfTeams = 0;
string winning_team;
double winning_average = 0;
double winning_median = 0;
bool isTie = false;
// Read till end of file.
while(!infile.eof()){
// Taking input of team name and scores.
string name;
getline(infile, name);
double scores[11];
double totalScore = 0;
for(int i=0;i<11;i++){
infile>>scores[i];
}
infile.ignore();
cout<<name<<endl;
for(int i=0;i<11;i++){
printf("%.2f ", scores[i]);
}
printf("\n");
// Sorting the scores, and ignore first and last to calculate mean and median.
bubbleSort(scores, 11);
for(int i=1;i<10;i++){
totalScore+=scores[i];
}
double median = scores[5];
double average = totalScore/9;
printf("Average score : %.2f\n", average);
printf("Median score : %.2f\n", median);
numberOfTeams++;
// If current average is same as winning average, the winning team must
if(average == winning_average){
isTie = true;
}
// If average of current team is higher than winning average (or)
// Average of current team is same as winning average but current team has higher median,
// update the winner.
if((average > winning_average) || (average == winning_average && median > winning_median)){
if(average > winning_average){
isTie = false;
}
winning_team = name;
winning_average = average;
winning_median = median;
}
cout<<"\n";
}
// Printing out relevant information.
cout<<"Total number of participating teams : "<<numberOfTeams<<endl;
cout<<"The winner is "<<winning_team<<" with average score "<<winning_average<<endl;
if(isTie){
cout<<"They won by breaking a tie with a higher median score."<<endl;
}
}
Output is run on same data as provided in question.
Input file -
Please ensure that the input file is in the same format as I have run for the code to run correctly.
The code has been commented so you can understand the code better.
I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)
Happy Coding !!
Get Answers For Free
Most questions answered within 1 hours.