title: Why is the average for my array of numbers not correct?
body: <p>I have written a method to take an array (via pointer) and its size to calculate the average. I am still relatively new to pointers. I have tried to remove the reference operator all throughout the code to the point at which it will compile, but the values returning are not intended. For instance, if I pass in an array size of 3, and then my array values are {1, 2, 3}, with my eyes closed I can tell you that that average should be 2. However my code returns 2.666666667. Thinking in reverse, I multiplied 2.6666...7 by 3, which equals 8. This leads me to think that I somewhere, a 2 is getting amended to the array, like {1,2,3,2}, however I'm unsure at this point. If anyone has any experience with pointers/simple arithmetic I'd appreciate your advice, because again, I am new to pointers and this idea of reference by address. Thanks!</p>
  
  <pre><code>double *Average(double *arr, int size)
      {
          double *avg;
          avg = (double *)malloc(sizeof(double));
  
          *avg = 0;
  
          int i;
          for (i = 0; i &lt; size; i++)
          {
              *avg = *avg + arr[i];
          }
  
          *avg = *avg / (double) size;
  
          printf("The average of the array: %f\n", *avg);
          return avg;
  }
  </code></pre>
  
  <p>How the method gets called from main:</p>
  
  <pre><code>else if (choice == 'C' || choice == 'c')
  {
      int count;
  
      printf("How many numbers do you want to use in the array?\n&gt; ");
      scanf("%d", &amp;count);
  
      double *arr;
      arr = (double *)malloc(sizeof(double) * count);
  
      int i;
      for (i = 0; i &lt; count; i++)
      {
  
          printf("Please enter a number (%d of %d)\n&gt; ", i + 1, count);
          scanf("%lf", &amp;arr[i]);
      }
  
      Min(arr, count);
  
      Max(arr, count);
  
      Average(arr, count);
  }
  </code></pre>
  
  <p>Tests:</p>
  
  <ul>
  <li>Average({1,3,5}, 3)
  
  <blockquote>
    <p>Expected: 3, Got: 4.333333</p>
  </blockquote></li>
  <li>Average({1,1,1}, 3)
  
  <blockquote>
    <p>Expected: 1, Got: 1</p>
  </blockquote></li>
  <li>Average({1,2,3,4,5,6,7,8,9}, 9)
  
  <blockquote>
    <p>Expected: 5, Got: 5.888889</p>
  </blockquote></li>
  </ul>
  
  <p>Two extra methods per user request:</p>
  
  <pre><code>double *Min(double *arr, int size)
  {
      double *min = &amp;arr[0];
      int i;
      for (int i = 1; i &lt; size; i++)
      {
          if (arr[i] &lt; *min)
          {
          *min = arr[i];
          }
      }
  
      printf("Smallest number in array: %f\n", *min);
      return min;
  }
  
  double *Max(double *arr, int size)
  {
      double *max = &amp;arr[0];
      int i;
      for (int i = 1; i &lt; size; i++)
      {
          if (arr[i] &gt; *max)
          {
              *max = arr[i];
          }
      }
  
      printf("Largest number in array: %f\n", *max);
      return max;
  }
  </code></pre>