Guest User

Untitled

a guest
Apr 11th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <chrono>
  5. #include <random>
  6. #include <iomanip>
  7. #include <fstream>
  8.  
  9. constexpr auto low_score = 4;
  10. constexpr auto high_score = 10;
  11. constexpr auto numJudges = 5;
  12.  
  13. double getScore(std::default_random_engine dre, std::uniform_int_distribution<int> di)
  14. {
  15. std::vector<int> scores(numJudges);//to record the scores
  16. std::generate(scores.begin(), scores.end(), [&]{ return di(dre);});//generates 5 random nos b/w 4 and 10
  17.  
  18. std::sort(scores.begin(), scores.end());//sort the scores generated
  19.  
  20. std::vector<int> truncatedScores {&scores[1], &scores[scores.size()-1]};
  21. //truncated vector removing the min and max scores
  22.  
  23. return static_cast<double>(std::accumulate(truncatedScores.begin(), truncatedScores.end(),0))/3;
  24. //average of the truncated scores
  25. }
  26.  
  27. int main()
  28. {
  29. auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
  30. std::default_random_engine dre(seed);//engine
  31. std::uniform_int_distribution<int> di(low_score,high_score);//distribution based on low and hi scores
  32.  
  33. // getScore(dre, di);
  34. std::ofstream fout{"D:\\test.txt"};//file to send scores to
  35.  
  36. std::cout << "How many performers? \n";
  37. size_t performers{};
  38. std::cin >> performers;
  39.  
  40. std::vector<double> scores;//vector to record individual scoreas
  41. for (size_t i = 0; i < performers; ++i)scores.push_back(std::move(getScore(dre, di)));
  42. // std::generate(scores.begin(), scores.end(), getScore);
  43. for (const auto& elem : scores)
  44. {
  45. std::cout << std::setprecision(3) << elem << "\n";//print to screen
  46. fout << std::setprecision(3) << elem << "\n";//print to file
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment