w2 q1
This commit is contained in:
parent
c89e5b4e6c
commit
4c260479dd
90
main.cpp
90
main.cpp
@ -1,33 +1,63 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
|
||||
int main()
|
||||
{
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
srand(time(nullptr));
|
||||
int gcount{ 1 };
|
||||
int n { 20 };
|
||||
int secret { rand()%n + 1 };
|
||||
cout << "Guess a number between 1 and " << n << ": ";
|
||||
int guess;
|
||||
cin >> guess;
|
||||
while (guess != secret)
|
||||
{
|
||||
if (guess > secret)
|
||||
{
|
||||
cout << "Number too big ; ";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Number too small ; ";
|
||||
}
|
||||
gcount++;
|
||||
cout << "Guess again: ";
|
||||
cin >> guess;
|
||||
}
|
||||
cout << "Correct!\n";
|
||||
cout << "Took " << gcount << " tries.\n";
|
||||
return 0;
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user