fork download
  1. #include <stdio.h>
  2. #define SIZE 99
  3.  
  4. void mean(const int answer[]);
  5. void median(int answer[]);
  6. void mode(int freq[], const int answer[]);
  7. void bubbleSort(int a[]);
  8. void printArray(const int a[]);
  9.  
  10. int main(void) {
  11. int frequency[10] = {0};
  12. int response[SIZE] = {
  13. 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
  14. 7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
  15. 6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
  16. 7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
  17. 6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
  18. 7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
  19. 5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
  20. 7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
  21. 7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
  22. 4, 5, 6, 1, 6, 5, 7, 8, 7
  23. };
  24.  
  25. mean(response);
  26. median(response);
  27. mode(frequency, response);
  28.  
  29. return 0;
  30. }
  31.  
  32. void mean(const int answer[]) {
  33. int total = 0;
  34. for (size_t j = 0; j < SIZE; ++j) {
  35. total += answer[j];
  36. }
  37. printf("Mean: %.4f\n", (double) total / SIZE);
  38. }
  39.  
  40. void median(int answer[]) {
  41. bubbleSort(answer);
  42. if (SIZE % 2 == 0) {
  43. double med = (answer[SIZE / 2 - 1] + answer[SIZE / 2]) / 2.0;
  44. printf("Median: %.1f\n", med);
  45. } else {
  46. printf("Median: %d\n", answer[SIZE / 2]);
  47. }
  48. }
  49.  
  50. void mode(int freq[], const int answer[]) {
  51. for (size_t rating = 1; rating <= 9; ++rating) {
  52. freq[rating] = 0;
  53. }
  54. for (size_t j = 0; j < SIZE; ++j) {
  55. ++freq[answer[j]];
  56. }
  57.  
  58. int largest = 0;
  59. for (size_t rating = 1; rating <= 9; ++rating) {
  60. if (freq[rating] > largest) {
  61. largest = freq[rating];
  62. }
  63. }
  64.  
  65. printf("Mode(s) appearing %d times: ", largest);
  66. for (size_t rating = 1; rating <= 9; ++rating) {
  67. if (freq[rating] == largest) {
  68. printf("%zu ", rating);
  69. }
  70. }
  71. printf("\n");
  72. }
  73.  
  74. void bubbleSort(int a[]) {
  75. for (int pass = 1; pass < SIZE; ++pass) {
  76. for (size_t j = 0; j < SIZE - 1; ++j) {
  77. if (a[j] > a[j + 1]) {
  78. int hold = a[j];
  79. a[j] = a[j + 1];
  80. a[j + 1] = hold;
  81. }
  82. }
  83. }
  84. }
  85.  
  86. void printArray(const int a[]) {
  87. for (size_t j = 0; j < SIZE; ++j) {
  88. if (j % 20 == 0) {
  89. printf("\n");
  90. }
  91. printf("%2d", a[j]);
  92. }
  93. printf("\n");
  94. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Mean: 6.8788
Median: 7
Mode(s) appearing 27 times: 8