Question: Project 5.b Modify the bubble sort function presented in chapter 9 so that it sorts an array of Persons from oldest to youngest (use the
Project 5.b
Modify the bubble sort function presented in chapter 9 so that it sorts an array of Persons from oldest to youngest (use the Person class from Project 4.b). It should take two parameters, an array of Persons, and the size of the array. Your function must be named personSort.
The files must be named Person.hpp, Person.cpp and personSort.cpp
Person.hpp
#ifndef PERSON_HPP #define PERSON_HPP
#include #include
class Person {
public: Person(void); Person(std::string n, double a); //Constructor std::string getName(); double getAge();
private: std::string nameMember; double ageMember; };
#endif
Person.cpp
#include #include #include #include "Person.hpp"
Person::Person(void) { nameMember = " "; ageMember = 0; } Person::Person(std::string nameIn, double ageIn) { nameMember = nameIn; ageMember = ageIn; }
std::string Person::getName() { return nameMember; }
double Person::getAge() { return ageMember; }
PersonSort.cpp - Must modify code to use current main and sort from oldest to youngest age
#include "Person.hpp" #include #include
// This program uses the bubble sort algorithm to sort an array // of integers in ascending order.
// Function prototypes void personSort(int [], int); void showArray(const int [], int);
int main() // Must use this main function { #include "Person.cpp" Person p0("Archibald", 42); Person p1("Bertrand", 19); Person p2("Charles", 54); Person array[] = {p0,p1,p2}; personSort(array, 3);
}
/************************************************************ * personSort * * This function performs an ascending-order bubble sort on * * array. The parameter size holds the number of elements * * in the array. * ************************************************************/ void personSort(int array[], int size) { int temp; bool swap;
do { swap = false; for (int count = 0; count < (size - 1); count++) { if (array[count] > array[count + 1]) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = true; } } } while (swap); // Loop again if a swap occurred on this pass. }
/************************************************************* * showArray * * This function displays the contents of array. The * * parameter size holds the number of elements in the array. * *************************************************************/ void showArray(const int array[], int size) { for (int count = 0; count < size; count++) std::cout << array[count] << " "; std::cout << std::endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
