Question: this assignment has two parts, i have done part one but need help with part two. here is part 1 of the question: this program
this assignment has two parts, i have done part one but need help with part two. here is part 1 of the question: this program is to create an array of pointers to objects of thepersonclass. The program can sort a group ofpersonobjects based on the alphabetical order of their names or the numerical order of their salary. part two requires us to re do the program without the function "order"
//////////////////////////////////////////////////////////////////// code below //////////////////////////////////////////////////////////////////// #include
using namespace std;
class person { private: string name; float salary; public: void setPerson() { cout << "Enter name: "; cin >> name; cout << "Enter salary: "; cin >> salary; } string getName() { return name; }
float getSalary() { return salary; } };
void order(person** a, person** b) //orders two pointers { person *temp = *a; *a = *b; *b = temp; } void bsort(person **p, int n, bool s) { for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if((s == true && (p[j]->getName() < p[i]->getName())) || (p[j]->getSalary() < p[i]->getSalary())) order(&p[i], &p[j]); } } }
void display(person **p, int n){ for(int i = 0; i< n; i++) cout << p[i]->getName() << " and salary is : " << p[i]->getSalary() << endl; cout << endl; }
int main(){ string choice; person **p = new person *[20]; int n = 0; do{ p[n] = new person(); p[n]->setPerson(); n++; cout << "more people? (y/n) : "; cin >> choice; }while(choice == "y" || choice == "Y"); cout << "sort by salary or name? (s/n): "; cin >> choice; cout << " Unsorted list" << endl; display(p, n); if(choice == "n") bsort(p, n, true); else bsort(p, n, false); cout << "Sorted list" << endl; display(p, n); for(int i = 0; i < n; i++) delete p[i]; delete []p; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
