// mySum2.cc - Example file for creating a sum of elements of an array
// Michael Haggerty for CISC105 sec 99

#include <iostream>

using namespace std;

void printArray(int list[], int size);
int addElements(int list[], int lower, int size);

int main()
{
   int A[] = {2, 4, 1, 7};
   int result;
   
   result = addElements(A, 0, 4);

   printArray(A, 4);
   cout << " = " << result << endl;
   
   return 0;
}

int addElements(int list[], int lower, int size)
{
   int result = 0;
   
   if (size == 0)  // return 0 if the array has no elements
      return result;
   // if the lenght of the array we are checking is 1 then return the value
   else if (lower == size-1) 
      return list[lower];
   else
      result = list[lower] + addElements(list, lower + 1, size);

   return result;
}

void printArray(int list[], int size)
{
   for(int i = 0; i < size - 1; i++)
      cout << list[i] << " + ";
   
   cout << list[size-1];
}