title: C largest sum of 3 consecutive numbers from array
body: <p>I am learning C and my teacher gave me a task, but I'm stuck.
  In array there are 10 positive numbers and I need to find 3 consecutive numbers with the highest sum. I found this solution online, but how do I find 3 consecutive numbers and sum them? How should I approach this? Should I look for largest number in the array and go from there?
  The code I found did a better job of what I had done so far.</p>
  
  <p>The array has: {1, 2, 3, 2, 0, 5, 1, 6, 0, 1} then the largest sum of 3 numbers would be {1, 2, 3, 2, 0, <strong>5, 1, 6</strong>, 0, 1} as it gives 12</p>
  
  <pre><code>#define N_ELEMENTS 10
  
  int main(int argc, const char * argv[]) {
  
      int a[N_ELEMENTS] = {1, 2, 3, 2, 0, 5, 1, 6, 0, 1};
      int i = 0;
      while(a[i] &lt; 0 &amp;&amp; i&lt;N_ELEMENTS) {
          i++;
      }
      if (a[i] &lt; 0) {
          printf ("DEBUG: array with only negative numbers. Print the smallest negative number as the sum and we are done.\n");
      }
      int sum_p=0, sum_n = 0;
      int largest_sum = 0;
      while (i&lt;N_ELEMENTS) {
          if (a[i] &gt; 0) {
              sum_p += a[i];
          }
          else {
              sum_n += a[i];
          }
          if (sum_p+sum_n &gt; largest_sum) {
              largest_sum = sum_p + sum_n;
          }
          if (sum_p+sum_n &lt;= 0) {
              // find the next positive number
              while(a[i] &lt; 0 &amp;&amp; i&lt;N_ELEMENTS) {
                  i++;
              }
              if (a[i] &lt; 0 || i == N_ELEMENTS) {
                  break;
              }
              sum_p = 0;
              sum_n = 0;
          } else {
              i++;
          }
      }
      printf ("DEBUG: The largest consecutive sum = %d\n", largest_sum);
  }
  </code></pre>