How to sum up N array elements, to the left of the given element?
<p>Sorry if the question is confusing, I am new to programming and I didn't know how to formulate it, and even Google couldn't understand me number of times.</p>

<p>I am trying to solve some kind of Fibonnaci sequence, but instead of summing the last 2 numbers, it should sum up the last N numbers.</p>

<pre><code>public double[] sequenceSum(double[] sequence) {
//some fancy method here
}
</code></pre>

<p>Where the sequence.length is actually how much numbers you should sum up.
So if the call is:
seqeunceSum(1,2,3,4);</p>

<p>...the sequence should continue {1,2,3,4,10,18,35,67...}.</p>

<p>if the call is:
seqeunceSum(1,0,1,0,1);
...the sequence should continue {1,0,1,0,1,3,5,10,19,38....}.</p>

<p>The sequence has its limits, of course, otherwise it'd be infinite, but I only need help with <strong>how to sum up N number of array elements, to the left</strong></p>

<p>Thanks a lot,</p>

<blockquote>
  <p>EDIT:</p>
</blockquote>

<p>Thanks, I took few opinions from here and I solved it. </p>

<p>And yes, the sequence has limits, sorry for not including that in the parameters, I judged on previous advise to ask Minimal and Specific questions.</p>

<p>Here is my code:</p>

<pre><code>public double[] sequenceSum(double[] sequence, int n) {
      double[] xArray = Arrays.copyOf(sequence, n);
      for(int i = sequence.length; i&lt;n; i++){
      double sum = 0;
         for (int j=(i-sequence.length); j &lt; i; j++) {
            sum += xArray[j];
            }
         xArray[i] = sum;
      }
      return xArray;
  }
}
</code></pre>

<p>Thanks everyone,</p>