0% found this document useful (0 votes)
13 views3 pages

C++ BMI Calculator and Sorter

The document outlines a C++ programming assignment consisting of three tasks focused on creating a 'Person' class to manage personal data and calculate BMI. The first task involves creating instances of the 'Person' class and implementing methods for greeting and BMI calculation. The subsequent tasks require sorting instances by BMI using selection sort and developing a menu-driven program to allow user input for various functionalities related to BMI and age calculations.

Uploaded by

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

C++ BMI Calculator and Sorter

The document outlines a C++ programming assignment consisting of three tasks focused on creating a 'Person' class to manage personal data and calculate BMI. The first task involves creating instances of the 'Person' class and implementing methods for greeting and BMI calculation. The subsequent tasks require sorting instances by BMI using selection sort and developing a menu-driven program to allow user input for various functionalities related to BMI and age calculations.

Uploaded by

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

LAB-1

TASK - 1

Create a C++ project called Lab1-Q1. Inside this project create a class named "Q1" with a main
function. Also create a class named "Person". The "Person" class should have the following
instance variables:

public:
std::string name;
double kilograms;
double heightMeters;
double BMI;

The "Person" class should have the following methods:

public void Greeting() - Prints to the standard output "Hello my name is 'name'"
public void calculateBMI() - Calculates BMI and prints to the standard output the person's BMI
category.
BMI is calculated by: kilograms / (heightMeters * heightMeters);
A BMI under 18.5 is under weight, a BMI under 25 is normal weight, a BMI under 30 is overweight,
and a BMI equal to or over 30 is obese.
In the class "Q1" that has the main function, create 3 separate instances of the person class and
assign each person a name, kilograms, and heightMeters. Then call the Greeting() and
calculateBMI() method on each person.

Task 2:

Sorting People by BMI using Selection Sort

• Modify the Person class from Task 1.


• Create an array of Person objects.
• Sort them in ascending order of BMI using Selection Sort.
• Print the sorted list of people with their names and BMI values.

Expected Output:

Hello, my name is Aubrey Graham.


Aubrey Graham's BMI is 24.80
Aubrey Graham's risk category is Normal weight.

Hello, my name is CJ Johnson.


CJ Johnson's BMI is 36.05
CJ Johnson's risk category is Obese.

Hello, my name is Rachel Green.


Rachel Green's BMI is 15.25
Rachel Green's risk category is Underweight.

Sorting people by BMI...


Sorted List:
1. Rachel Green - BMI: 15.25
2. Aubrey Graham - BMI: 24.80
3. CJ Johnson - BMI: 36.05

Task 3:
Comprehensive Program Implementation
• Extend the Person class to allow user input for:
o Name
o Weight
o Height
o Age
• Implement a menu-driven program where the user can:
o Calculate BMI
o Sort people by BMI using Selection Sort
o Compute the GCD of two ages using the Euclidean Algorithm
• Ensure the output format matches the example below.
Expected Output:
Welcome to the BMI Program!
Please select an option:
1. Calculate BMI
2. Sort people by BMI
3. Exit
Enter your choice: 1

Enter name: Aubrey Graham


Enter weight (kg): 70
Enter height (m): 1.75

Hello, my name is Aubrey Graham.


Aubrey Graham's BMI is 22.86
Aubrey Graham's risk category is Normal weight.

Please select an option:


1. Calculate BMI
2. Sort people by BMI
3. Exit
Enter your choice: 2

Sorting people by BMI...


Sorted List:
1. Rachel Green - BMI: 15.25
2. Aubrey Graham - BMI: 22.86
3. CJ Johnson - BMI: 36.05

Please select an option:


1. Calculate BMI
2. Sort people by BMI
3. Exit
Enter your choice: 3

Exiting program... Goodbye!

Common questions

Powered by AI

The Selection Sort algorithm functions by iterating over the list of 'Person' objects to find the minimum BMI value and then swapping it with the value at the start of the unsorted section of the array. This process is repeated, progressively closing in on the end of the array until the entire list is sorted in ascending order of BMI. The sorted list shows the names of the people along with their BMI values .

When executing a selection sort on an array of people by their BMI values, the program follows these steps: Starting at the beginning of the array, it selects the first element as the minimum. It then iterates through the remaining elements to find the smallest BMI. The smallest element found is swapped with the first element. The process is repeated starting with the second element, finding the next smallest BMI from the unsorted part, and performing a swap. These steps continue until the list is fully sorted, resulting in an ascending order of BMI values .

Integrating GCD computation into the menu-driven program serves both a functional purpose and enhances user experience by offering additional mathematical capabilities. It allows users to interact with the program beyond BMI calculations and sorting. The functional benefit lies in computational practice and accommodating educational use, while user engagement is enhanced through diverse feature offerings. However, the relevance depends on the context of use and user needs .

In the C++ program, each person's BMI is determined by dividing their weight in kilograms by the square of their height in meters. The resulting BMI value is then categorized into risk categories as follows: underweight (BMI < 18.5), normal weight (BMI < 25), overweight (BMI < 30), and obese (BMI >= 30). These categories are printed alongside the calculated BMI to inform the user about their health risk level .

The C++ program ensures smooth execution from user input to result output by defining clear data types for each attribute such as 'std::string' for names and 'double' for kilograms and heightMeters, ensuring precision in BMI calculation. The program captures user inputs, handles exceptions or invalid entries for robust input processing, and utilizes well-defined functions for BMI calculation and sorting. By maintaining separation of concerns and implementing well-structured methods, the program efficiently processes different operations, delivering accurate and timely results .

The design choices in structuring the 'Person' class and the main program illustrate a modular and object-oriented approach, accommodating scalability and future extensions. The use of classes and methods allows additional features to be seamlessly integrated, such as new attributes, greater data validation, and enhanced user interaction. Modularity ensures that each class and method serve specific functionalities, which simplifies maintenance and future expansion. However, considerations around performance optimization and interface complexity become critical as the program scales .

The Euclidean Algorithm is used within the extended 'Person' class program to compute the greatest common divisor (GCD) of two ages provided by the user. This is achieved through a series of modulo operations that continuously reduce the pair of numbers until the remainder becomes zero, at which point the GCD is the last non-zero remainder. This feature is incorporated to provide additional mathematical computation capabilities within the program, increasing its functionality .

The user interaction in the C++ project is managed through a menu-driven program that prompts users to select from several options: calculate BMI, sort people by BMI, or exit the program. The program responds to user input by executing the corresponding functionality, such as collecting user data for BMI calculation, performing a sort operation on the list of people, or computing the GCD of ages if implemented. This structure enables clear navigation and enhanced interaction by providing users with predefined choices .

The comprehensive program implementation extends the 'Person' class to include user inputs for attributes such as name, weight, height, and age. This enhancement allows users to provide real-time data to calculate BMI and interact with the program dynamically. By facilitating user input and providing a menu-driven interface, the program becomes more user-friendly and interactive, allowing users to compute BMI, sort people by BMI, and compute the GCD of ages using the Euclidean Algorithm .

The 'Person' class in the C++ project provides two key methods: 'Greeting()' and 'calculateBMI()'. The 'Greeting()' method prints a welcome message that includes the person's name, displaying 'Hello my name is 'name''. The 'calculateBMI()' method calculates the Body Mass Index (BMI) using the formula: kilograms / (heightMeters * heightMeters), then prints the individual's BMI and corresponding risk category based on the calculated BMI value: underweight (BMI < 18.5), normal weight (BMI < 25), overweight (BMI < 30), and obese (BMI >= 30).

You might also like