TU-Programmieren_2/exercise2/task2.cpp
2025-04-09 10:22:44 +02:00

117 lines
3.8 KiB
C++

#include "task2.hpp" // count_gt|count_lt|select_gt|select_lt|select_gt_and_lt|mean|median|minmax
#include <algorithm>
/// @todo Include standard library headers as needed
/// @brief Counts how many values in a sequence are greater than (gt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Number of values in the sequence, which are greater than than ref
int count_gt(std::vector<double> data, double ref) {
int count = 0;
for (int i = 0; i < data.size(); i++) {
if (data[i] > ref) {
count++;
}
}
return count;
}
/// @brief Counts how many values in a sequence are less than (lt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Number of values in the sequence, which are less than than ref
int count_lt(std::vector<double> data, double ref) {
int count = 0;
for (int i = 0; i < data.size(); i++) {
if (data[i] < ref) {
count++;
}
}
return count;
}
/// @brief Selects values from a sequence which are greater than (gt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_gt(std::vector<double> data, double ref) {
std::vector<double> selected;
for (int i = 0; i < data.size(); i++) {
if (data[i] > ref) {
selected.push_back(data[i]);
}
}
return selected;
}
/// @brief Selects values from a sequence which are less than (lt) a reference value
/// @param data Sequence of values
/// @param ref Reference value
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_lt(std::vector<double> data, double ref) {
std::vector<double> selected;
for (int i = 0; i < data.size(); i++) {
if (data[i] < ref) {
selected.push_back(data[i]);
}
}
return selected;
}
/// @brief Selects values from a sequence which are bounded by two reference values
/// @param data Sequence of values
/// @param lower Lower bound
/// @param upper Upper bound
/// @return Sequence of selected (copied) values in the order of occurence in the original sequence
std::vector<double> select_gt_and_lt(std::vector<double> data, double lower, double upper) {
std::vector<double> selected;
for (int i = 0; i < data.size(); i++) {
if (data[i] > lower && data[i] < upper) {
selected.push_back(data[i]);
}
}
return selected;
}
/// @brief Calculates the mean for a sequence of values
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Mean value (arithmetic mean)
double mean(std::vector<double> data) {
double sum = 0;
for (int i = 0; i < data.size(); i++) {
sum += data[i];
}
return sum / data.size();
}
/// @brief Calculate the median for a sequence of numbers
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Median value:
/// - if the length of the sequence is odd, the median value is "the middle value", else
/// - if the length of the sequence is even, the median value is the avarage of the "two middle values"
double median(std::vector<double> data) {
std::sort(data.begin(), data.end());
if (data.size() % 2 == 0) {
return (data[data.size() / 2 - 1] + data[data.size() / 2]) / 2;
} else {
return data[data.size() / 2];
}
}
/// @brief Finds the minimum and maximum value
/// @param data Sequence of values; assertion: data.size() >= 1
/// @return Tuple with the minimum and maximum value (in this order)
std::tuple<double, double> minmax(std::vector<double> data) {
double min = data[0];
double max = data[0];
for (int i = 1; i < data.size(); i++) {
if (data[i] < min) {
min = data[i];
}
if (data[i] > max) {
max = data[i];
}
}
return std::make_tuple(min, max);
}