[Question]
  [
I've been scrolling around this site for a while, but just recently got really interested in actually trying out some of the challenges. I was intending to try my hand at some of the existing code-golf topics, but I didn't have Internet access for a while yesterday, and in the meantime, I thought up my own challenge.
Your task is to create a program or function that takes an array of Floats `a` and an integer `n`, then sets each value in `a` to the average of the two beside it, `n` times. When repeatedly used with increasing values of `n`, this generates a wave-like motion:
![wave motion](https://www.dropbox.com/s/9n6kvqbko0i445v/WaveTest.gif?raw=1)
**Specifics:**
* If there happens to be only one item in `a`, or if `n` is 0 or less, the program should return the original array.
* Inputs and outputs can be in any format you desire, as long as they are visibly separated.
**For each step:**
* The first item in `a` should become the average of itself and the next item.
* The last item in `a` should become the average of itself and the previous item.
* Any other item in `a` should become the average of the previous item and the next item.
* Make sure you are calculating off the previous step's array and not the current one!
Test cases: **NOTE: Your inputs/outputs do not have to be in this format!**
```
[0, 0, 1, 0, 0], 1          -> [0, 0.5, 0, 0.5, 0]
[0, 0, 1, 0, 0], 2          -> [0.25, 0, 0.5, 0, 0.25]
[0, 0, 1, 0, 0], 0          -> [0, 0, 1, 0, 0]
[0, 0, 1, 0, 0], -39        -> [0, 0, 1, 0, 0]
[0, 16, 32, 16, 0], 1       -> [8, 16, 16, 16, 8]
[0, 1, 2, 3, 4, 5], 1       -> [0.5, 1, 2, 3, 4, 4.5]
[0, 64], 1                  -> [32, 32]
[0], 482                    -> [0]
[32, 32, 32, 16, 64, 16, 32, 32, 32], 4 -> [33, 27, 40, 22, 44, 22, 40, 27, 33]
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answer in bytes wins. The winner will be chosen in one week (on August 1). Good luck!
**Edit:** Congrats to the winner, [@issacg](https://codegolf.stackexchange.com/a/53801/42545), with a whopping **18 bytes!**
  
[Answer]
# Pyth, ~~46~~ 18 bytes
```
ucR2sV+hGGt+GeG.*Q
```
This code expects input in the form `iterations, [wave1, wave2, wave3 ...]`, as seen at the first link below.
[Demonstration.](https://pyth.herokuapp.com/?code=ucR2sV%2BhGGt%2BGeG.*Q&input=4%2C+%5B32%2C+32%2C+32%2C+16%2C+64%2C+16%2C+32%2C+32%2C+32%5D&debug=0) [Test Harness.](https://pyth.herokuapp.com/?code=FQ%2B%5DQ.QucR2sV%2BhGGt%2BGeG.*Q&input=1%2C+%5B0%2C+0%2C+1%2C+0%2C+0%5D%0A2%2C+%5B0%2C+0%2C+1%2C+0%2C+0%5D%0A0%2C+%5B0%2C+0%2C+1%2C+0%2C+0%5D%0A-39%2C+%5B0%2C+0%2C+1%2C+0%2C+0%5D%0A1%2C+%5B0%2C+16%2C+32%2C+16%2C+0%5D%0A1%2C+%5B0%2C+1%2C+2%2C+3%2C+4%2C+5%5D%0A1%2C+%5B0%2C+64%5D%0A482%2C+%5B0%5D%0A4%2C+%5B32%2C+32%2C+32%2C+16%2C+64%2C+16%2C+32%2C+32%2C+32%5D&debug=0)
The program works by applying the code within the reduce (`u`) function to the input list, as many times as the number of iterations.
I will demonstrate the wave propagation function on the list `[0, 1, 2, 3, 4, 5]`, which is in `G`:
`+hGG` prepends `G`'s first element to `G`, forming `[0, 0, 1, 2, 3, 4, 5]`.
`t+GeG` appends `G`'s last element to `G` and removes its first element, forming `[1, 2, 3, 4, 5, 5]`.
`sV` first forms pairs from the lists, giving `[[0, 1], [0, 2], [1, 3], [2, 4], [3, 5], [4, 5]]` with the final element of the first list truncated away. Then, the pairs are summed via the `s` function, giving `[1, 2, 4, 6, 8, 9]`.
`cR2` uses floating point division to divide all of the numbers by 2, giving the desired result, `[0.5, 1.0, 2.0, 3.0, 4.0, 4.5]`.
[Answer]
# [Snowman 1.0.0](https://github.com/KeyboardFire/snowman-lang/releases/tag/v1.0.0-beta), 219 chars
```
{vg" "aS:10sB;aM0aa,AAg**-:|al|'NdE'0nRal@(%}{->:1*?{0AaG;:?{;bI:dUNiNwR'NdEwRaC;aM(~:?{(()1wR]0wRaC*))#'wRaC|*#|(()#aLNdEdUNdEwR]wR]aCwR*))#aC;:0wRdUaCwR*?{#aC;#bI:*#0aA'|aa|'!*+'(()#1aA*))#|,aa|'*`nA2nD;aM|*0*;bR|tSsP
```
With linebreaks for "readability":
```
{vg" "aS:10sB;aM0aa,AAg**-:|al|'NdE'0nRal@(%}{->:1*?{0AaG;:?{;bI:dUNiNwR'NdEwRaC;
aM(~:?{(()1wR]0wRaC*))#'wRaC|*#|(()#aLNdEdUNdEwR]wR]aCwR*))#aC;:0wRdUaCwR*?{#aC;#
bI:*#0aA'|aa|'!*+'(()#1aA*))#|,aa|'*`nA2nD;aM|*0*;bR|tSsP
```
Ungolfed / unminified version:
```
{vg" "aS:10sB;aM  // input space-separated list of numbers
0aa,AAg           // get first element and array of all-but-first elements
**                // discard original input and the 0
// execute the following (input[0]) times
-:
    |al|'NdE'0nR               // get range from (0..input.length-1]
    al@(%}{->:1*?{0AaG;:?{;bI  // chop off first element if any
    :dUNiNwR'NdEwRaC;aM        // map n -> [n-1 n+1]
    // if the input consisted of a single element, append [0 0]
    // otherwise prepend [0 1] and append [len-2 len-1]
    (~:?{(()1wR]0wRaC*))#'wRaC|*#|(()#aLNdEdUNdEwR]wR]aCwR*))#aC;
        :0wRdUaCwR*?{#aC;#bI
    // map indeces to avg(input[i1], input[i2])
    :*#0aA'|aa|'!*+'(()#1aA*))#|,aa|'*`nA2nD;aM
    // replace old input, reset permavar
    |*0*
;bR
|tSsP  // output result
```
Sample I/O format:
```
llama@llama:...Code/snowman/ppcg53799waves$ snowman waves.snowman 
4 32 32 32 16 64 16 32 32 32
[33 27 40 22 44 22 40 27 33]
```
[Answer]
# Pyth - 25 24 bytes
Uses enumerate, reduce to do iterations.
```
u.ecs@L++hGGeG,khhk2GvzQ
```
[Try it online here](http://pyth.herokuapp.com/?code=u.ecs%40L%2B%2BhGGeG%2Ckhhk2GvzQ&input=4%0A%5B32%2C+32%2C+32%2C+16%2C+64%2C+16%2C+32%2C+32%2C+32%5D&debug=1).
[Answer]
# Racket, ~~164~~ 145 bytes
```
(define(f a n)(if(< n 1)a(f(let([l(length a)][r list-ref])(for/list([i(in-range l)])(/(+(r a(max(- i 1)0))(r a(min(+ i 1)(- l 1))))2)))(- n 1))))
```
### Ungolfed
```
(define (f a n)
  (if (< n 1)
      a
      (f (let ([l (length a)] [r list-ref])
           (for/list ([i (in-range l)])
             (/ (+ (r a (max (- i 1) 0))
                   (r a (min (+ i 1) (- l 1))))
                2))) (- n 1))))
```
Note, you may need the `#lang racket` line to run this.
[Answer]
# R, 109 bytes
```
function(x,n){l=length(x);t=if(l>2)c(.5,0,.5)else if(l==2)c(.5,.5)else 1;for(i in 1:n)x=filter(x,t,c=T);c(x)}
```
This creates an unnamed function that accepts a vector and an integer and returns a vector. The approach here is to treat the input as a univariate time series and apply a linear convolution filter.
Ungolfed + explanation:
```
f <- function(x, n) {
    # Define filter coefficients
    t <- if (length(x) > 2)
        c(0.5, 0, 0.5)
    else if (length(x) == 2)
        c(0.5, 0.5)
    else
        1
    # Apply the filter n times
    for (i in 1:n) {
        # The circular option wraps the filter around the edges
        # of the series, otherwise the ends would be set to NA.
        x <- filter(x, t, circular = TRUE)
    }
    # Returned the modified input, stripped of the extraneous
    # properties that the filter function adds.
    c(x)
}
```
Examples:
```
> f(c(32, 32, 32, 16, 64, 16, 32, 32, 32), 4)
[1] 33 27 40 22 44 22 40 27 33
> f(0, 482)
[1] 0
> f(c(0, 64), 1)
[1] 32 32
```
[Answer]
# Haskell, 76 characters
The trick is to add the first number to the beginning of the list and the last one to the end of the list instead of dealing with the boundary conditions. 
```
f a@(x:s)=(/2)<$>zipWith(+)(x:a)(s++[last s])
f x=x
a#n|n<1=a|n>0=f a#(n-1)
```
Tests:
```
λ: [0, 0, 1, 0, 0]#1  
[0.0,0.5,0.0,0.5,0.0]
λ: [0, 0, 1, 0, 0]#2
[0.25,0.0,0.5,0.0,0.25]
λ: [0, 0, 1, 0, 0]#0  
[0.0,0.0,1.0,0.0,0.0]
λ: [0, 0, 1, 0, 0]#(-39) 
[0.0,0.0,1.0,0.0,0.0]
λ: [0, 16, 32, 16, 0]#1
[8.0,16.0,16.0,16.0,8.0]
λ: [0, 1, 2, 3, 4, 5]#1
[0.5,1.0,2.0,3.0,4.0,4.5]
λ: [0, 64]#1
[32.0,32.0]
λ: [0]#482
[0.0]
λ: [32, 32, 32, 16, 64, 16, 32, 32, 32]#4
[33.0,27.0,40.0,22.0,44.0,22.0,40.0,27.0,33.0]
```
[Answer]
# CJam, ~~23~~ 22 bytes
```
q~{_(@)@@+@@+.+.5f*}*`
```
[Try it online](http://cjam.aditsu.net/#code=q~%7B_(%40)%40%40%2B%40%40%2B.%2B.5f*%7D*%60&input=%5B32%2032%2032%2016%2064%2016%2032%2032%2032%5D%204)
The input is in CJam list format, e.g. for the last example:
```
[32 32 32 16 64 16 32 32 32] 4
```
The output is also a CJam list:
```
[33.0 27.0 40.0 22.0 44.0 22.0 40.0 27.0 33.0]
```
The basic approach is that in each step, the vector is shifted one position to the left and one position to the right. Each of the two vectors is padded with the first/last element, and then the average of the two vectors is calculated.
Explanation:
```
q~    Get and interpret input.
{     Loop over repeat count.
  _     Copy list.
  (     Pop off left element.
  @     Get original list to top.
  )     Pop off right element.
  @@    Get first element and list with last element removed to top.
  +     Concatenate. This gives right-shifted list with first element repeated.
  @@    Get list with first element removed and last element to top.
  +     Concatenate. This gives left-shifted list with last element repeated.
  .+    Perform vector addition of two shifted lists.
  .5f*  Multiply sum by 0.5 to give average.
}*    End loop over repeat count.
`     Convert result array to string.
```
[Answer]
**Java, 181 bytes**
Here's the golfed version:
```
float[]g(float[]i,int n){float[]c=i.clone();int l=c.length,s=1;if(n>0&&l>1){c[0]=(i[0]+i[1])/2f;c[--l]=(i[l]+i[l-1])/2f;while(s<l)c[s]=(i[s-1]+i[++s])/2f;return g(c,n-1);}return i;}
```
Ungolfed:
```
float[] g(float[] i, int n) {
    float[] c = i.clone();
    int l = c.length,s=1;
    if(n>0&&l>1) {
        c[0] = (i[0]+i[1])/2f;
        c[--l] = (i[l]+i[l-1])/2f;
        while(s<l)
            c[s] = (i[s-1] + i[++s]) / 2f;
        return g(c, n-1);
    }
    return i;
}
```
I tried to shorten assignments and conditionals as much as possible with Java. Improvements are welcome, of course.
[Answer]
# JavaScript (ES6), ~~153~~ ~~132~~ 67 chars
I come back to my first answer 6 months later and what do I do? Golf off 50%, that's what. ;)
```
s=(a,n)=>n<1?a:s(a.map((j,i)=>(a[i&&i-1]+a[a[i+1]+1?i+1:i])/2),n-1)
```
This version calls itself repeatedly until `n` is less than 1, decrementing `n` by 1 each time.
A non-recursive solution (~~151~~ ~~130~~ 78 chars):
```
(a,n)=>n<1?a:eval("while(n--)a=a.map((j,i)=>(a[i&&i-1]+a[a[i+1]+1?i+1:i])/2)")
```
## Ungolfed: (outdated)
### Recursive:
```
s = function (a, n) {
  if (n < 1)
    return a;
  b = [];
  l = a.length;
  x = y = 0;
  for(var i = 0; i < l; i++) {
    x = a[(i < 1) ? 0 : i-1];
    y = a[(i > l-2) ? i : i+1];
    b[i] = (x + y)/2;
  }
  if (n > 2)
    return b;
  return s(b,n-1);
}
```
### Non-recursive:
```
s = function (a, n) {
  if (n < 1)
    return a;
  b = [];
  l = a.length;
  x = y = 0;
  while(n-- > 0) {
    for(var i = 0; i < l; i++) {
      x = a[(i < 1) ? 0 : i-1];
      y = a[(i > l-2) ? i : i+1];
      b[i] = (x + y)/2;
      a = b.slice(0);   // setting a to a copy of b, for copyright reasons
    }
  return b;
}
```
[Answer]
# Java, 203 bytes
Trying my first put with Java. Improvement tips are welcome:)
```
double[]f(double[]a,int n){int j,s,i=0;s=a.length-1;if(n<1||s<1)return a;double b[]=a;for(;i++<n;a=b.clone()){b[0]=.5*(a[0]+a[1]);b[s]=.5*(a[s]+a[s-1]);for(j=1;j<s;++j)b[j]=.5*(a[j-1]+a[j+1]);}return b;}
```
Pretty printed:
```
double[] g(double[] a, int n) {
  int j, s, i = 0;
  s = a.length - 1;
  if (n < 1 || s < 1)
     return a;
  double b[] = a;
  for (; i++ < n; a = b.clone()) {
     b[0] = .5 * (a[0] + a[1]);
     b[s] = .5 * (a[s] + a[s - 1]);
     for (j = 1; j < s; ++j)
        b[j] = .5 * (a[j - 1] + a[j + 1]);
  }
  return b;
}
```
[Answer]
# Python 2, 98 Bytes
Took the straightforward approach, used `exec` to get out of using a while loop. I think there's a better way of doing the logic to figure out special case positions, but this works for now. Input should be formatted like `[list], times`.
```
b,c=input()
k=~-len(b)
exec'b=[(b[x-(0<x<k)]+b[x+(x<k)-(x==k)])/2.for x in range(-~k)];'*c
print b
```
Ungolfed:
```
BASE,TIME = input()
TEMP = [0]*len(BASE)                               # Temporary array as to not modify base.
while TIME:
    for x in xrange(len(BASE)):
        if x == 0:                                
            TEMP[x] = (BASE[x]   + BASE[x+1])/2.0  # First element special case.
        elif x == len(BASE)-1:                    
            TEMP[x] = (BASE[x]   + BASE[x-1])/2.0  # Last element special case.
        else:                                     
            TEMP[x] = (BASE[x-1] + BASE[x+1])/2.0  # Every other element.
    BASE = TEMP                                    # Set base to temporary array.
    TEMP = [0]*len(BASE)                           # Reset temporary array to 0s.
    TIME = TIME - 1
print BASE
```
[Answer]
# Mathematica, 81 bytes
I have a feeling it could be golfed more if I could figure out a better way to handle the positivity condition.
```
f[l_,_]:=l;f[l_,n_/;n>0]:=Nest[.5{1,0,1}~ListConvolve~ArrayPad[#,1,"Fixed"]&,l,n]
```
Worth noting: Mathematica offers lots of potential built-in solutions in its range of list-processing and filter functions, as well as `CellularAutomaton`. I chose `Nest[... ListConvolve ...]` because it was the easiest way to work out the kinks at the ends of the list, but other angles might prove shorter.
[Answer]
## Matlab , 109
```
function a=f(a,n)
if numel(a)>1&n>0
for k=1:n
a=[a(1)+a(2) conv(a,[1 0 1],'valid') a(end-1)+a(end)]/2;end
end
```
Examples:
```
>> f([0, 0, 1, 0, 0], 1)
ans =
         0    0.5000         0    0.5000         0
>> f([0, 0, 1, 0, 0], 2)
ans =
    0.2500         0    0.5000         0    0.2500
>> f([0, 0, 1, 0, 0], 0)
ans =
     0     0     1     0     0
>> f([0, 0, 1, 0, 0], -39)
ans =
     0     0     1     0     0
>> f([0], 482)
ans =
     0
>> f([], 10)
ans =
     []
```
[Answer]
# Scala, ~~195 characters (186 with lazy output, i.e. `Stream`)~~ 187 characters
```
(t:Seq[Float],n:Int)⇒t.size match{case 0|1⇒t;case 2⇒{val a=t.sum/2;Seq(a,a)};case i⇒(t/:(1 to n)){(s,_)⇒(s.take(2).sum/2)+:s.sliding(3).map(l=>(l(0)+l(2))/2).toList:+(s.drop(i-2).sum/2)}}
```
probably not optimal, mapping `sliding(3)` is very useful in this case.
### tests:
```
scala> (t:Seq[Float],n:Int)⇒t.size match{case 0|1⇒t;case 2⇒{val a=t.sum/2;Seq(a,a)};case i⇒(t/:(1 to n)){(s,_)⇒(s.take(2).sum/2)+:s.sliding(3).map(l=>(l(0)+l(2))/2).toList:+(s.drop(i-2).sum/2)}}
res0: (Seq[Float], Int) => List[Float] = <function2>
scala> res0(Seq(0, 0, 1, 0, 0), 1)
res1: Seq[Float] = List(0.0, 0.5, 0.0, 0.5, 0.0)
scala> res0(Seq(0, 0, 1, 0, 0), 2)
res2: Seq[Float] = List(0.25, 0.0, 0.5, 0.0, 0.25)
scala> res0(Seq(0, 0, 1, 0, 0), 0)
res3: Seq[Float] = List(0.0, 0.0, 1.0, 0.0, 0.0)
scala> res0(Seq(0, 0, 1, 0, 0), -39)
res4: Seq[Float] = List(0.0, 0.0, 1.0, 0.0, 0.0)
scala> res0(Seq(0, 16, 32, 16, 0), 1)
res5: Seq[Float] = List(8.0, 16.0, 16.0, 16.0, 8.0)
scala> res0(Seq(1, 2, 3, 4, 5), 1)
res6: Seq[Float] = List(1.5, 2.0, 3.0, 4.0, 4.5)
scala> res0(Seq(0,64), 1)
res7: Seq[Float] = List(32.0, 32.0)
scala> res0(Seq(0), 482)
res8: Seq[Float] = List(0.0)
scala> res0(Seq(32, 32, 32, 16, 64, 16, 32, 32, 32), 4)
res9: Seq[Float] = List(33.0, 27.0, 40.0, 22.0, 44.0, 22.0, 40.0, 27.0, 33.0)
```
[Answer]
# q (27 characters)
```
{avg x^/:1 -1 xprev\:x}/[;]
```
Examples
```
q)f:{avg x^/:1 -1 xprev\:x}/[;]
q)f[4;32 32 32 16 64 16 32 32 32]
33 27 40 22 44 22 40 27 33f
//1-length input
q)f[10;enlist 1] 
,1f
//0-length input
q)f[10;`float$()]
`float$()
```
[Answer]
# R, 93 Bytes
As an unnamed function
```
function(a,n){l=length(a);while((n=n-1)>=0)a<-colMeans(rbind(c(a[-1],a[l]),c(a[1],a[-l])));a}
```
Expanded
```
function(a,n){
    l=length(a);             # get the length of the vector
    while((n=n-1)>=0)        # repeat n times
        a<-colMeans(         # do the column means and assign to a
          rbind(             # create an array
            c(a[-1],a[l]),   # shift the vector left and duplicate last
            c(a[1],a[-l])    # shift the vector right and duplicate first
          )
        );
    a                        # return the vector
}
```
Tests
```
> f=function(a,n){l=length(a);while((n=n-1)>=0)a<-colMeans(rbind(c(a[-1],a[l]),c(a[1],a[-l])));a}
> f(c(0, 0, 1, 0, 0), 1)
[1] 0.0 0.5 0.0 0.5 0.0
> f(c(0, 0, 1, 0, 0), 2)         
[1] 0.25 0.00 0.50 0.00 0.25
> f(c(0, 0, 1, 0, 0), 0)         
[1] 0 0 1 0 0
> f(c(0, 0, 1, 0, 0), -39)        
[1] 0 0 1 0 0
> f(c(0, 16, 32, 16, 0), 1)       
[1]  8 16 16 16  8
> f(c(0, 1, 2, 3, 4, 5), 1)      
[1] 0.5 1.0 2.0 3.0 4.0 4.5
> f(c(0, 64), 1)                  
[1] 32 32
> f(c(0), 482)                    
[1] 0
> f(c(32, 32, 32, 16, 64, 16, 32, 32, 32),4)
[1] 33 27 40 22 44 22 40 27 33
> 
```
[Answer]
# C++14, 158 bytes
```
#define D(a,b)d[i]=(c[a]+c[b])/2;
auto f(auto c,int n){while(n-->0&&c.size()>1){auto d{c};int i{};D(0,1)while(++i<c.size()-1)D(i-1,i+1)D(i,i-1)c=d;}return c;}
```
Requires input to be a container `value_type==double`, like `vector<double>`.
Ungolfed:
```
#define D(a,b) d[i] = (c[a]+c[b])/2;   //average
auto f(auto c, int n) {
  while(n-- > 0 && c.size() > 1) {     //breaks
    auto d{c};                         //copy container
    int i{};
    D(0,1)                             //left
    while(++i < c.size()-1)            //count up to right
      D(i-1,i+1)                       //interior
    D(i,i-1)                           //right
    c=d;                               //overwrite container
  }
  return c;
}
```
[Answer]
## Racket 223 bytes
```
(let p((l l)(m 0)(g list-ref))(cond[(> n m)(let*((j(length l))(k(for/list((i j))(cond[(= i 0)(/(+(g l 0)(g l 1))2)]
[(= i(- j 1))(/(+(g l(- j 2))(g l(- j 1)))2)][else(/(+(g l(+ i 1))(g l(- i 1)))2)]))))(p k(+ 1 m) g))][l]))
```
Ungolfed: 
```
(define(f l n)
  (let loop ((l l)
             (m 0)
             (lr list-ref))
    (cond
      [(> n m)
       (let* ((j (length l))
              (k (for/list ((i j))
                   (cond
                     [(= i 0)       (/ (+ (lr l 0)
                                          (lr l 1))
                                       2)]
                     [(= i (- j 1)) (/ (+ (lr l (- j 2))
                                          (lr l (- j 1)))
                                        2)]
                     [else          (/ (+ (lr l (+ i 1))
                                          (lr l (- i 1)))
                                       2)])
                   )))
         (loop k
               (+ 1 m)
               lr))]
      [else l]
      )))
```
Testing: 
```
(f '[32 32 32 16 64 16 32 32 32] 4)
```
Output: 
```
'(33 27 40 22 44 22 40 27 33)
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 14 bytes
```
.ịṚjṡ3 .ịⱮÆmµ¡
```
[Try it online!](https://tio.run/##y0rNyan8/1/v4e7uhztnZT3cudBYAcR5tHHd4bbcQ1sPLfz//3@0sZGOAgwbmukomJlAaIR47H8TAA "Jelly – Try It Online")
Full program.
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), ~~160~~ 144 bytes
```
float[]a(float[]b,int c)=>c>0&b.Length>1?a(b.Select((_,w)=>w<1?(b[w]+b[w+1])/2:w>b.Length-2?(b[w]+b[w-1])/2:(b[w-1]+b[w+1])/2).ToArray(),c-1):b;
```
Uses good ol' recursion.
[Try it online!](https://tio.run/##xZLfT4MwEMff@SsuezBtOJAyXHQMFh/0aW@a@ECIKU1xJBMSqBJD@NtnO4YYtz3bH9fr3efSb3IVjSOaYr/PdxVXScrJ0cmwKBUIGsUi9q4ydyPLN7WN2ZqTzH2SOykUIa/YaqBdsTXJkja1tbFZSq/9ZRuPJY4/JZ0hSQZ3wqn7XN3XNf8iFIXD6DILR0FJCko2CiLorFK2cAxD5yHoxQ7W6xF@xj9gJxBbIMz94Zy4UwxBQ3OEAOHmMrYIfiu68Og55C9kJI3bSFsEk9Rh91YfWp@8BhWZSv0HkrRj6KOHzvwO2WEGtz4GmrPyqiYGziMvzFemT8emh7ltU6uztAJOTDzJUzTGdHpTNIpQ97GqH7jYkvLjPYpf6kJJ49ozhBmloSk9BDdFKcmZe7//Bg "C# (Visual C# Interactive Compiler) – Try It Online")
[Answer]
# Japt, ~~39~~ 37 bytes
I just wanted to see how well my golfing language could hold up to my first challenge.
```
Vm0 o r_£ZgY©Y-1 +ZgY>Zl -2?Y:°Y)/2}U
```
[Try it online!](http://ethproductions.github.io/japt?v=master&code=Vm0wIG8gcl+jWmdZqVktMSArWmdZPlpsIC0yP1k6sFkpLzJ9VQ==&input=WzMyLCAzMiwgMzIsIDE2LCA2NCwgMTYsIDMyLCAzMiwgMzJdLCA0)
```
Vm0 o r_£ZgY©Y-1 +ZgY>Zl -2?Y:°Y)/2}U
Vm0 o      // Generate the range of integers [0, max(V,0)).
r_     }U  // Reduce it with this function, with a starting value of U:
£          //  Return the argument, with each item X, index Y, and the full array Z mapped by this function:
ZgY©Y-1 +  //   Return (Z[max(Y-1,0)] plus
ZgY>Zl -2? //    Z[Y > Z.length-2?
Y:°Y)      //      Y:--Y],)
/2         //   divided by two.
           // Implicit: output last expression
```
]