C++ Scoring a Diving Competition The names of divers and their scores for a dive
ID: 3841780 • Letter: C
Question
C++ Scoring a Diving Competition
The names of divers and their scores for a dive are stored in a file called DIV1.txt . Each diver's name is stored on a line followed by that diver’s data for one dive. The data contains the difficulty factor of the dive and a score from each of 5 judges. Compute each diver’s total score by throwing out his/her low and high scores (do not change the order of the scores), averaging the remaining three scores, and then multiplying this average by the difficulty factor. Output each diver's name, his/her scores, and the total score for the dive arranged from the diver with the highest score to the lowest. Output is to be directed to a file.
The contents of DIV1.txt is to be:
KNIFE JACK 1.3 6.0 5.1 6.3 5.9 6.5
WILLIAMSON FLIP A 1.4 3.5 4.1 4.7 7.2 3.8
SOMMER TODD 1.2 8.0 9.1 8.1 9.3 9.0
SWAN MIKE 1.1 4.3 2.1 9.9 6.2 7.0
Input: Assume a maximum of 50 divers. Name of diver, difficulty factor, array for scores, and the total score for the dive should be members of a structure. The structure should contain a nested structure for difficulty factor, array of scores, and total score for dive. Each diver’s structure needs to be an element of an array of structures.
Processes: Computation of total score is to be handled in a separate function. A separate function should perform the sort using the bubble sort algorithm. Output: Use a separate function for output. Output is to be directed to a file.
Theme Issues: Array of structures, Sorting.
Requirements:
- Function main() may contain only declarations, function calls to your declared functions, and comments.
- Function to input all diver information and store this in an array of structures. The number of judges needs to be a #define and the code should not depend on it being the value of 5.
- Function to calculate the total score for each diver. This may be called either from main() or from the input function.
- Function to sort by total score from highest to lowest implementing the bubble sort.
- Function to output (and only one output function where all the output occurs).
Thank you.
Explanation / Answer
The program is as follows:
// Assuming the output file to be ouput.txt
#include<iostream.h>
#include<string>
#include<sstream>
#define NO_OF_JUDGES 5
using namespace std;
// Divers Performance
struct perform {
float diff_factor;
float score[NO_OF_JUDGES];
float total_score;
}
struct diver {
string name;
struct perform perf;
};
diver list[50];
int count; //count holds the total number of divers
void input()
{
int count1;
ifstream infile;
infile.open("DIV1.txt");
if (!infile){
cout << "Error in file opening" << endl;
return;
}
count = 0;
while (std::getline(infile, line)){
std::istringstream ss(line); //Talking input from string stream
if (!(ss >> list[count].firstname >> list[count].firstname){
if (!(ss >> list[count].perf.diff_factor){
count1 = 0;
while (!(ss >> list[count].perf.score[count1]) && count1 < NO_OF_JUDGES){
count1++;
}
}
}
count++;
}
infile.close();
}
void cal_total_score() {
float max, min;
float sum;
float temp;
for (int i = 0; i<count; i++){
max = list[count].perf.score[0];
min = list[count].perf.score[0];
for (int j = 0; j<NO_OF_JUDGES; j++){
if (max < list[count].perf.score[j])
max = list[count].perf.score[j];
if (min > list[count].perf.score[j])
min = list[count].perf.score[j];
}
sum = 0;
for (int j = 0; j<NO_OF_JUDGES; j++){
if (list[count].perf.score[j] != max && list[count].perf.score[j] != min)
sum = sum + list[count].perf.score[j];
}
list[count].perf.total_score = (sum/NO_OF_JUDGES) * list[count].perf.diff_factor;
for (int j = 0; j<NO_OF_JUDGES-1; j++){ //Sorting the scores
for (int k = 0; k<NO_OF_JUDGES-1-j; k++){
if (list[count].perf.score[k] > list[count].perf.score[k+1]){
temp = list[count].perf.score[k];
list[count].perf.score[k] = list[count].perf.score[k+1];
list[count].perf.score[k+1] = temp;
}
}
}
}
}
void sort() {
diver temp;
for (int i = 0; i < count-1; i++){
for (j=0; j<cout - i - 1; j++){
if (list[j].perf.total_score > list[j+1].perf.total_score){
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
}
void output(){
ofstream outfile;
outfile.open("output.txt");
for(int i = 0; i<count; i++){
outfile << list[count].firstname << " " << list[count].lastname << " ";
for (int j = 0; j < NO_OF_JUDGES; j++){
outfile << list[count].perf.score[j] << " ";
}
outfile << list[count].perf.total_score << endl;
}
outfile.close();
}
void main(){
input();
cal_total_score();
sort();
output();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.