// Function to sum the elements of an integer array

#include <stdio.h>

int  arraySum (int  array[], const int  n);
int  arraySum2 (int  *array, const int  n);
int main (void)
{
     
     int  values[10] = { 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 };
     printf ("The sum is %i\n", arraySum (values, 10));

     printf ("The sum is %i\n", arraySum2 (values, 10));
     return 0;
}


int  arraySum (int  array[], const int  n)
{
     int  sum = 0, *ptr;
     /*What does array + n mean?  Remember array is basically a pointer
      *that is pointing to the beginning of the array.  The variable n
      *represents the number of elements in the array.  So array + n is
      *the end of the array.  So why do you think we created the variable
      *arrayEnd?  Array + n will not change when the program is ran this 
      *is calculated.  Since it does not change why recalculate it each
      *time the for loop executes.  This called optimization.    */
     int  * const arrayEnd = array + n;

     for ( ptr = array;  ptr < arrayEnd;  ++ptr )
          sum += *ptr;

     return sum;
}


int  arraySum2 (int  *array, const int  n)
{
     int  sum = 0;
     int  * const arrayEnd = array + n;

     for (  ; array < arrayEnd;  ++array )
          sum += *array;

     return sum;
}