title: unwanted change a data in a filter algorithm
body: <p>I have written the simple linear median filter code in C++ programming language. I have a severe problem, in the part of this filter my data will change and the result of this filter does not make any sense. I have found in which part this occurrence happened but I have no idea how to fix this problem. I mean when I print the data in the commented line I wil l get the wrong answer. For instance , the result of buffer_data[1] in the first deceleration of this value is 51 but this result in the commented lines becomes 1434234133123 or so on. </p>
  
  <pre><code>double median_filter( int data, int counter){
    int sum=0;
    double result=0;
    static int buffer_length=8;
    int buffer_data[buffer_length];
    int temp=0;
  
  // first time initiation
  
        if (counter==1){
            for (int i=1;i&lt;=buffer_length;i++){
              buffer_data[i]=0;
            }
        }
  
        if (counter &lt;= buffer_length){
        buffer_data[counter]=data;
        }
  
        /* this part of the code changes the data of the buffer or so on
  
        else{
              for (int i=1;i&lt;=(buffer_length-1);i++){             
                buffer_data[i]=buffer_data[i+1];
              }
              buffer_data[buffer_length]=data;
        }
  
       */ 
             for (int i=1;i&lt;=buffer_length;i++){
              sum=sum+(buffer_data[i]);
             }
  
             if (counter&lt;= buffer_length)
             result=double(sum/counter);
             else
             result=double(sum/buffer_length);
  
             return (result);
  }
  </code></pre>
  
  <p>Update[1]</p>
  
  <p>I have fixed your precious comments, but still the problem exist. Here is the fixed code based on your comments.</p>
  
  <pre><code>double median_filter( int data, int counter){
    int sum=0;
    double result=0;
    int buffer_data[8];
    int temp=0;
  // first time initiation
        if (counter==1){
            for (int i=0;i&lt;=(7);i++){
              buffer_data[i]=0;
            }
        }
  
          if (counter &lt;= (7)){
            buffer_data[counter]=data;          
          }
        //this part of the code changes the data of the buffer or so on
        else{
              for (int i=0;i&lt;=(6);i++){             
                buffer_data[i]=buffer_data[i+1];
              }
              buffer_data[7]=data;
        }
  
             for (int i=0;i&lt;=(7);i++){
                sum=sum+(buffer_data[i]);
             }
  
             if (counter&lt; (7))
             result=double(sum/(counter+1));
             else
             result=double(sum/8);
  
             return (result);
  }
  </code></pre>