fabbe680

5.1B

Feb 7th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. // Laboration 5, Assignment_1B.cpp
  2. // Fabian Tjernström (fatj1700) 2018-12-14
  3.  
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.  
  14. const int aSize = 600;
  15. vector<int> a(aSize);
  16. int average = 0, numFreq = 0, maxFreq = 0, hit = 0;
  17. int mostFreq, searchNum, currentNum, max, min;
  18.  
  19. //Randomize Vector
  20.  
  21. srand(time(NULL));
  22.  
  23. for(auto &e: a) {
  24. e = rand() % 100 + 1;
  25. }
  26.  
  27. //Copy Vector
  28.  
  29. vector<int> aCopy(aSize);
  30. aCopy = a;
  31.  
  32. //Average
  33.  
  34. for(auto e: aCopy) {
  35. average += e;
  36. }
  37.  
  38. average = (average / aCopy.size());
  39. cout << "Average: ";
  40. cout << average << endl;
  41.  
  42. //Most Frequent Number
  43.  
  44. sort(aCopy.begin(), aCopy.end());
  45.  
  46. for(auto e: aCopy) {
  47. if(e > currentNum) {
  48. numFreq = 0;
  49. currentNum = e;
  50. }
  51. if(currentNum == e) {
  52. numFreq++;
  53. }
  54. if(numFreq > maxFreq) {
  55. maxFreq = numFreq;
  56. mostFreq = currentNum;
  57. }
  58. }
  59.  
  60. cout << "Most frequent number: ";
  61. cout << mostFreq << endl;
  62.  
  63. //Highest and Smallest Value
  64.  
  65. min = *(min_element(aCopy.begin(), aCopy.end()));
  66. max = *(max_element(aCopy.begin(), aCopy.end()));
  67.  
  68. cout << "Smallest Value: ";
  69. cout << min << endl;
  70. cout << "Highest Value: ";
  71. cout << max << endl;
  72.  
  73. //Number Search
  74.  
  75. cout << "--------" << endl;
  76. cout << "Enter a number between 1-100 to search for: ";
  77. cin >> searchNum;
  78.  
  79. find(aCopy.begin(), aCopy.end(),searchNum);
  80.  
  81. for(auto e: aCopy) {
  82. if(e == searchNum) {
  83. hit++;
  84. }
  85. }
  86.  
  87. cout << "Number of occurrences of '" << searchNum << "' are: ";
  88. cout << hit << endl;
  89.  
  90. // Print
  91.  
  92. char answer;
  93. cout << "--------" << endl;
  94.  
  95. while(answer != 'y' || answer != 'n') {
  96. cout << "Print list? [y/n]: ";
  97. cin >> answer;
  98. cout << "--------" << endl;
  99. if (answer == 'y') {
  100. for (auto e: aCopy) {
  101. cout << e << endl;
  102. }
  103. }
  104. if (answer == 'n') {
  105. cout << "Okay, goodbye!" << endl;
  106. return 0;
  107. }
  108. }
  109.  
  110. return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment