[Solved] logic help for smallest/largest value


You need to choose a standard unit of measure. The question suggests meters, so use that (you can use float or double for this, depending on what precision you need).

The problem is then simple, create some variables for the sum, smallest seen, and largest seen, the for each new input, convert to the standard format, and update the variables.

The solution (to get you started, not working code) might look something like this:

// You can represent the different types of units as integers
float convertToMeters(float unconvertedValue, int unit) {
  // Convert unconvertedValue based on unit
}

float smallest = std::numeric_limits<float>::max();
float largest  = std::numeric_limits<float>::lowest();
float sum      = 0.0f;

// Update for each new input
while (new_input) {
  float convertedValue = convertToMeters(new_value, unit);

  // Update total
  sum += convertedValue;

  // Update smallest and largest
  if (convertedValue > largest) largest = convertedValue;
  else if (convertedValue < smallest) smallest = convertedValue;
}

As Nathan mentioned, #include <limits> for the limits.

3

solved logic help for smallest/largest value