fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. // Function to compute minimum value in a vector
  6. double minValue(const std::vector<double>& values) {
  7. return *std::min_element(values.begin(), values.end());
  8. }
  9.  
  10. // Function to compute maximum value in a vector
  11. double maxValue(const std::vector<double>& values) {
  12. return *std::max_element(values.begin(), values.end());
  13. }
  14.  
  15. // Function to compute percentile of a vector
  16. double percentile(const std::vector<double>& values, double percentile) {
  17. std::vector<double> sortedValues = values;
  18. std::sort(sortedValues.begin(), sortedValues.end());
  19. size_t n = sortedValues.size();
  20. double rank = percentile * (n - 1);
  21. size_t lower = static_cast<size_t>(rank);
  22. double fraction = rank - lower;
  23. if (lower + 1 < n) {
  24. return sortedValues[lower] + fraction * (sortedValues[lower + 1] - sortedValues[lower]);
  25. } else {
  26. return sortedValues[lower];
  27. }
  28. }
  29.  
  30. int main() {
  31. std::vector<double> data = {10, 20, 30, 40, 50};
  32.  
  33. // Calculate range
  34. double range = maxValue(data) - minValue(data);
  35.  
  36. // Calculate interquartile range (IQR)
  37. double q1 = percentile(data, 0.25);
  38. double q3 = percentile(data, 0.75);
  39. double iqr = q3 - q1;
  40.  
  41. // Print results
  42. std::cout << "Range: " << range << std::endl;
  43. std::cout << "Interquartile Range (IQR): " << iqr << std::endl;
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0.02s 25212KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <vector>
#include <algorithm>

// Function to compute minimum value in a vector
double minValue(const std::vector<double>& values) {
    return *std::min_element(values.begin(), values.end());
}

// Function to compute maximum value in a vector
double maxValue(const std::vector<double>& values) {
    return *std::max_element(values.begin(), values.end());
}

// Function to compute percentile of a vector
double percentile(const std::vector<double>& values, double percentile) {
    std::vector<double> sortedValues = values;
    std::sort(sortedValues.begin(), sortedValues.end());
    size_t n = sortedValues.size();
    double rank = percentile * (n - 1);
    size_t lower = static_cast<size_t>(rank);
    double fraction = rank - lower;
    if (lower + 1 < n) {
        return sortedValues[lower] + fraction * (sortedValues[lower + 1] - sortedValues[lower]);
    } else {
        return sortedValues[lower];
    }
}

int main() {
    std::vector<double> data = {10, 20, 30, 40, 50};

    // Calculate range
    double range = maxValue(data) - minValue(data);

    // Calculate interquartile range (IQR)
    double q1 = percentile(data, 0.25);
    double q3 = percentile(data, 0.75);
    double iqr = q3 - q1;

    // Print results
    std::cout << "Range: " << range << std::endl;
    std::cout << "Interquartile Range (IQR): " << iqr << std::endl;

    return 0;
}