Average and median.
This commit is contained in:
parent
25f1c13adf
commit
17c38c219f
91
main.cpp
91
main.cpp
@ -1,68 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
using string = std::string;
|
||||
using stringv = std::string_view;
|
||||
|
||||
void printm(stringv);
|
||||
void printlm(stringv);
|
||||
int prompti(stringv);
|
||||
string prompts(stringv);
|
||||
string promptm(stringv*, int);
|
||||
|
||||
int main() {
|
||||
bool run {true};
|
||||
while (run){
|
||||
int input { prompti(promptm(new stringv[] {
|
||||
"Enter an option:",
|
||||
"0) Say hello.",
|
||||
"*) Exit."}, 3)) };
|
||||
if (input == 0)
|
||||
printlm("Hello.");
|
||||
else{
|
||||
printlm("Bye!");
|
||||
run = false;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printm(stringv m){
|
||||
//Placw in header!
|
||||
template <typename T>
|
||||
void printm(T m){
|
||||
std::cout << m;
|
||||
}
|
||||
|
||||
void printlm(stringv m){
|
||||
template <typename T>
|
||||
void printlm(T m){
|
||||
printm(m);
|
||||
printm("\n");
|
||||
}
|
||||
|
||||
int prompti(stringv message) {
|
||||
int tr {};
|
||||
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(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(halfway));
|
||||
else
|
||||
printlm((v.at(halfway - 1) + v.at(halfway)) / 2.0);
|
||||
double avg {};
|
||||
for (int i = 0; i < static_cast<int>(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 -1;
|
||||
return 0;
|
||||
}
|
||||
*status = true;
|
||||
return tr;
|
||||
}
|
||||
|
||||
string prompt(stringv message) {
|
||||
string toret {""};
|
||||
std::cout << message;
|
||||
if (!(std::cin >> toret)){
|
||||
std::cin.clear();
|
||||
std::cin.ignore();
|
||||
return "";
|
||||
}
|
||||
return toret;
|
||||
}
|
||||
|
||||
string promptm(stringv msgs[], int l) {
|
||||
string tr {""};
|
||||
for (int i = 0; i < l; i++) {
|
||||
tr.append(msgs[i]);
|
||||
tr.append("\n");
|
||||
}
|
||||
return tr;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user