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...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a series of emails. Console Email Creator ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi James, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Josephine, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Art, We've got some great deals for you. Check our website! Specifications...
In this assignment you will write a PHP program that reads from a data file, creates...
In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table. Create a PHP file called hw4.php that will do the following: - Display your name. - Read text data from a file. The data file is hw3.txt. The file hw3.txt will hold the following text: PQRParrot, Quagga, Raccoon DEFDo statements, Else statements, For statements GHIGeese, Hippos, If statements...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT