Advertisement
Guest User

ou2

a guest
Oct 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. /*
  2. * Programmeringsteknik med C och Matlab
  3. * Fall 18
  4. * Assignment 2
  5.  
  6. * File: ou2.c
  7. * Description: The program reads in the number of judges and the score from each judge.
  8. Then it calculates the average score without regard to the lowest and
  9. highest judge score. Finally it prints the results (the highest, the
  10. lowest and the final average score).
  11. * Author:
  12. * CS username:
  13. * Date: 1810XX
  14. * Input:
  15. * Output:
  16. * Limitations:
  17.  
  18. */
  19.  
  20. #include <stdio.h>
  21.  
  22. /* Declaration of functions. */
  23. void printInformation(void);
  24. int enterNumberOfJudges(void);
  25. double enterScore(int nrOfJudges, double array[]);
  26. void printScore(int nrOfJudges, double array[]);
  27. double calculateResult(int nrOfJudges, double array[], double *max, double *min, double *average);
  28. double printResult(double *max, double *min, double *average);
  29.  
  30.  
  31. /* Function: main
  32. * Description:
  33. * Input:
  34. * Output:
  35. */
  36.  
  37. int main(void) {
  38.  
  39. int nrOfJudges = 0;
  40. double arr[nrOfJudges];
  41. double max;
  42. double min;
  43. double average;
  44.  
  45. printInformation();
  46.  
  47. nrOfJudges = enterNumberOfJudges();
  48.  
  49. printf("\n");
  50.  
  51. enterScore(nrOfJudges, arr);
  52.  
  53. printf("\n");
  54.  
  55. printScore(nrOfJudges, arr);
  56.  
  57. calculateResult(nrOfJudges, arr, &max, &min, &average);
  58.  
  59. printf("\n");
  60.  
  61. printResult(&max, &min, &average);
  62.  
  63. return 0;
  64. }
  65.  
  66. /* Function: printInformation.
  67. * Description: Prints information at start of the program.
  68. * Input: Non.
  69. * Output: Information on the screen.
  70. */
  71.  
  72. void printInformation(void) {
  73. printf("Program information\n");
  74. printf("The program reads in the number of judges and the score from each judge.\n");
  75. printf("Then it calculates the average score without regard to the lowest and\n");
  76. printf("highest judge score. Finally it prints the results (the highest, the\n");
  77. printf("lowest and the final average score).\n\n");
  78. }
  79.  
  80. /* Function: enterNumberOfJudges.
  81. * Description: Lets the user enter the amount of judges.
  82. * Input: Non.
  83. * Output: Number of judges.
  84. */
  85.  
  86. int enterNumberOfJudges(void) {
  87.  
  88. int nrOfJudges;
  89.  
  90. do {
  91. printf("Number of judges (min 3 and max 10 judges)? ");
  92. scanf("%d", &nrOfJudges);
  93.  
  94. } while (nrOfJudges < 3 || nrOfJudges > 10);
  95.  
  96. return nrOfJudges;
  97. }
  98.  
  99. /* Function: enterScore.
  100. * Description: Lets the user enter score for each judge.
  101. * Input: The array and it's size.
  102. * Output: Elements for the array.
  103. */
  104.  
  105. double enterScore(int nrOfJudges, double array[]) {
  106.  
  107. for (int i = 0; i < nrOfJudges; i++) {
  108. printf("Score from judge %d? ", i+1);
  109. scanf("%lf", &array[i]);
  110. }
  111.  
  112. return 0;
  113. }
  114.  
  115. /* Function: printScore.
  116. * Description: Prints scores entered previously for each judge.
  117. * Input: The array and it's size.
  118. * Output: Scores entered for each judge.
  119. */
  120.  
  121. /* Fungerar ej för värde 6 eller större */
  122. void printScore(int nrOfJudges, double array[]) {
  123.  
  124. printf("Loaded scores:\n");
  125.  
  126. for (int i = 0; i < nrOfJudges; i++) {
  127. printf("Judge %d: %.1f\n", i+1, array[i]);
  128. }
  129. }
  130.  
  131. /* Function: calculateResult.
  132. * Description: Calculates highest score, lowest score and average score excluding highest and lowest score.
  133. * Input: The array and it's size.
  134. * Output: Higest value, lowest value and average value.
  135. */
  136.  
  137. double calculateResult(int nrOfJudges, double array[], double *max, double *min, double *average) {
  138.  
  139. const int n = nrOfJudges;
  140.  
  141. *max = array[0];
  142. *min = array[0];
  143. double sum = 0;
  144.  
  145. for (int i = 0; i < n; i++) {
  146. if (array[i] > *max) {
  147. *max = array[i];
  148. }
  149. if (array[i] < *min) {
  150. *min = array[i];
  151. }
  152. }
  153.  
  154. for (int i = 0; i < n; i++) {
  155. if (array[i] > *min && array[i] < *max) {
  156. sum += array[i];
  157. }
  158. }
  159.  
  160. *average = sum / (n - 2);
  161.  
  162. return *max;
  163. return *min;
  164. return *average;
  165. }
  166.  
  167. /* Function: printResult.
  168. * Description: Prints results and ends program.
  169. * Input: Max value, min value and average value.
  170. * Output: Max value, min value and average value.
  171. */
  172.  
  173. double printResult(double *max, double *min, double *average) {
  174.  
  175. printf("Final result:\n");
  176. printf("Highest judge score: %.1f\n", *max);
  177. printf("Lowest judge score: %.1f\n", *min);
  178. printf("Final average score: %.1f\n", *average);
  179.  
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement