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,10,4,6,8} /* 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, NUM);
  20. avg = array_avg(theArray, NUM);
  21. min = array_min(theArray, NUM);
  22.  
  23. printf("The sum is %i\n", sum);
  24. printf("The average is %d\n", avg);
  25. printf("The minimum is %d\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. sum=0;
  44.  
  45. for (count=0; count < NUM; ++count)
  46. {
  47. sum = sum + theArray[count];
  48. }
  49. return(sum);
  50.  
  51.  
  52. /****************************************************************/
  53. /* Function: array_avg */
  54. /* */
  55. /* Purpose: Finds the avg of all of the elements in theArray. */
  56. /* */
  57. /* Parameters: sum - sum of the elements */
  58. /* avg - average of the elements */
  59. /* Returns: avg-the average of all of the elements */
  60. /****************************************************************/
  61.  
  62.  
  63. float array_avg(int theArray[])
  64. {
  65. int count;
  66. sum=0;
  67. float avg;
  68.  
  69. for (count=0; count < NUM; ++count)
  70. {
  71. sum = sum + theArray[count];
  72. avg = (sum + theArray[count])/NUM;
  73. }
  74. return(avg);
  75.  
  76.  
  77.  
  78.  
  79. /****************************************************************/
  80. /* Function: array_min */
  81. /* */
  82. /* Purpose: Finds the minimum element in theArray. */
  83. /* */
  84. /* Parameters: min- the minimum element */
  85. /* */
  86. /* Returns: The minimum element in theArray */
  87. /****************************************************************/
  88. int array_min(int theArray[])
  89. {
  90. int count;
  91. min = theArray[0];
  92.  
  93. for(count = 1; count < NUM; ++count)
  94. {
  95. if(min > theArray[count])
  96. min = array[count];
  97. }
  98. return(min);
  99. }
  100.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:13:2: error: expected ‘,’ or ‘;’ before ‘int’
  int sum;    /* sum of the elements */
  ^~~
prog.c:19:2: error: ‘sum’ undeclared (first use in this function)
  sum = array_sum(theArray, NUM);
  ^~~
prog.c:19:2: note: each undeclared identifier is reported only once for each function it appears in
prog.c:19:8: error: too many arguments to function ‘array_sum’
  sum = array_sum(theArray, NUM);
        ^~~~~~~~~
prog.c:6:5: note: declared here
 int array_sum(int theArray[]);
     ^~~~~~~~~
prog.c:20:8: error: too many arguments to function ‘array_avg’
  avg = array_avg(theArray, NUM);
        ^~~~~~~~~
prog.c:7:7: note: declared here
 float array_avg(int theArray[]);
       ^~~~~~~~~
prog.c:21:8: error: too many arguments to function ‘array_min’
  min = array_min(theArray, NUM);
        ^~~~~~~~~
prog.c:8:5: note: declared here
 int array_min(int theArray[]);
     ^~~~~~~~~
prog.c: In function ‘array_sum’:
prog.c:43:2: error: ‘sum’ undeclared (first use in this function)
  sum=0;
  ^~~
prog.c: In function ‘array_min’:
prog.c:91:2: error: ‘min’ undeclared (first use in this function)
  min = theArray[0];
  ^~~
prog.c:96:8: error: ‘array’ undeclared (first use in this function)
  min = array[count];
        ^~~~~
prog.c: In function ‘array_avg’:
prog.c:99:1: error: expected declaration or statement at end of input
 }
 ^
prog.c: In function ‘array_sum’:
prog.c:99:1: error: expected declaration or statement at end of input
prog.c:99:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
At top level:
prog.c:63:7: warning: ‘array_avg’ defined but not used [-Wunused-function]
 float array_avg(int theArray[])
       ^~~~~~~~~
prog.c:88:5: warning: ‘array_min’ defined but not used [-Wunused-function]
 int array_min(int theArray[])
     ^~~~~~~~~
stdout
Standard output is empty