Compare commits
4 Commits
c89e5b4e6c
...
f8c0961b3f
Author | SHA1 | Date | |
---|---|---|---|
f8c0961b3f | |||
5ca32463d7 | |||
453902b44e | |||
4c260479dd |
@ -33,6 +33,8 @@
|
|||||||
<Add option="-fexceptions" />
|
<Add option="-fexceptions" />
|
||||||
</Compiler>
|
</Compiler>
|
||||||
<Unit filename="main.cpp" />
|
<Unit filename="main.cpp" />
|
||||||
|
<Unit filename="stats.cpp" />
|
||||||
|
<Unit filename="stats.h" />
|
||||||
<Extensions />
|
<Extensions />
|
||||||
</Project>
|
</Project>
|
||||||
</CodeBlocks_project_file>
|
</CodeBlocks_project_file>
|
||||||
|
80
main.cpp
80
main.cpp
@ -1,33 +1,63 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstdlib>
|
#include <vector>
|
||||||
#include <ctime>
|
#include <iomanip>
|
||||||
|
#include "stats.h"
|
||||||
|
|
||||||
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
using std::cout;
|
std::setprecision(3);
|
||||||
using std::cin;
|
printlm("Calculate the average and median of scores:");
|
||||||
srand(time(nullptr));
|
bool s {};
|
||||||
int gcount{ 1 };
|
std::vector<double> v {};
|
||||||
int n { 20 };
|
do
|
||||||
int secret { rand()%n + 1 };
|
|
||||||
cout << "Guess a number between 1 and " << n << ": ";
|
|
||||||
int guess;
|
|
||||||
cin >> guess;
|
|
||||||
while (guess != secret)
|
|
||||||
{
|
{
|
||||||
if (guess > secret)
|
auto d = promptd("Enter a score (Enter a non number to finish entry) : ",&s);
|
||||||
{
|
if (s) v.push_back(d);
|
||||||
cout << "Number too big ; ";
|
|
||||||
}
|
}
|
||||||
else
|
while(s);
|
||||||
{
|
printm("Score median is: ");
|
||||||
cout << "Number too small ; ";
|
printlm(med(v, false));
|
||||||
}
|
printm("Score median (No Extremes) is: ");
|
||||||
gcount++;
|
printlm(med(v, true));
|
||||||
cout << "Guess again: ";
|
printm("Score average is: ");
|
||||||
cin >> guess;
|
printlm(avg(v, false));
|
||||||
}
|
printm("Score average (No Extremes) is: ");
|
||||||
cout << "Correct!\n";
|
printlm(avg(v, true));
|
||||||
cout << "Took " << gcount << " tries.\n";
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double promptd(string message, bool *status)
|
||||||
|
{
|
||||||
|
double tr {0.0};
|
||||||
|
*status = false;
|
||||||
|
std::cout << message;
|
||||||
|
if (!(std::cin >> tr))
|
||||||
|
{
|
||||||
|
std::cin.clear();
|
||||||
|
std::cin.ignore();
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
*status = true;
|
||||||
|
return tr;
|
||||||
|
}
|
||||||
|
|
||||||
|
21
stats.cpp
Normal file
21
stats.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "stats.h"
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
double med(std::vector<double> &scores,bool ignoreExtremes) {
|
||||||
|
const auto len = scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0;
|
||||||
|
sort(scores.begin(), scores.end());
|
||||||
|
using vec_size = std::vector<double>::size_type;
|
||||||
|
const vec_size middle = (vec_size) (len / 2);
|
||||||
|
if (middle%2 == 1)
|
||||||
|
return scores[middle];
|
||||||
|
else
|
||||||
|
return (scores[middle - 1] + scores[middle]) / 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double avg(const std::vector<double> &scores,bool ignoreExtremes) {
|
||||||
|
auto sum = 0.0;
|
||||||
|
using vec_size = std::vector<double>::size_type;
|
||||||
|
for (vec_size i = (ignoreExtremes && scores.size() > 2) ? 1 : 0; i < scores.size() - (ignoreExtremes && scores.size() > 2) ? 1 : 0; i++) sum += scores[i];
|
||||||
|
return sum / static_cast<double>(scores.size() - (ignoreExtremes && scores.size() > 2) ? 2 : 0);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user