64 lines
1.4 KiB
C++
64 lines
1.4 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <algorithm>
|
|
#include <iomanip>
|
|
|
|
using string = std::string;
|
|
|
|
//Placw in header!
|
|
template <typename T>
|
|
void printm(T m){
|
|
std::cout << m;
|
|
}
|
|
|
|
template <typename T>
|
|
void printlm(T m){
|
|
printm(m);
|
|
printm("\n");
|
|
}
|
|
|
|
typedef std::vector<double>::size_type vtype;
|
|
|
|
double promptd(string,bool*);
|
|
|
|
int main() {
|
|
std::setprecision(3);
|
|
printlm("Calculate median and average from sequence:");
|
|
std::vector<double> v{};
|
|
bool s {};
|
|
do {
|
|
auto d = promptd("Enter a number (Enter non-number to complete entry) : ",&s);
|
|
if (s) v.push_back(d);
|
|
}
|
|
while(s);
|
|
sort(v.begin(), v.end());
|
|
printm("The count is: ");
|
|
printlm(static_cast<int>(v.size()));
|
|
printm("The median is: ");
|
|
int halfway {static_cast<int>(v.size()) / 2};
|
|
if (static_cast<int>(v.size())%2 == 1)
|
|
printlm(v.at(static_cast<vtype>(halfway)));
|
|
else
|
|
printlm((v.at(static_cast<vtype>(halfway - 1)) + v.at(static_cast<vtype>(halfway))) / 2.0);
|
|
double avg {};
|
|
for (vtype i = 0; i < v.size(); ++i) avg += v.at(i);
|
|
avg /= static_cast<int>(v.size());
|
|
printm("The average is: ");
|
|
printlm(avg);
|
|
return 0;
|
|
}
|
|
|
|
double promptd(string message, bool *status) {
|
|
double tr {};
|
|
*status = false;
|
|
std::cout << message;
|
|
if (!(std::cin >> tr)){
|
|
std::cin.clear();
|
|
std::cin.ignore();
|
|
return 0;
|
|
}
|
|
*status = true;
|
|
return tr;
|
|
}
|
|
|