Solved Homework: Question: C++ Help. Please make sure the code below FOLLOWS ALL DIRECTIONS- PLEASE CORRECT IF IT DOES NOT. …

C++ Help. Please make sure the code below FOLLOWS ALL DIRECTIONS- PLEASE CORRECT IF IT DOES NOT. PLEASE ADD COMMENTS TO CODE FOR BETTER CLARITY. THANK YOU.

Instructions:

Implement a class Person with the following data members:

name (string) – name of this person

age (integer) – age of this person

Write a program that reads in a list of names and ages and stores them in a one-dimensional array of Person objects. The maximum number of names that will be entered is 100 names. After reading in the list of names and ages, sort the list of people from the youngest (lowest age) to oldest (highest age) using the Bubble Sort. Then print out the name and age for each person in the sorted list.

Sample Output (user input is bold) :

Enter name (-1 to stop): Bart

Enter age of Bart: 10

Enter name (-1 to stop): Lisa

Enter age of Lisa: 8

Enter name (-1 to stop): Maggie

Enter age of Maggie: 1

Enter name (-1 to stop): Homer

Enter age of Homer: 36

Enter name (-1 to stop): Marge

Enter age of Marge: 34

Enter name (-1 to stop): -1

Name: Maggie, age: 1

Name: Lisa, age: 8

Name: Bart, age: 10

Name: Marge, age: 34

Name: Homer, age: 3

———————————————————————————————————————————————————————————————

Code:

#include<iostream>
#include<string>
#define MAX_PERSON 100

using namespace std;

//Person Class
class Person
{
private: string name;
int age;
public: Person() {}
Person(string n, int a)
{
name = n;
age = a;
}

//bubble sort method for sorting
static void sort(Person *array, int size)
{
for (int i = 1; i < size; i++)
{
for (int j = 0; j < (size – i); ++j)
{
if (array[i].getAge() > array[j].getAge())
{
Person temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

//getters for nam and age
string getName() { return name; }
int getAge() { return age; }

//method to print name and age
static void printPerson(Person p)
{
cout << “Name: ” << p.getName() << “, Age: ” << p.getAge() << endl;
}
};

//Main Class
int main(int argc, char const *argv[])
{
//created array for MAX PERSON
Person *array = new Person[MAX_PERSON];
int i = 0;
for(i=0; i<MAX_PERSON; i++)
{
string name;
int age;
cout << “Enter name(-1 to stop): “;
cin >> name;
if (name==”-1″)
break;
cout << “Enter age of ” << name << “: “;
cin >> age;
array[i] = Person(name, age);

}

//sorting the person
Person::sort(array, i);

//printing the person
for (int j = 0; j < i; j++)
{
Person::printPerson(array[j]);
}
return 0;
}

Expert Answer

answers

Modified Program:

#include<iostream>
#include<string>
#define MAX_PERSON 100

using namespace std;

//Person Class
class Person
{
private: string name;
int age;
public: Person() {}
Person(string n, int a)
{
name = n;
age = a;
}

//bubble sort method for sorting
static void sort(Person *array, int size)
{
for (int i = 1; i < size; i++)
{
for (int j = 0; j < (size – i); ++j)
{
if (array[j].getAge() > array[j+1].getAge())//here the logic is wromg and I have changed it now it will work fine
{
Person temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

//getters for nam and age
string getName() { return name; }
int getAge() { return age; }

//method to print name and age
static void printPerson(Person p)
{
cout << “Name: ” << p.getName() << “, Age: ” << p.getAge() << endl;
}
};

//Main Class
int main(int argc, char const *argv[])
{
//created array for MAX PERSON
Person *array = new Person[MAX_PERSON];
int i = 0;
for(i=0; i<MAX_PERSON; i++)
{
string name;
int age;
cout << “Enter name(-1 to stop): “;
cin >> name;
if (name==”-1″)
break;
cout << “Enter age of ” << name << “: “;
cin >> age;
array[i] = Person(name, age);

}

//sorting the person
Person::sort(array, i);

//printing the person
for (int j = 0; j < i; j++)
{
Person::printPerson(array[j]);
}
return 0;
}

Output:

optionscompilation execution Enter name(-1 to stop): Bart Enter age of Bart: 10 Enter name(-1 to stop): Lisa Enter age of Lisa: 8 Enter name(-1 to stop): Maggie Enter age of Maggie: 1 Enter name(-1 to stop): Homer Enter age of Homer: 36 Enter name(-1 to stop): Marge Enter age of Marge: 34 Enter name(-1 to stop): -1 Name: Maggie, Age: 1 Name: Lisa, Age: 8 Name: Bart, Age: 10 Name: Marge, Age: 34 Name: Homer, Age: 36 0 Exit code: e (normal program termination)

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?