Question

In: Computer Science

c++ Create a program that creates a sorted list from a data file. The program will...

c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last name age The group class should have the following functions: Constructor(s) Destructor - optional Get - read first name, last name, and age from an input stream Put - write last name, first name, and age to an output stream The group class should have the following operators: > < == compare the group to another group, using Last name, First name, then age, and return a bool Your program should do the following: Prompt the user for the name of the file Open the file Read the data into the array of group objects (maximum size 20) Close the file Sort the array Display the array ============== text file Ann ember 70 jacob Mark 68 David smith 45 Frank lee 37 John doe 30 Kathleen honor 34 bob ember 42 bob ember 13 Richard start 47 Susan hox 36 Expert Answer

Solutions

Expert Solution

PLEASE GIVE THUMBS UP, THANKS

SAMPLE OUTPUT:

CODE :

#include<iostream>
#include<fstream>
using namespace std;
//GROUP CLASS
class group
{
   //PRIVATE MEMBERS
   private:
       string fname;
       string lname;
       int age;
   public:
       //FUNCTIONS
       void Get(ifstream &in)
       {
       if(in)
       {
           in>>fname>>lname>>age;
       }  
       }
       void Put()
       {
           cout<<fname<<" "<<lname<<" "<<age<<endl;
       }
       int operator <(group &A)
       {
           if(age<A.age)
           return 1;
           else
           return 0;
       }
       int operator >(group &A)
       {
           if(age>A.age)
           return 1;
           else
           return 0;
       }
       int operator ==(group &A)
       {
           if(age==A.age)
           return 1;
           else
           return 0;
       }
       void setfname(string f)
       {
           fname=f;
       }
       void setlname(string f)
       {
           lname=f;
       }
       void setage(int i)
       {
           age=i;
       }
       string getfname()
       {
           return fname;
       }
       string getlname()
       {
           return lname;
       }
       int getage()
       {
           return age;
       }
      
};
// A function to implement sort
void Sort(group arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)   
  
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j].getage()<arr[j+1].getage())
       {
           group temp;
           temp.setfname(arr[j].getfname());
           temp.setlname(arr[j].getlname());
           temp.setage(arr[j].getage());
          
           arr[j].setfname(arr[j+1].getfname());
           arr[j].setlname(arr[j+1].getlname());
           arr[j].setage(arr[j+1].getage());
          
           arr[j+1].setfname(temp.getfname());
           arr[j+1].setlname(temp.getlname());
           arr[j+1].setage(temp.getage());
       }
}
int main()
{
   group D[20];
   int n=0;
   ifstream infile;
   string filename;
   cout<<"Enter Filename: ";
   cin>>filename;
   infile.open(filename.c_str());
   if(!infile)
   {
       cout<<"Unable to open file"<<endl;
       return 0;
   }
   while(infile)
   {
       D[n].Get(infile);
       n++;
   }
   Sort(D,n);
   for(int i=0; i<n; i++)
   {
       D[i].Put();
   }
}


Related Solutions

Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to an output file.
Concepts tested by this program            Hash Table,            Link List,hash code, buckets/chaining,exception handling, read/write files (FileChooser)A concordance lists every word that occurs in a document in alphabetical order, and for each word it gives the line number of every line in the document where the word occurs.Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to...
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
C program! Create a list of 5 things-to-do when you are bored (in the text file...
C program! Create a list of 5 things-to-do when you are bored (in the text file things.txt, one line each), where each thing is described as a string of characters of length in between 10 and 50. Design a C program to read these things in (from stdin or by input redirection) and store them in the least memory possible (i.e., only the last byte in the storage for each string can be the null character). After reading things in,...
Create an ADT class that creates a friend contact list. The program should be able to...
Create an ADT class that creates a friend contact list. The program should be able to add, remove and view the contacts. In C++
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and...
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and stores them in a 1D array of size 15. Next, prompt the user for a number to search for in the array (target). Then, print the array. Next, search the array using a linear search – printing out each of the indices (or “indexes”) that are being examined until the algorithm either finds the target or doesn’t. Then, do the same thing for a...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT