I am stuck trying at sorting the students from highest to lowest in my C++ progr
ID: 3722140 • Letter: I
Question
I am stuck trying at sorting the students from highest to lowest in my C++ program
-----------------------------------------------------------------------------------------------------------
There is a class called Person which has name and age as private variables.
Another class called Student, which is derived from person with gpa and id as variables.
name is a string, age and id are integers and gpa is a double... All of them are private variables.
age, gpa and id should be generated randomly when the object is created with the following ranges:
age 20 to 32
gpa 0.0 to 4.0 // this one is tricky to get as random doesnt generate decimal numbers.
id 1000 to 9999
In the start of the program, you will ask for how many students there are. dynamically create an array of students of that size. (we are not creating an object of type person for this test).
In a loop, you will ask for the names of each of these people (all the other data was already created randomly through the constructors).
Once you are done, you are to display everyone in the array neatly.
Sort the students by gpa (from highest to lowest, make sure you sort the students not just swapping of gpa)
and display the sorted array neatly. (show the Students, not just the GPA's).
------------------------------------------------------------------------------------------------------------------------------------------------------------
my code
# include
# include
# include
# include
# include
# include
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person ()
{
name = " ";
age = 0;
}
Person (string n, int a)
{
name = n;
age = a;
}
void setInfo(string n, int a)
{
name = n;
age = a;
}
void setName(string n)
{
name = n;
}
void setAge()
{
int a = rand() % 12 + 21;
age = a;
}
string getName()
{
return name;
}
int getAge()
{
return age;
}
};
class Student:public Person
{
private:
double gpa;
int id;
public:
Student ()
{
gpa = 0.0;
id = 0;
}
Student(string n, int a, double g, int i)
{
Person::setInfo(n,a);
gpa = g;
id = i;
}
void setALL(string n, int a, double g, int i)
{
Person::setInfo(n,a);
gpa = g;
id = i;
}
void setGPA()
{
double g = rand() % 3 + 1;
gpa = g;
}
void setID()
{
int i = 1 + rand() % 8999 + 1000;
id = i;
}
double getGPA()
{
return gpa;
}
int getID()
{
return id;
}
};
int main()
{
int numStu;
string name;
Student *S;
Student A;
cout<<"How many students are there?"<
cin>>numStu;
S = new Student[numStu];
vector stu(numStu);
for (int i = 0; i < numStu; i++)
{
cout<<"What is the name for person "<
cin>>name;
S[i].setName(name);
S[i].setAge();
S[i].setID();
S[i].setGPA();
}
for ( int x=0;x
{
cout<
}
return 0;
}
Explanation / Answer
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <ctime>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person ()
{
name = " ";
age = 0;
}
Person (string n, int a)
{
name = n;
age = a;
}
void setInfo(string n, int a)
{
name = n;
age = a;
}
void setName(string n)
{
name = n;
}
void setAge()
{
int a = rand() % 12 + 21;
age = a;
}
string getName()
{
return name;
}
int getAge()
{
return age;
}
};
class Student:public Person
{
private:
double gpa;
int id;
public:
Student ()
{
gpa = 0.0;
id = 0;
}
Student(string n, int a, double g, int i)
{
Person::setInfo(n,a);
gpa = g;
id = i;
}
void setALL(string n, int a, double g, int i)
{
Person::setInfo(n,a);
gpa = g;
id = i;
}
void setGPA()
{
double g = rand() % 3 + 1;
gpa = g;
}
void setID()
{
int i = 1 + rand() % 8999 + 1000;
id = i;
}
double getGPA()
{
return gpa;
}
int getID()
{
return id;
}
};
int main()
{
int numStu;
string name;
Student *S;
Student A;
cout<<"How many students are there?"<
cin>>numStu;
S = new Student[numStu];
for (int i = 0; i < numStu; i++)
{
cout<<"What is the name for person "<
cin>>name;
S[i].setName(name);
S[i].setAge();
S[i].setID();
S[i].setGPA();
}
for ( int x=0;x<numStu-1;x++){
int m =x;
for(int y=x+1;y<numStu;y++){
if(S[y].getGPA() > S[m].getGPA()){
m=y;
}
}
Student temp=S[m];
S[m]=S[x];
S[x]=temp;
}
for ( int x=0;x<numStu;x++){
cout<<S[x].getName()<<" "<<S[x].getID()<<" "<<S[x].getAge()<<" "<<S[x].getGPA()<<" ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.