Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #define NUM_SCORE 5
  3. #define MAX_SCORE 102
  4. #define MIN_SCORE 0
  5.  
  6. void getScores (int a[]);
  7. double average (int a[]);
  8. void bubbleSort (int a[]);
  9. void swap (int *p, int*q);
  10.  
  11. main()
  12. {
  13. int firstRun = 1, i;
  14. char repeat = 's';
  15. int a[NUM_SCORE];
  16.  
  17.  
  18. do
  19. {
  20. if (!firstRun)
  21. {
  22. getScores(a);
  23. }
  24. if (firstRun)
  25. {
  26. a[0] = 72;
  27. a[1] = 78;
  28. a[2] = 82;
  29. a[3] = 88;
  30. a[4] = 99;
  31. firstRun--;
  32. }
  33. printf(" Array: ");
  34. for ( i=0 ; i<NUM_SCORE ; i++)
  35. printf("%d ", a[i]);
  36. printf("\n Average: %.2lf\n", average(a));
  37. bubbleSort (a);
  38. printf(" Sorted: ");
  39. for ( i=0 ; i<NUM_SCORE ; i++)
  40. printf("%d ", a[i]);
  41. printf("\n Highest: %d", a[NUM_SCORE-1]);
  42.  
  43. printf("\nWant to calculate midterm scores?");
  44. repeat = getchar();
  45. getchar();
  46. }while(repeat != 'n' && repeat != 'N');
  47. }
  48. double average (int a[])
  49. {
  50. double average, sum = 0;
  51. int i;
  52. for ( i=0 ; i<NUM_SCORE ; ++i)
  53. sum = sum + a[i];
  54. average = (sum/NUM_SCORE);
  55.  
  56. return average;
  57. }
  58.  
  59. void bubbleSort (int a[])
  60. {
  61. int i, j;
  62. for(i = 0 ; i < NUM_SCORE-1 ; ++i)
  63. for(j = NUM_SCORE-1 ; i<j ; --j)
  64. if( a[j-1] > a[j])
  65. swap(&a[j-1], &a[j]);
  66. }
  67.  
  68. void swap (int *p, int*q)
  69. /* swap stores the addresses of two numbers passed as paramters to p and q
  70. then swaps the two values stored by the two addresses by storing the
  71. dereferenced value in a placer then assigning the value at p to the
  72. value at q. Then the value at q is changed to the value at p by assigning
  73. it to placer which was previously stored as the value at p. This effectively
  74. switches the values stored at the addresses.*/
  75. {
  76. double placer;
  77. placer = *p;
  78. *p = *q;
  79. *q = placer;
  80. return;
  81. }
  82.  
  83. void getScores (int a[])
  84. {
  85. int tmp, yes, i;
  86. do
  87. {
  88. yes = 1;
  89. printf("\nEnter 5 midterm scores (0-102):");
  90. for ( i=0 ; i<NUM_SCORE ; i++)
  91. {
  92. scanf("%d", tmp);
  93. if (tmp <= MIN_SCORE || tmp >= MAX_SCORE)
  94. {
  95. printf("\nERROR! Enter legal scores in the range (0-102)");
  96. yes--;
  97. }
  98. if (yes)
  99. {
  100. &a[i] = tmp;
  101. }
  102. }
  103. }while(!yes);
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement