intro to object oriented programming qn

Objective

The objective of this problem is to introduce a simple Object-Oriented Programming (OOP) problem and ensure that students understand the concept of OOP. In this case, it’s tested with the understanding of object.
Problem Description
Given a group of people with different heights and weights, determine the shortest and tallest people in the group, and calculate their body mass index(BMI).
The formula for BMI is
BMI = weight(in kg)/height(in meters)^2

Input

The first line of the input contains an integer N (2 <= N <= 100) denoting the number of people in the group. The next N lines contain the information (name, height in centimeters, and weight in kilograms) of the people in the group.

Output
Output the name of the shortest and tallest people in the group, assuming that there is only one shortest person and one tallest person in the group.

Suppose A is the shortest and B is the tallest person in the group, the output will be:

A is the shortest with BMI equals to C.
B is the tallest with BMI equals to D.
Output the BMI value correct to 2 decimal places.

Please refer to sample output for more details.
Sample Input

4
Diamond 178 55
Jarod 160 80
Douglas 180 60
Rod 151 48

Sample Output

Rod is the shortest with BMI equals to 21.05.
Douglas is the tallest with BMI equals to 18.52.

-------------------------------------------------
my method was to create a structure "student" which had a string with a name, and two integer variables to take in the height and weight.

create 2 "student" variables named talleststudent and shorteststudent
make a for loop to scan in the right number of times based on integer N from the user. create a simple algorithm to compare values and allocate them to variables talleststudent and shorteststudent and then print it out.

any idea how to do this using classes and objects?

Last edited on
I would probably create a Person class, with a string name, integers for height and weight and a GetBMI function, which returns the BMI as a double or float. Alternatively, you could have a BMI variable, and work it out in the constructor, assuming you have valid height and weight values.

Since we know the number of Person classes we need, I'd probably create an array of them. Alternatively, you could use something more dynamic, like a vector.

For the tallest / shortest, I'd simply create a couple of pointers and loop through the students, comparing their heights and assigning accordingly.
Topic archived. No new replies allowed.