Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int main() {
  7. vector<int> v;
  8. int userIn = 0;
  9.  
  10. cout << "Enter integers and enter a letter when done." << endl;
  11. // get input from user
  12. // cin >> userIn will evaluate to false to exit loop
  13. while (cin >> userIn) {
  14. v.push_back(userIn);
  15. }
  16.  
  17. sort(v.begin(), v.end()); // Sort
  18.  
  19. // set min to first element in list since list is sorted
  20. int minVal = v.at(0);
  21. // set max to last element in list since list is sorted (note the -1)
  22. int maxVal = v.at(v.size() - 1);
  23. // since the loop starts looking at index 1,
  24. // initialize sum to the first element in list
  25. int sum = v.at(0);
  26.  
  27. double median;
  28. // don't forget this is integer division
  29. if (v.size() % 2 == 0) { // even
  30. median = (v.at(v.size() / 2 - 1) + v.at(v.size() / 2)) / 2.0;
  31. // ^index of first ^index of second
  32. }
  33. else { // odd
  34. median = v.at(v.size() / 2);
  35. }
  36.  
  37. /*
  38. To calculate the mode we'll track two things
  39. 1. the current candidate for mode (curMode/curCount)
  40. 2. the current winner for mode (mode/modeCount)
  41. */
  42. int curMode = v.at(0);
  43. int curCount = 1;
  44.  
  45. int mode = v.at(0);
  46. int modeCount = 1;
  47.  
  48.  
  49. for (int i = 1; i < v.size(); ++i) {
  50. /*
  51. -- If we weren't doing the sort to find median and mode,
  52. this code would be better, but since we already had
  53. to sort, we'll take advantage of the sorted list.
  54. if (v.at(i) < minVal) {
  55. minVal = v.at(i);
  56. }
  57. if (v.at(i) > maxVal) {
  58. maxVal = v.at(i);
  59. }
  60. */
  61. sum += v.at(i); // Increase sum by amount of current value
  62. if (curMode == v.at(i)) { // value is same as current mode candidate
  63. curCount++; // increment the count by 1
  64. }
  65. else { // value is a new candidate for mode
  66. if (curCount > modeCount) { // current candidate has a larger count
  67. mode = curMode; // set current candidate to mode winner
  68. modeCount = curCount; // set candidate count to mode count
  69. }
  70. // regardless of a new winner,
  71. // new candidate
  72. curMode = v.at(i); // set current value to be the new candidate for mode
  73. curCount = 1; // set candidate count to 1 for this occurrence
  74. }
  75. }
  76. // We still have a candidate that we haven't checked to see
  77. // if it is the new winner
  78. if (curCount > modeCount) { // current candidate has a larger count
  79. mode = curMode; // set current candidate to mode winner
  80. modeCount = curCount; // set candidate count to mode count
  81. }
  82.  
  83. // Output results
  84. cout << "Your numbers are: ";
  85. for (int i = 0; i < v.size(); ++i) {
  86. cout << v.at(i) << " ";
  87. }
  88. cout << endl;
  89. cout << "Max: " << maxVal << endl;
  90. cout << "Min: " << minVal << endl;
  91. cout << "Average: " << (static_cast<double>(sum) / v.size()) << endl;
  92. cout << "Median: " << median << endl;
  93. cout << "Mode: " << mode << "(" << modeCount << ")" << endl;
  94.  
  95. system("pause");
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement