In: Computer Science
C++ Programming
Consider an input file that lists test participants in alphabetical order and their scores. Each line has the following format:
LastName FirstName score
Load the input file into an array of structs, knowing there is a maximum of 50 participants. Output to output.txt the participants with the top 5 scores, in decreasing order of scores. Each output line should have the following format:
FirstName,LastName,score
Notes:
Example input file:
Baker Alex 90 Eaves Pat 100 Kay Ivy 100 Lamar Jay 80, O'Conor Ashton 95 Saber Alice 98 Sabin Chris 89 Valdez Daniel 99 Valko Pete 92
Example output file:
Pat,Eaves,100 Ivy,Kay,100 Daniel,Valdez,99 Alice,Saber,98 Ashton,O'Conor,95
#include <iostream>
using namespace std;
struct participant
{
string fname;
string lname;
int score;
};
int main() {
/* Type your code here. */
return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct participant
{
string fname;
string lname;
int score;
};
void split(string line,vector<struct
participant>&vt)
{
   participant p1;
   //initilize all member of participant p1 by
default
   p1.fname="";
   p1.lname="";
   p1.score=0;
   int i=0;
   //find last name using split by space
   for(;i<line.size();i++)
   {if(line[i]==' ')break;
   p1.lname+=line[i];}
   i++;
   //find first name using split by space again
   for(;i<line.size();i++)
   {if(line[i]==' ')break;
   p1.fname+=line[i];}
   i++;
   //now find score by multiply score by 10 and add
(new character and deduct ascii value of 0)
  
for(;i<line.size()&&(line[i]>='0'&&line[i]<='9');i++)
   p1.score=p1.score*10+line[i]-'0';
   vt.push_back(p1);
}
bool comp(struct participant a, struct participant b)
{   return a.score>b.score;
}
int main() {
/* Type your code here. */
  
   ifstream fin;
// by default open mode = ios::in mode
fin.open("input.txt");
// Execute a loop until EOF (End of File)
   string line;
  
   vector<struct participant>vt;
while (fin) {
  
// Read a Line from File
getline(fin, line);
   if(line=="\0")break;
//split the string int fname,lname,score
   split(line,vt);
}
  
// Close the file
fin.close();
   //sort the vector in decresing order by scoe
   sort(vt.begin(),vt.end(),comp);
   //ofstream for output
   ofstream file;
   file.open ("output.txt");
   for(int i=0;i<vt.size()&&i<5;i++)
   {string
s=vt[i].fname+","+vt[i].lname+","+to_string(vt[i].score)+"\n";
   //write this in output file
   file << s;
   }
file.close();
  
// Close the File
  
return 0;
}
input.txt

output.txt
