void sortfunction(double scores[], int scoresize) {
int maxelement;
int index;
for (maxelement = scoresize – 1; maxelement > 0; maxelement–)
{
for (index = 0; index < maxelement; index++) { if (scores[index] > scores[index + 1])
{
swap(scores[index], scores[index + 1]);
}
}
}
// for (int i = 0; i < scoresize; i++) {
// cout << scores[i] << endl;
// }
}
int main() {
double median = 0;
double average = 0;
string filename;
const int scoresize = 11;
double scores[scoresize]; // array of scors
double teamnum;
int scoreposition = 0;
streamsize ss = cout.precision();
ifstream somefile(filename);
string teams; // string for the teams
double highAverage = 0;// used this as a variable to have something to compare average to and to store the highest average
string winners; // this string was made to store the winning team
double highestmedian = 0; // highest median is useful , because i can compare the median variable with it and store the highest median
bool medianwinner = false; // setting a bool variable allowed me to choose when the hihgest median variable comes into play
int teamcount = 0; // simple counter for the teams attending
cout << “Enter filename and file location” << endl; // lines 46-50 were very helpful in testing for file errors and making the correct code as well as telling me if the file was not opening correctly cin >> filename;
somefile.open(filename);
if (somefile.fail()) {
cout << “Error opening file” << endl;
}
if (somefile) {
while (!somefile.eof()) {
cout << fixed << setprecision(2); // lines 54 – 62 are responsible for reading scores and adding on to the teamcount(that will count teams) getline(somefile, teams); ++teamcount; scoreposition = 0; cout << teams << endl; for (int i = 0; i < 11; i++) { // loop to intake data somefile >> teamnum;
cout << teamnum << ” “;
scores[scoreposition++] = teamnum;
}
double highest = scores[0]; // lines 65-76 were made in order to get the highest and lowest averages so it will be easier to get those values out of the average
double smallest = scores[0];
double sum = scores[0];
for (int i = 1; i < 11; i++) { sum += scores[i]; if (scores[i] > highest) {
highest = scores[i];
}
if (scores[i] < smallest) { smallest = scores[i]; } } sortfunction(scores, scoresize);//the function made it really easy to assort the array of scores from lowest to highest which easily allowed me to pick a median as all i had to do //was choose the element in the middle median = scores[5]; sum -= highest + smallest; // total of scores which helps me find average double average = sum / 9; //cout << “Average: ” << average << endl; if (average > highAverage) { // lines 83 – 96 use the bool median winner to see if the highest median will come into play,using the variable highaverage to make a comparison and
// highestmedain to store the median
highAverage = average;
winners = teams;
highestmedian = median;
medianwinner = false;
}
else if (average == highAverage) {
if (median >= highestmedian) {
winners = teams;
highAverage = average;
highestmedian = median;
}
medianwinner = true;
}
// median = scores[5];
somefile.ignore(); // lines 99-107 simply show a stat report for the teams including total scores, averages and medians for the teams
cout << endl;
cout << endl;
cout << “Stat report for ” << teams << endl;
cout << “—————————–” << endl;
cout << “Average is ” << average << endl;
cout << “median is: ” << median << endl;
cout << “total is: ” << sum << endl; // helps me make sure average is correct as i divide sum by 9 and see if average is correct
cout << endl;
}
}
if (medianwinner) { // if the bool is true the program will display the team won with a higher median
cout << “The winning team won by breaking a tie with a higher median” << endl;
}
cout << “The amount of teams that attended are: ” << teamcount << endl;
cout << “Winning team is : ” << winners << endl; // showcasing the winner
cout << “Average of ” << winners << ” is: ” << highAverage << endl; //winners average
somefile.close();
system(“PAUSE”);
return 0;
Sample Solution
Is this question part of your Assignment?
We can help
Our aim is to help you get A+ grades on your Coursework.
We handle assignments in a multiplicity of subject areas including Admission Essays, General Essays, Case Studies, Coursework, Dissertations, Editing, Research Papers, and Research proposals
Header Button Label: Get Started NowGet Started Header Button Label: View writing samplesView writing samples