Question

In: Computer Science

C++ Programming Consider an input file that lists test participants in alphabetical order and their scores....

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:

  • The name of the input file is acquired through standard input.
  • If the file does not exists, print the message "File not found!" in output.txt.
  • The output file must be named output.txt.
  • In case of a tie, output participants with the same score in alphabetical order.
  • If there are less than 5 participants, output all of them in decreasing order of scores.

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;
}

Solutions

Expert Solution

#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


Related Solutions

C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
C Programming file.c takes in one input argument that denotes a file to be read. It...
C Programming file.c takes in one input argument that denotes a file to be read. It needs to convert the contents of that file into a character array (char *) and then into a an unsigned character array (unsigned char *). Please fix and or complete the program, and explain any of the changes too: ---- #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { FILE *f; f = fopen(argv[1], "r"); if( !f ) { exit(1); }...
Create a report that lists all the customers in alphabetical order by last name. The report...
Create a report that lists all the customers in alphabetical order by last name. The report should include first name, last name, and email address of all customers. This report involves getting data from one table. The three items should be lined up in columns. Use one of the formatting functions available through Python (the % operator or the format function). Don’t use tabs to line up columns, this does not reliably work and is inflexible.1 Even though we won’t...
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
A study of the effects of color on easing anxiety compared anxiety test scores of participants...
A study of the effects of color on easing anxiety compared anxiety test scores of participants who completed the test printed on either soft yellow paper or harsh green paper. The researcher predicted that the yellow paper would produce lower anxiety scores than the green paper. The scores for the six participants who completed the test printed on the yellow paper were: 17, 19, 28, 21, 18, 20. The scores for the five participants who completed the test of the...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books...
JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books in order to identify duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called duplicateTitles.out. When complete, the output file should contain all titles that are duplicated in the input file. Note that the duplicate titles should be written once, even though the input file may contain same titles multiple...
Create a C# application for SummerSchool that reads the file Participants. participant's data on the screen....
Create a C# application for SummerSchool that reads the file Participants. participant's data on the screen. Create a Participant class that contains field first name, last name, age, and course taken for the summer school and To fields of records in the file are separated with semicolon (). 3 marks) E.g. of a record in the file Participant.txt: 123,John,Smith35:Math using System; using static System. Console; using System. I0; class SummerSchool static void Main() /7Your code as answer
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT