fabbe680

Array, Search and Sort v1.0

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