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

NO STRUCTURES OR POINTERS ONLY FOLLOW INSTRUCTIONS AS ABOVE INPUT The data file

ID: 3867284 • Letter: N

Question

NO STRUCTURES OR POINTERS

ONLY FOLLOW INSTRUCTIONS AS ABOVE

INPUT The data file you are to use is gymnasts.txt Each line contains the following information for one gymnast: String name of gymnast (Store in the array for names) Integer- age of gymnast (Store in the array for ages) Double - score on vault (Add into sum for All Around score but do not store in an array) Double - score on balance beam (Add into sum for All Around score but do not store in an array) Double - score on bars (Add into sum for All Around score but do not store in an array) Double - score of floor (Add into sum for All Around score but do not store in an array) Assume 100 is the maximum number of gymnasts. Calculation (done in input function) In a third one-dimensional array (type double) compute the gymnast's All Around score by adding the scores for vault, balance beam, bars, and floor. This is best done in the input function. In order to move the file pointer to the next gymnast you will really, really enjoy the following string temp getline(inFile,temp)://clear In or rl (Mac) before next string OUTPUT Output 1: Output the name, age and All Around score for each gymnast. These should be in the following format and sorted by All Around score from high to low using selection sort. Output is to be to file 2017 USAIGC/IAGC WORLD CHAMPIONSHIPS NAME AGE ALL AROUND

Explanation / Answer

C++ code:

#include <bits/stdc++.h>

using namespace std;

vector<string> names;

vector<int> ages;

vector<double> allscores;

void read(string inputfile)

{

string line;

ifstream myfile (inputfile.c_str());

if (myfile.is_open())

{

while ( getline (myfile,line) )

{

string buf; // Have a buffer string

stringstream ss(line); // Insert the string into a stream

vector<string> tokens; // Create vector to hold our words

while (ss >> buf)

tokens.push_back(buf);

names.push_back(tokens[0]);

ages.push_back(atoi(tokens[1].c_str()));

double tmp = atof(tokens[2].c_str()) + atof(tokens[3].c_str()) +atof(tokens[4].c_str()) +atof(tokens[5].c_str());

allscores.push_back( tmp );

}

myfile.close();

}

else

{

cout << "Unable to open file" << endl;

exit(1);

}

}

void printt()

{

ofstream outfile;

outfile.open("output.txt");

outfile << "All Entries ";

outfile << "Name" << " " << "Age" << " " <<"All Around Score!" << endl;

for (int i = allscores.size() - 1; i >= 0; --i)

{

outfile << names[i] << " " << ages[i] << " " << allscores[i] << endl;

}

  

outfile << " Entries with age = 10 ";

outfile << "Name" << " " <<"All Around Score!" << endl;

for (int i = allscores.size() - 1; i >= 0; --i)

{

if(ages[i] == 10)

{

outfile << names[i] << " " << allscores[i] << endl;

}

}

}

void selectionSort()

{

int n = allscores.size();

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

{

int min = j;

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

if (allscores.at(min) > allscores.at(i)) {

min = i;

}

}

if (min != j){

swap(allscores.at(j), allscores.at(min));

swap(names.at(j), names.at(min) );

swap(ages.at(j), ages.at(min) );

}

}

}

int main()

{

string inputfile = "gymnasts.txt";

read(inputfile);

selectionSort();

printt();

return 0;

}

Sample gymnasts.txt //Note that names should be a string without spaces!

n1 10 2 3 4 1
n2 11 2 3 4 3
n3 10 2 3 4 4
n4 9 2 3 4 2   

Sample output.txt

All Entries

Name Age All Around Score!

n3 10 13

n2 11 12

n4 9 11

n1 10 10

Entries with age = 10

Name All Around Score!

n3 13

n1 10