0% found this document useful (0 votes)
10 views2 pages

D Que2.cpp

The document contains C++ code that defines a class for representing a person and functions to find the tallest and oldest person from a list. There is a bug in the code that causes it to crash when executed, which needs to be identified using GDB. The code includes methods for sorting and accessing person details, but the specific issue leading to the crash is not detailed in the document.

Uploaded by

sidgft1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

D Que2.cpp

The document contains C++ code that defines a class for representing a person and functions to find the tallest and oldest person from a list. There is a bug in the code that causes it to crash when executed, which needs to be identified using GDB. The code includes methods for sorting and accessing person details, but the specific issue leading to the crash is not detailed in the document.

Uploaded by

sidgft1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// The code is supposed to find the tallest and oldest person from a list of

people. However, there's a bug in the code that causes it to crash when run. Use
GDB to debug the code and find the bug.

#include <bits/stdc++.h>

// Class to represent a person


class Person {
public:
std::string name;
int age;
int height;

// Constructor to initialize person's details


Person(std::string n, int a, int h) {
name = n;
age = a;
height = h;
}

// Getter methods to access person's details


std::string getName() const{ return name; }
int getAge() const{ return age; }
int getHeight() const{ return height; }
};

// Function to sort people by age


bool sortByAge(const Person& a, const Person& b) {
return a.getAge() < b.getAge();
}

// Function to find the tallest person


Person findTallestPerson(const Person people[], int size) {
Person tallest = people[0];
for (int i = 1; i < size; i++) {
if (people[i].getHeight() > tallest.getHeight()) {
tallest = people[i];
}
}
return tallest;
}

// Function to find the oldest person


Person findOldestPerson(const Person people[], int size) {
Person oldest = people[0];
for (int i = 1; i < size; i++) {
if (people[i].getAge() > oldest.getAge()) {
oldest = people[i];
}
}
return oldest;
}

// Function to sort people by age


void sortPeopleByAge(Person people[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (people[i].getAge() > people[j].getAge()) {
Person temp = people[i];
people[i] = people[j];
people[j] = temp;
}
}
}
}

int main() {
// Create an array of people
Person people[] = {
Person("John", 25, 170),
Person("Alice", 30, 165),
Person("Bob", 20, 180),
Person("Charlie", 35, 175),
Person("David", 40, 160)
};

int size = sizeof(people) / sizeof(people[0]);

// Sort people by age


sortPeopleByAge(people, size);

// Find the tallest person


Person tallest = findTallestPerson(people, size);

// Find the oldest person


Person oldest = findOldestPerson(people, size);

// Print the results


std::cout << "Tallest person: " << tallest.getName() << std::endl;
std::cout << "Oldest person: " << oldest.getName() << std::endl;

return 0;
}

You might also like