#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKLy8gRnVuY3Rpb24gdG8gY29tcHV0ZSBtaW5pbXVtIHZhbHVlIGluIGEgdmVjdG9yCmRvdWJsZSBtaW5WYWx1ZShjb25zdCBzdGQ6OnZlY3Rvcjxkb3VibGU+JiB2YWx1ZXMpIHsKICAgIHJldHVybiAqc3RkOjptaW5fZWxlbWVudCh2YWx1ZXMuYmVnaW4oKSwgdmFsdWVzLmVuZCgpKTsKfQoKLy8gRnVuY3Rpb24gdG8gY29tcHV0ZSBtYXhpbXVtIHZhbHVlIGluIGEgdmVjdG9yCmRvdWJsZSBtYXhWYWx1ZShjb25zdCBzdGQ6OnZlY3Rvcjxkb3VibGU+JiB2YWx1ZXMpIHsKICAgIHJldHVybiAqc3RkOjptYXhfZWxlbWVudCh2YWx1ZXMuYmVnaW4oKS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jUpOwogICAgZG91YmxlIHEzID0gcGVyY2VudGlsZShkYXRhLCAwLjc1KTsKICAgIGRvdWJsZSBpcXIgPSBxMyAtIHExOwoKICAgIC8vIFByaW50IHJlc3VsdHMKICAgIHN0ZDo6Y291dCA8PCAiUmFuZ2U6ICIgPDwgcmFuZ2UgPDwgc3RkOjplbmRsOwogICAgc3RkOjpjb3V0IDw8ICJJbnRlcnF1YXJ0aWxlIFJhbmdlIChJUVIpOiAiIDw8IGlxciA8PCBzdGQ6OmVuZGw7CgogICAgcmV0dXJuIDA7Cn0K
#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;
}