1
0
This commit is contained in:
Captain ALM 2022-10-14 12:34:58 +01:00
parent c89e5b4e6c
commit 4c260479dd
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -1,33 +1,63 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <vector>
#include <ctime> #include <algorithm>
#include <iomanip>
int main() using string = std::string;
{
using std::cout; //Placw in header!
using std::cin; template <typename T>
srand(time(nullptr)); void printm(T m){
int gcount{ 1 }; std::cout << m;
int n { 20 }; }
int secret { rand()%n + 1 };
cout << "Guess a number between 1 and " << n << ": "; template <typename T>
int guess; void printlm(T m){
cin >> guess; printm(m);
while (guess != secret) printm("\n");
{ }
if (guess > secret)
{ typedef std::vector<double>::size_type vtype;
cout << "Number too big ; ";
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 else
{ printlm((v.at(static_cast<vtype>(halfway - 1)) + v.at(static_cast<vtype>(halfway))) / 2.0);
cout << "Number too small ; "; double avg {};
} for (vtype i = 0; i < v.size(); ++i) avg += v.at(i);
gcount++; avg /= static_cast<int>(v.size());
cout << "Guess again: "; printm("The average is: ");
cin >> guess; printlm(avg);
}
cout << "Correct!\n";
cout << "Took " << gcount << " tries.\n";
return 0; 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;
}