Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std::cout<<"Welcome to Stats Array!\n"
  6. <<"These are your choices:\n"
  7. <<"N- Numbers\n"
  8. <<"S- Sum of all\n"
  9. <<"A- Average of all\n"
  10. <<"B- Biggest of all\n"
  11. <<"F- Most Frequent of all\n"
  12. <<"H- How many numbers\n"
  13. <<"M- Median of all\n"
  14. <<"Q- Quit\n";
  15.  
  16. bool running = true;
  17. int frequency[100] = {0};
  18.  
  19. // run until user specifies to stop
  20. while(running) {
  21. char choice;
  22. int numInput;
  23. std::cin>>choice;
  24. switch (choice) {
  25. // add input to frequency
  26. case 'N': {
  27. std::cin>>numInput;
  28. frequency[numInput] += 1;
  29. std::cout<<"OK"<<std::endl;
  30. break;
  31. }
  32. // sum of all numbers
  33. case 'S': {
  34. int sum = 0;
  35. for (int i = 0; i < 100; i++) {
  36. if (frequency[i] > 0) {
  37. sum += i * frequency[i];
  38. }
  39. }
  40. std::cout<<sum<<std::endl;
  41. break;
  42. }
  43. // average of all numbers
  44. case 'A': {
  45. int sum = 0;
  46. int num = 0;
  47. for (int i = 0; i < 100; i++) {
  48. if (frequency[i] > 0) {
  49. num += frequency[i];
  50. sum += i * frequency[i];
  51. }
  52. }
  53. if(num == 0) {
  54. std::cout<<"ERROR"<<std::endl;
  55. break;
  56. }
  57. double avg = sum/(double)num;
  58. std::cout<<avg<<std::endl;
  59. break;
  60. }
  61. // biggest of all numbers
  62. case 'B': {
  63. int biggest = 0;
  64. for (int i = 0; i < 100; i++) {
  65. if (frequency[i] > 0) {
  66. biggest = i;
  67. }
  68. }
  69. if (biggest == 0) {
  70. std::cout<<"ERROR"<<std::endl;
  71. break;
  72. }
  73. std::cout<<biggest<<std::endl;
  74. break;
  75. }
  76. // most frequent of all numbers
  77. case 'F': {
  78. int num = 0;
  79. for (int i = 0; i < 100; i++) {
  80. if (frequency[i] > frequency[num]) {
  81. num = i;
  82. }
  83. }
  84. if (frequency[num] == 0) {
  85. std::cout<<"ERROR"<<std::endl;
  86. break;
  87. }
  88. std::cout<<num<<std::endl;
  89. break;
  90. }
  91. // count of how many numbers
  92. case 'H': {
  93. int howMany = 0;
  94. for (int i = 0; i < 100; i++) {
  95. if (frequency[i] > 0) {
  96. howMany += frequency[i];
  97. }
  98. }
  99. std::cout<<howMany<<std::endl;
  100. break;
  101. }
  102. // median of all numbers
  103. case 'M': {
  104. int howMany = 0;
  105. for (int i = 0; i < 100; i++) {
  106. if (frequency[i] > 0) {
  107. howMany += frequency[i];
  108. }
  109. }
  110. if (howMany == 0) {
  111. std::cout<<"ERROR"<<std::endl;
  112. break;
  113. }
  114. int median = howMany / 2 + 1;
  115. std::cout<<median<<std::endl;
  116. break;
  117. }
  118. // quit the program
  119. case 'Q': {
  120. running = false;
  121. break;
  122. }
  123. // input error handling
  124. default: {
  125. std::cout<<"Bad input. Try capitalizing the letter."<<std::endl;
  126. break;
  127. }
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement