fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define NUM 5 /* number of elements in theArray (can be any number of elements) */
  4.  
  5. /* function prototypes */
  6. int array_sum(int theArray[]);
  7. float array_avg(int theArray[]);
  8. int array_min(int theArray[]);
  9.  
  10. int main()
  11. {
  12. int theArray[NUM] = {-2,11,3,5,9}; /* array to process */
  13. int sum; /* sum of the elements */
  14. int avg; /* average of the elements */
  15. int min; /* minimum element in the array */
  16.  
  17.  
  18.  
  19. sum = array_sum(theArray);
  20. avg = array_avg(theArray);
  21. min = array_min(theArray);
  22.  
  23. printf("The sum is %i\n", sum);
  24. printf("The average is %d\n", avg);
  25. printf("The minimum is %i\n", min);
  26.  
  27. return(0);
  28. }
  29.  
  30. /****************************************************************/
  31. /* Function: array_sum */
  32. /* */
  33. /* Purpose: Finds the sum of all of the elements in theArray. */
  34. /* */
  35. /* Parameters: sum - sum of the elements */
  36. /* */
  37. /* Returns: sum - the sum of all the elements */
  38. /****************************************************************/
  39.  
  40. int array_sum(int theArray[])
  41. {
  42. int count;
  43. int sum;
  44.  
  45. sum=0;
  46.  
  47. for (count=0; count < NUM; ++count)
  48. {
  49. sum = sum + theArray[count];
  50. }
  51. return(sum);
  52. }
  53.  
  54.  
  55. /****************************************************************/
  56. /* Function: array_avg */
  57. /* */
  58. /* Purpose: Finds the avg of all of the elements in theArray. */
  59. /* */
  60. /* Parameters: sum - sum of the elements */
  61. /* avg - average of the elements */
  62. /* Returns: avg-the average of all of the elements */
  63. /****************************************************************/
  64.  
  65.  
  66. float array_avg(int theArray[])
  67. {
  68. int count;
  69. int sum;
  70. float avg;
  71.  
  72. sum=0;
  73.  
  74. for (count=0; count < NUM; ++count)
  75. {
  76. sum = sum + theArray[count];
  77. avg = sum/NUM;
  78. }
  79. return(avg);
  80. }
  81.  
  82.  
  83.  
  84. /****************************************************************/
  85. /* Function: array_min */
  86. /* */
  87. /* Purpose: Finds the minimum element in theArray. */
  88. /* */
  89. /* Parameters: min- the minimum element */
  90. /* */
  91. /* Returns: The minimum element in theArray */
  92. /****************************************************************/
  93. int array_min(int theArray[])
  94. {
  95. int count;
  96. int min;
  97. min = theArray[0];
  98.  
  99. for(count = 1; count < NUM; ++count)
  100. {
  101. if(min > theArray[count])
  102. min = theArray[count];
  103. }
  104. return(min);
  105. }
  106.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
The sum is 26
The average is 5
The minimum is -2