Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Scoring a Diving Competition The names of divers and their scores for a dive

ID: 3841298 • 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.

Explanation / Answer

Program:

#include<iostream>

#include<fstream>

using namespace std;

struct diver

{

            string fname,lname;

            float dod;

            float score[5];

            float total_score;

            float highest=0,lowest=100,total = 0;

}d[50];

int n= 5; /*Here you need to give n = 50, if your file contains maximum of 50 records. For sample I have taken only 5 records.*/

void output_result(diver d[]){

            ofstream outfile;

            outfile.open("divop.txt");

            int i,j;

            for(i=0;i<n;i++){

                        outfile<<d[i].fname<<" "<<d[i].lname<<" "<<d[i].dod<<" ";

                        for(j=0;j<5;j++)

                                    outfile<<d[i].score[j]<<" ";

                        outfile<<d[i].total_score<<endl;

            }

            outfile.close();

}

float compute_total_score(float score[],float highest,float lowest,float dod,float total){

            total = (total - highest - lowest)/3;

            float total_score = total * dod;

            return total_score;

}

void bubble_sort(diver d[]){

            diver temp;

            int i,j;

            for(i=0;i<n-1;i++){

                        for(j=0;j<n-i-1;j++){

                                    if(d[j].total_score>d[j+1].total_score){

                                                temp = d[j];

                                                d[j] = d[j+1];

                                                d[j+1] = temp;

                                    }

                        }

            }

            output_result(d);

}

int main()

{

            ifstream infile;

            infile.open("DIV1.txt");

            int i,j;

            for(i=0;i<n;i++){

                        infile>>d[i].fname;

                        infile>>d[i].lname;

                        infile>>d[i].dod;

                        for(j=0;j<5;j++){

                                    infile>>d[i].score[j];

                                    if(d[i].score[j] > d[i].highest)

                                                            d[i].highest = d[i].score[j];

                                                if(d[i].score[j]<d[i].lowest)

                                                            d[i].lowest = d[i].score[j];

                                                d[i].total += d[i].score[j];

                        }

            }

            for(i=0;i<n;i++)

            {

                        d[i].total_score = (d[i].score,d[i].highest,d[i].lowest,d[i].dod,d[i].total);

            }

            bubble_sort(d);

            infile.close();

            return 0;

}

Sample Input and Output:

Input File: /*DIV1.txt */

KNIFE JACK 1.3 6.0 5.1 6.3 5.9 6.5

WILLIAMSON FLIP 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

RAM DEV 1.3 4.8 3.7 8.3 6.4 8.0

Output File: /*divop.txt */

WILLIAMSON FLIP 1.4 3.5 4.1 4.7 7.2 3.8 23.3

SWAN MIKE 1.1 4.3 2.1 9.9 6.2 7 29.5

KNIFE JACK 1.3 6 5.1 6.3 5.9 6.5 29.8

RAM DEV 1.3 4.8 3.7 8.3 6.4 8 31.2

SOMMER TODD 1.2 8 9.1 8.1 9.3 9 43.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote