title: how to write a function that smooths a given array
body: <p>I am writing a function which smooths the given n element array with signature <code>void smoothData(int n, double data[])</code> according to the <code>1:2:1</code> filter given below. I should not generate entire new array and copy it back to original array.</p>
  
  <pre><code>si=(d(i−1)+2*di+d(i+1))/4
  </code></pre>
  
  <p>First and last element of smoothed array should be got from following rule instead of using <code>si</code></p>
  
  <pre><code>s0=(3*d0+d1)/4
  
  s(n−1)=(d(n−2)+3*d(n−1))/4
  </code></pre>
  
  <p>where <code>si</code> is the <code>i-th</code> element in smoothed array, <code>d</code> is the input array.
  <code>d(n-1)</code> is the <code>(n-1)th</code> element of original array etc.</p>
  
  <p>I got output of <code>1.250, 2.250, 4.000, 4.000</code>. The third element is wrong, which should be <code>3.500</code>. </p>
  
  <p><a href="https://i.stack.imgur.com/VkJqb.png" rel="nofollow noreferrer">https://i.stack.imgur.com/VkJqb.png</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void smoothData(int n, double data[])
  {
  
      double s0 = (data[0] * 3 + data[1]) / 4.0;
      double sLast = (data[n-2] + data[n-1] * 3) / 4.0;
      double si = 0.0;
      double di = data[0];
      for (int i = 0; i&lt;=n-4; i++) {  
          si = (di + 2 * data[i+1] + data[i+2])/4.0;
          di = data[i+1];
          data[i+1] = si;            
      }
      data[0] = s0;
      data[n-1] = sLast;    
  }
  
  int main()
  {
  
      double data[] = {1.0, 2.0, 4.0, 4.0};
      smoothData(4, data);
      printf("Smoothed data: %.3lf", data[0]);
      for (int i = 1; i &lt; 4; i++) {
          printf(", %.3lf", data[i]);
      }
      puts("");
  }
  </code></pre>