fabbe680

5.1A

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