title: C function not returning value
body: <p>I've written a program for a class and am having a problem. The purpose of the program is to read a set of values into an array, calculate the average, and then find how many elements in that array are larger than that average. The function prototypes were provided so that cannot be changed. Also, we were instructed to initialize the array to size 10 and double the size whenever the number of read elements exceeds the current size, so that cannot be changed. <br /><br />
  The problem I'm running into is returning a value from the aboveaverage function. It works properly within itself (I can put a printf to display count before return, but in the main function, the value being returned is 0. Is there anyone that can help with this please? It's getting frustrating.
  <br /><br />
  Also, the commented printf line was to check the value being returned by the function. I commented it out instead of deleting it so I wouldn't have to retype it every time.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  double average(double *ptr, int size);
  int aboveaverage(double *ptr, int size, double average);
  
  int main(int argc, char* argv[])
  {
      double *ptr, avg, above, temp;
      int size = 10, i, j;
      void *tmp;
      FILE *fp;
  
      avg = above = 0;
  
      if (argc != 2)
      {
          printf("Invalid number of arguments, 2 required\n");
          return 1;
      }
  
      fp = fopen(argv[1], "r");
      ptr = (double *)calloc(10, sizeof(double));
      printf("Allocated 10 doubles\n");
  
      for (i = 0;fscanf(fp, "%lf", &amp;temp) != EOF; i++)
      {
          if (i &gt;= size - 1)
          {
              size*=2;    
              tmp = realloc(ptr, size);
              if (tmp == NULL)
              {
                  printf("Error with realloc, exiting\n");
                  return 1;
              }
              printf("Reallocated to %d doubles\n", size);
          }
          ptr[i] = temp;
          j = i;
      }
  
      size = j + 1;
      avg = average(ptr, size);
      above = aboveaverage(ptr, size, avg);
      //printf("%d\n", above);
  
      printf("%d elements are above average of %lf\n", above, avg);
  
      free(ptr);
  
      return 0; 
  }
  
  double average(double *ptr, int size)
  {
      double sum;
      int i;
      while (i &lt; size)
      {
          sum+=ptr[i];
          i++;
      }
  
      return (sum / size);
  }
  
  int aboveaverage(double *ptr, int size, double avg)
  {
      int count=0, temp;
      for (int i = 0; i &lt; size; i++)
      {
          temp = (int)ptr[i];
          if (temp &gt; avg)
          count++;
      }
      return count;
  }
  </code></pre>