fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. // Function prototype
  6. void getData(int *, int);
  7. void selectionSort(int *, int);
  8. double getAvg(int *, int);
  9. double getMedian(int *, int);
  10. int getMode(int *, int);
  11.  
  12. int main()
  13. {
  14. int *Movies, Stud, Mode;
  15. double Avg, Med;
  16.  
  17. // Ask user how many students were surveyed.
  18. cout << "How many students were surveyed? ";
  19. cin >> Stud;
  20.  
  21. Movies = new int[Stud];
  22.  
  23. getData(Movies, Stud);
  24.  
  25. selectionSort(Movies, Stud);
  26.  
  27. Avg = getAvg(Movies, Stud);
  28.  
  29. Med = getMedian(Movies, Stud);
  30.  
  31. Mode = getMode(Movies, Stud);
  32.  
  33. cout << "Statistical results for the number of movies\n"
  34. << " college students see in a month.\n";
  35. cout << fixed << showpoint << setprecision(2);
  36. cout << "Average: " << Avg << endl;
  37. cout << "Median: " << Med << endl;
  38. cout << "Mode: " << Mode << endl;
  39.  
  40. delete [] Movies;
  41. Movies = 0;
  42.  
  43. return 0;
  44. }
  45.  
  46. //*******************************************************************************
  47. // getData *
  48. // The function asks the user to enter the survey data and stores it in an *
  49. // integer array. The size parameter holds the size of the array. *
  50. //*******************************************************************************
  51. void getData(int *array, int size)
  52. {
  53. cout << "Enter the number of movies each student has seen in the month.\n";
  54. for (int i = 0; i < size; i++)
  55. {
  56. cout << "Student " << (i + 1) << ": ";
  57. cin >> *(array + i);
  58. }
  59. }
  60.  
  61. //*******************************************************************************
  62. // selectionSort *
  63. // This function performs an ascending-order selection sort on array. The *
  64. // parameter size holds the number of elements in the array. *
  65. //*******************************************************************************
  66. void selectionSort(int *array, int size)
  67. {
  68. int scan, minIndex, minValue;
  69.  
  70. for (int scan = 0; scan < (size - 1); scan++)
  71. {
  72. minIndex = scan;
  73. minValue = *(array + scan);
  74. for (int i = scan + 1; i < size; i++)
  75. {
  76. if (*(array + i) < minValue)
  77. {
  78. minValue = *(array + i);
  79. minIndex = i;
  80. }
  81. }
  82. *(array + minIndex) = *(array + scan);
  83. *(array + scan) = minValue;
  84. }
  85. }
  86.  
  87. //*******************************************************************************
  88. // getAvg *
  89. // This function calculates and returns the average of the values in array. The *
  90. // parameter size holds the number of elements in array. *
  91. //*******************************************************************************
  92. double getAvg(int *array, int size)
  93. {
  94. double Sum = 0;
  95. for (int i = 0; i < size; i++)
  96. {
  97. Sum += *(array +i);
  98. }
  99. return Sum / size;
  100. }
  101.  
  102. //*******************************************************************************
  103. // getMedian *
  104. // This function calculates and returns the median of the values in the array. *
  105. // The parameter size holds the number of elements in the array. *
  106. //*******************************************************************************
  107. double getMedian(int *array, int size)
  108. {
  109. int Mid = (size - 1) / 2;
  110. double Med;
  111.  
  112. if (size % 2 == 0)
  113. {
  114. Med = (*(array + Mid) + *(array + (Mid + 1))) / 2;
  115. }
  116. else
  117. Med = *(array + Mid);
  118.  
  119. return Med;
  120. }
  121.  
  122. //*******************************************************************************
  123. // getMode *
  124. // This function calculates and returns the mode of the values in the array. *
  125. // The parameter size holds the number of elements in the array. *
  126. //*******************************************************************************
  127. int getMode(int *array, int size)
  128. {
  129. int Mode, Most, Count;
  130. Count = Most = 0;
  131.  
  132. for (int i = 0; i < size; i++)
  133. {
  134. Count++;
  135. if (*(array + i) < *(array + i + 1))
  136. {
  137. if (Count > Most)
  138. {
  139. Mode = *(array + i);
  140. Most = Count;
  141. }
  142. Count = 0;
  143. }
  144. }
  145. return Mode;
  146. }
  147.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
How many students were surveyed? Enter the number of movies each student has seen in the month.
Statistical results for the number of movies
     college students see in a month.
Average: -nan
Median:  0.00
Mode:    -610189280