In: Computer Science
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 |
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