Question

Machine Problem 7: Structures ---- TO BE COMPLETED IN C++ This assignment is to give you...

Machine Problem 7: Structures ---- TO BE COMPLETED IN C++

This assignment is to give you practice using one-dimensional arrays 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 m6dive.txt

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:

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.

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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Cpp Code for the given question:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int findWinner(double finalScores[], int n);
void sort(double * arr);
int main(){
ifstream ifs;
//open the file
ifs.open("D://m6dive.txt");
//I saved the file in D drive as m6dive.txt
string names[100];//for diver's names
double diff[100];//for difficulty
static double finalScores[100];//for total socres
double min[100];//for minimum score of a diver
double max[100];//for maximum score of a diver
if (ifs.fail()){
cout << "Input file cannot be opened" << endl;
}
//Read first line of the file. The first line contains an integer for the
//number of divers in the competition
int no_divers;
ifs >> no_divers;
//Creating score arrays for each diver
double **scores=new double *[no_divers];
//Read each diver information and calculate scores
int i = 0;
while (i < no_divers){
//Allocate space for a single dimensional array of size 9
scores[i] = new double[9];
//read diver name
ifs >> names[i];
//read difficulty
ifs >> diff[i];
//read first judges score
ifs >> scores[i][0];
//initialize min and max
min[i] = scores[i][0];
max[i] = scores[i][0];
//Read remaining judges scores2-scores9
for (int j = 1; j < 9; j++){
ifs >> scores[i][j];
//find minimum score and maximum score
if (min[i] > scores[i][j])
min[i] = scores[i][j];
if (max[i] < scores[i][j])
max[i] = scores[i][j];
}
//Find the total score
for (int j = 0; j < 9; j++){
finalScores[i] += scores[i][j];//
}
finalScores[i] -= min[i];//discard minimum score
finalScores[i] -= max[i];//discard maximum score
finalScores[i] = finalScores[i] * diff[i];//multiply the sum with difficulty
i++;//move to next diver info
}
//Display the output
i = 0;
cout << setw(10)<<"NAME"<<" DIFF "<< resetiosflags(ios::left ) << setw(5);
cout << "SORTED SCORES\t\t\t\t\tPOINTS" << endl;
cout << "______________________________________________________________________" << endl;
while (i < no_divers){
//pass a single dimensional score array to sort
sort(&scores[i][0]);
//Print diver info with sorted scores
cout << setw(10)<< resetiosflags(ios::left)<<names[i]<< " "<< setprecision(1) << fixed<<diff[i];
for (int j = 0; j < 9; j++)
cout << setw(5) << setprecision(1) << fixed << scores[i][j] ;
cout<< "\t"<<finalScores[i] << endl;
i++;
}
//Find winner
int winner = findWinner(finalScores, no_divers);
cout << "**Winner**\n" << names[winner] << " with points " << finalScores[winner] << endl;
return 0;
}
//sorting a single dimensional array
void sort(double * arr){
double temp = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
//Returns the index of the winner
int findWinner(double finalScores[],int n){
double temp = finalScores[0];
int index = 0;
for (int i = 0; i < n; i++){
if (temp < finalScores[i]){
temp = finalScores[i];
index = i;
}
}
return index;
}

m6dive.txt:

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

Eliza 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

Text file:

Add a comment
Know the answer?
Add Answer to:
Machine Problem 7: Structures ---- TO BE COMPLETED IN C++ This assignment is to give you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ETM-3030 DIVING SCORES EXCEL 10 POINTS In this assignment you will use excel to process the score...

    ETM-3030 DIVING SCORES EXCEL 10 POINTS In this assignment you will use excel to process the scores of a diving competition. In the next assignment you will solve the same problem using C#. The diving works as follows: A set of judges score each dive from o0 to 10 in stes of 0.5. The total score for a dive is obtained by discarding the lowest and highest of the judges' scores, adding the remaining scores, and multiplying the result by...

  • A particular talent competition has five judges, each of whom awards a score between 0 and...

    A particular talent competition has five judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3, are allowed. A performer’s final score is determined by dropping the highest and the lowest score received then averaging the three remaining scores. Write a program that does the following: 1. Reads names and scores from an input file into a dynamically allocated array of structures. The first number in the input file represents the...

  • c++ implement a student class Determine the final scores, letter grades, and rankings of all students...

    c++ implement a student class Determine the final scores, letter grades, and rankings of all students in a course. All records of the course will be stored in an input file, and a record of each student will include the first name, id, five quiz scores, two exam scores, and one final exam score. For this project, you will develop a program named cpp to determine the final scores, letter grades, and rankings of all students in a course. All...

  • Please Write in C++ (10-30) @ 11:55pm 1.9 HW7 This homework assignment gives you the opportunity...

    Please Write in C++ (10-30) @ 11:55pm 1.9 HW7 This homework assignment gives you the opportunity to practice functions, functions that call other functions, reference variables, logical statements (what is meant by logical statement is a statement such as if, if/else if, switch), and input validation, HW7 (Graded out of 100) A talent competition has 5 judges, each of whom awards a score between 0 and 10 for each performer. Fractional scores, such as 8.3, are allowed. A performer's final...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT