Question

In: Computer Science

in c++ language This assignment is to give you practice using structs and sorting. In competitive...

in c++ language

This assignment is to give you practice using structs and sorting.

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 m7divers.txt using the data given at the end.

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: (This is not the data to use)

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

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 dive.

Example for sample 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


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: Use functions to modularize your program.

Use this data for your input file. download the data from the attachment.

NAME DIFF SORTED SCORES        
Anne 2.0 8.0 8.5 8.5 8.5 9.0 9.0 9.0 9.5 9.5
Sarah 1.6 7.0 7.5 8.0 8.0 8.0 8.5 8.5 8.5 9.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
Martha 2.7 9.0 9.0 9.5 9.5 9.0 8.5 8.5 8.5 8.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

Solutions

Expert Solution

DATA FILE: m7divers.txt

Create the data file as text file named m7divers.txt with following data and store it in the same folder where your C++ program is stored. The individual data items should be separated by tabs. Make sure that no blank line is present after the line with last data line.

7

Anne             2.0       8.0       8.5       8.5       8.5       9.0       9.0       9.0       9.5       9.5

Sarah             1.6       7.0       7.5       8.0       8.0       8.0       8.5       8.5       8.5       9.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

Martha          2.7       9.0       9.0       9.5       9.5       9.0       8.5       8.5       8.5       8.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

//PROGRAM

//////////////////////////////////////////////////////

// CALCULATING SCORES OF SWIMMERS                   //

//////////////////////////////////////////////////////

//include header files

#include<iostream>

#include<iomanip>

#include<fstream>

//maxmimum size of divers

//for declaring the size of structures

#define MAX 10

//include std name space

using namespace std;

//swap two variables

//used by the sort function

//parameters: two floating-point numbers

//output: contents get swapped

//side effects: None

void swap(float a, float b)

{

    int temp = a;

    a = b;

    b = temp;

}

//function to implement bubble sort

//parameters: floating-point array of numbers and its size

//output: sorted array

//side effects: None

void sort(float arr[], int n)

{

    int i, j;

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

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

            if (arr[j] > arr[j+1])

                swap(arr[j], arr[j+1]);

}

//main function

int main(void)

{

    fstream indata; //declaring input file stream

    struct //stucture to hold details of divers

    {

        string name; // holds name

        float diff; // holds diificulty level

        float score[9]; //holds 9 scores

        float total; //holds total score

    }diver[MAX]; //array of divers

    int i,j, divers, winner, maxpoint;

    //opening the input file containing data

    indata.open("m7divers.txt",ios::in);

    //check if data file is created or not

    if(!indata)

    {

        cout<<"Data File missing."

        <<"Create a text file named m7divers.txt"

        <<" with the given data";

        return 0;

    }

    //set precision and fixed point flag

    cout.precision(1);

    cout<<fixed;

    //get number of divers from file

    indata>>divers;

    //initialized index of diver

    i=0;

    //read data of divers from file

    while(!indata.eof())

    {   indata>>diver[i].name>>diver[i].diff;

        for(int j=0;j<9; j++)

            indata>>diver[i].score[j];

        i++;

   }

    //sort diver scores

    for(int i=0;i<divers; i++)

        sort(diver[i].score,9);

    //initialize winner and

    //maximum point of the winner

    winner=0;

    maxpoint=0;

    //calculate totals of divers

    //also finds winner

    for(int i=0;i<divers; i++)

    {

        diver[i].total=0;

        for(int j=1;j<8; j++)

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

        diver[i].total *= diver[i].diff;

        if(maxpoint<diver[i].total)

        {   maxpoint=diver[i].total;

            winner=i;

        }

    }

    //display the table headings

    cout<<endl<<"-------------------------------------------------------------------";

    cout<<endl<<"      NAME DIFF S1   S2   S3   S4   S5   S6   S7   S8   s9   TOT";

    cout<<endl<<"-------------------------------------------------------------------";

    //display details of each diver

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

    {   cout<<endl<<setw(10)<<diver[i].name<<" "<<setw(4)<<diver[i].diff<<" ";

        for(int j=0;j<9; j++)

            cout<<" "<<setw(4)<<diver[i].score[j];

        cout<<" "<<setw(6)<<diver[i].total;

    }

    //display winner as footer of the table

    cout<<endl<<"-------------------------------------------------------------------";

    cout<<endl<<"Winner is "<<diver[winner].name<<" with a total score of "<< diver[winner].total;

    cout<<endl<<"-------------------------------------------------------------------";

    return 0;

}

SCREENSHOT OF PROGRAM

OUTPUT OF SUCCESSFUL RUN IN CODE::BLOCK

-------------------------------------------------------------------

      NAME DIFF S1   S2   S3   S4   S5   S6   S7   S8   s9   TOT

-------------------------------------------------------------------

      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

   Deborah 2.3   9.0 9.0 9.5 10.0 10.0 9.5 9.5 9.5 9.5 154.1

   Kathryn 2.4   9.0 9.0 9.0 9.5 9.5 9.5 9.0 8.0 8.5 152.4

    Martha 2.7   9.0 9.0 9.5 9.5 9.0 8.5 8.5 8.5 8.5 168.8

Elizabeth 2.9   8.0 8.0 7.5 8.5 8.5 8.0 8.0 7.5 8.5 162.4

      Tina 2.5   8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 8.5 148.8

-------------------------------------------------------------------

Winner is Martha with a total score of 168.8

-------------------------------------------------------------------

SCREENSHOT OF SUCCESSFUL RUN IN CODE::BLOCK


Related Solutions

This assignment is to give you practice using struts, arrays, and sorting. (Objective C++ and please...
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,...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this...
This assignment will give you practice in Handling Exceptions and using File I/O. Language for this program is JAVA Part One A hotel salesperson enters sales in a text file. Each line contains the following, separated by semicolons: The name of the client, the service sold (such as Dinner, Conference, Lodging, and so on), the amount of the sale, and the date of that event. Prompt the user for data to write the file. Part Two Write a program that...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Assignment #1: Sorting with Binary Search Tree (IN C LANGUAGE) Through this programming assignment, the students...
Assignment #1: Sorting with Binary Search Tree (IN C LANGUAGE) Through this programming assignment, the students will learn to do the following: 1. Know how to process command line arguments. 2. Perform basic file I/O. 3. Use structs, pointers, and strings. 4. Use dynamic memory. This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree...
In this assignment, you are expected to rewrite the program from assignment7 using structs. That is,...
In this assignment, you are expected to rewrite the program from assignment7 using structs. That is, instead of using multiple arrays (one for each field, you need to create a single array of the datatype you should create as a struct). So, write a C++ program (use struct and dynamic memory allocation) that reads N customer records from a text file (customers.txt) such as each record has 4 fields (pieces of information) as shown below: Account Number (integer) Customer full...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and...
Assignment Overview IN C++ This assignment will give you practice with numerical calculations, simple input/output, and if-else statements. Candy Calculator [50 points] The Harris-Benedict equation estimates the number of calories your body needs to maintain your weight if you do no exercise. This is called your basal metabolic rate or BMR. The calories needed for a woman to maintain her weight is: BMR = 655 + (4.3 * weight in pounds) + (4.7 * height in inches) - (4.7 *...
C++ program to read line comments. This assignment will give you a little bit of practice...
C++ program to read line comments. This assignment will give you a little bit of practice with string parsing. Your task is to write a program that reads in a .cpp file line-by-line, and prints out only the text that's after the // characters that indicate a single-line comment. You do not have to worry about /* multiline comments */ though there will be a small amount of extra credit for programs that correctly extract these comments as well.
This assignment is to give you practice using enums, string variables, and string functions. In order...
This assignment is to give you practice using enums, string variables, and string functions. In order to get full credit for the program you must use these three topics. You are to write a program that will read a series of names of people from a data file that has been created with extra blank spaces and reformat the names into a standardized format. The datafile is mp6names.txt. It is arranged with the information for one person on each line....
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Language: c++ works in visual basic Write a program that uses an array of nested structs...
Language: c++ works in visual basic Write a program that uses an array of nested structs to store the addresses for your store’s customers.  Each customer has a name and two addresses: home address and business address.  Each address has a street, city, state, and zip code. Requirements: 1. Data structure a. Define an Address struct with street, city, state and zip fields b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT