In: Computer Science
c++
students.txt
20
Shawn Lynch 2.0 Hasan Stephens 2.6 Frank Wright 2.7 Hugo Ray 2.9 Justin Gardner 3.0 Kelly Jenkins 2.2 Rafael Seymour 3.7 Jose Cantu 0.6 David Gilmore 1.3 Emma Paterson 2.1 Jackie White 1.9 Robert Green 3.8 Julio Armstrong 1.1 Erik Cook 4.0 Jessica Hale 3.0 Vanessa Rivera 0.9 Sandra Ferguson 3.1 Christian Wang 1.1 Jackson Martinez 1.9 Austin Black 4.0
For your program, you will need to define a class Student, with private members for first name, last name, and GPA, and any methods you determine that you need (constructors, gets/sets, etc.) Once the class is defined, you will need to populate an array of Student objects to be sorted with data provided in the students.txt file.
First line in the text file will be size of the array.
Every line after the first contains three pieces of information, separated by spaces: First Name, Last Name, and GPA
and sort the Student data by GPA.(Using MergeSort or QuickSort)
display unsorted data and sorted data
//C++ Code
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class Student
{
private:
string firstName;
string lastName;
double gpa;
public:
//getters
string getFirstName()
{
return firstName;
}
string getLastName()
{
return lastName;
}
double getGPA()
{
return gpa;
}
//setters
void setFirstName(string fname)
{
firstName = fname;
}
void setLastName(string lname)
{
lastName = lname;
}
void setGPA(double gpa)
{
this->gpa = gpa;
}
void display()
{
cout << fixed <<
setprecision(1);
cout<< "Student Name:
"<<setw(10)<< firstName << setw(15) <<
lastName<<setw(15) << "GPA:" << setw(5) <<
gpa << endl;
}
};
//function prototype
void sort(Student st[], int low, int high);
int partition(Student st[], int low, int high);
void swap(Student &s1, Student &s2);
int main()
{
//varibles
string fname, lname;
double gpa;
//open file
ifstream infile("students.txt");
if (!infile)
{
cout << "File not
found!";
return -1;
}
//read size
int size = 0;
infile >> size;
//create dynamic array of students
Student* students = new Student[size];
int i = 0;
//Read the data from file
cout << "Before Sort\n\n";
while (!infile.eof())
{
infile >> fname >>
lname >> gpa;
students[i].setFirstName(fname);
students[i].setLastName(lname);
students[i].setGPA(gpa);
students[i].display();
i++;
}
cout << "\nAfter Sort\n\n";
//Call sort
sort(students, 0, size - 1);
for (int i = 0; i < size; i++)
{
students[i].display();
}
//pause
system("pause");
}
//Function definition
int partition(Student st[], int low, int high)
{
Student pivot = st[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (st[j].getGPA() <
pivot.getGPA())
{
i++;
swap(st[i],
st[j]);
}
}
swap(st[i + 1], st[high]);
return (i + 1);
}
void swap(Student &s1, Student &s2)
{
Student s = s1;
s1 = s2;
s2 = s;
}
void sort(Student st[], int low,int high)
{
if (low < high)
{
int p = partition(st, low,
high);
sort(st, low, p - 1);
sort(st, p + 1, high);
}
}
//File
//Output
//If you need any help regarding this solution ....... please leave a comment ....... thanks