title: Recursively finding the average of even numbers in an array
body: <p>I'm trying to find the <strong>average of all even numbers</strong> in an array using <strong>recursion</strong> and I'm stuck.</p>
  
  <p>I realize that <code>n</code> will have to be <strong>decremented for each odd number</strong> so I divide by the correct value, but I can't wrap my mind around how to do so with recursion.</p>
  
  <p>I don't understand how to keep track of <code>n</code> as I go, considering it will just revert when I <code>return</code>.</p>
  
  <p>Is there a way I'm missing to keep track of <code>n</code>, or am I looking at this the wrong way entirely?</p>
  
  <p>EDIT: I should have specified, I need to use recursion specifically. It's an assignment. </p>
  
  <pre><code>public static int getEvenAverage(int[] A, int i, int n)
  {
      // first element
      if (i == 0)
          if (A[i] % 2 == 0)
              return A[0];
          else
              return 0;
  
      // last element
      if (i == n - 1)
      {
          if (A[i] % 2 == 0)
              return (A[i] + getEvenAverage(A, i - 1, n)) / n;
          else
              return (0 + getEvenAverage(A, i - 1, n)) / n;
      }
  
      if (A[i] % 2 == 0)
          return A[i] + getEvenAverage(A, i - 1, n);
      else
          return 0 + getEvenAverage(A, i - 1, n);
  }
  </code></pre>