fork download
  1. // Marvyn Jesse De La Torre CS1A Chapter 9 #13 pg. 539
  2. /*****************************************************************
  3.  * ANALYZE MOVIE STATISTICS
  4.  * ___________________________________________________________________
  5.  * This program collects the number of movies college students
  6.  * watched in one month. It calculates the average, median, and
  7.  * mode of the numbers entered by the user.
  8.  *
  9.  * INPUT
  10.  * studentCount : Number of students surveyed (at least 1)
  11.  * movies : Number of movies watched by each student (0 or more)
  12.  *
  13.  * OUTPUT
  14.  * average : Average number of movies watched
  15.  * median : Middle value of the sorted numbers
  16.  * mode : Most frequent value, if one exists
  17.  *****************************************************************/
  18.  
  19.  
  20. #include <iomanip>
  21. #include <iostream>
  22. using namespace std;
  23.  
  24. int getStudentCount()
  25. {
  26. int count;
  27.  
  28. do
  29. {
  30. cout << "How many students were surveyed? ";
  31. cin >> count;
  32.  
  33. if (count <= 0)
  34. cout << "At least one student must be surveyed.\n";
  35. } while (count <= 0);
  36.  
  37. return count;
  38. }
  39.  
  40. int getMovieCount(int studentNumber)
  41. {
  42. int movies;
  43.  
  44. do
  45. {
  46. cout << "Movies seen by student " << studentNumber << ": ";
  47. cin >> movies;
  48.  
  49. if (movies < 0)
  50. cout << "The number of movies cannot be negative.\n";
  51. } while (movies < 0);
  52.  
  53. return movies;
  54. }
  55.  
  56. void sortArray(int* values, int size)
  57. {
  58. for (int startScan = 0; startScan < size - 1; ++startScan)
  59. {
  60. int minIndex = startScan;
  61.  
  62. for (int index = startScan + 1; index < size; ++index)
  63. {
  64. if (*(values + index) < *(values + minIndex))
  65. minIndex = index;
  66. }
  67.  
  68. int temporary = *(values + startScan);
  69. *(values + startScan) = *(values + minIndex);
  70. *(values + minIndex) = temporary;
  71. }
  72. }
  73.  
  74. double findAverage(const int* values, int size)
  75. {
  76. int total = 0;
  77.  
  78. for (int index = 0; index < size; ++index)
  79. total += *(values + index);
  80.  
  81. return static_cast<double>(total) / size;
  82. }
  83.  
  84. double findMedian(const int* sortedValues, int size)
  85. {
  86. int middle = size / 2;
  87.  
  88. if (size % 2 != 0)
  89. return *(sortedValues + middle);
  90.  
  91. return (*(sortedValues + middle - 1) + *(sortedValues + middle)) / 2.0;
  92. }
  93.  
  94. int findMode(const int* sortedValues, int size)
  95. {
  96. int mode = -1;
  97. int highestFrequency = 1;
  98. int currentFrequency = 1;
  99.  
  100. for (int index = 1; index < size; ++index)
  101. {
  102. if (*(sortedValues + index) == *(sortedValues + index - 1))
  103. {
  104. ++currentFrequency;
  105. }
  106. else
  107. {
  108. if (currentFrequency > highestFrequency)
  109. {
  110. highestFrequency = currentFrequency;
  111. mode = *(sortedValues + index - 1);
  112. }
  113.  
  114. currentFrequency = 1;
  115. }
  116. }
  117.  
  118. if (currentFrequency > highestFrequency)
  119. mode = *(sortedValues + size - 1);
  120.  
  121. return mode;
  122. }
  123.  
  124. int main()
  125. {
  126. const int studentCount = getStudentCount();
  127. int* movies = new int[studentCount];
  128.  
  129. for (int index = 0; index < studentCount; ++index)
  130. *(movies + index) = getMovieCount(index + 1);
  131.  
  132. sortArray(movies, studentCount);
  133.  
  134. cout << fixed << setprecision(2);
  135. cout << "\nMovie statistics\n";
  136. cout << "Average movies seen: "
  137. << findAverage(movies, studentCount) << endl;
  138. cout << "Median movies seen: "
  139. << findMedian(movies, studentCount) << endl;
  140.  
  141. int mode = findMode(movies, studentCount);
  142.  
  143. if (mode == -1)
  144. cout << "Mode: No mode\n";
  145. else
  146. cout << "Mode: " << mode << endl;
  147.  
  148. delete[] movies;
  149. movies = nullptr;
  150.  
  151. return 0;
  152. }
Success #stdin #stdout 0s 544KB
stdin
6
4
2
4
1
3
4
stdout
How many students were surveyed? Movies seen by student 1: Movies seen by student 2: Movies seen by student 3: Movies seen by student 4: Movies seen by student 5: Movies seen by student 6: 
Movie statistics
Average movies seen: 3.00
Median movies seen: 3.50
Mode: 4