title: Algorithm to compute mode
body: <p>I'm trying to devise an algorithm in the form of a function that accepts two parameters, an array and the size of the array. I want it to return the mode of the array and if there are multiple modes, return their average. My strategy was to take the array and first sort it. Then count all the occurrences of a number. while that number is occurring, add one to counter and store that count in an array m. So m is holding all the counts and another array q is holding the last value we were comparing. </p>
  
  <p>For example: is my list is <code>{1, 1, 1, 1, 2, 2, 2}</code>
  then i would have <code>m[0] = 4 q[0] = 1
  and then m[1] = 3 and q[1] = 2.</code></p>
  
  <p>so the mode is <code>q[0] = 1;</code></p>
  
  <p>unfortunately i have had no success thus far. hoping someone could help.</p>
  
  <pre><code>float mode(int x[],int n)
  {
      //Copy array and sort it
      int y[n], temp, k = 0, counter = 0, m[n], q[n];
  
      for(int i = 0; i &lt; n; i++)
          y[i] = x[i];
  
      for(int pass = 0; pass &lt; n - 1; pass++)
          for(int pos = 0; pos &lt; n; pos++)
              if(y[pass] &gt; y[pos]) {
                  temp = y[pass];
                  y[pass] = y[pos];
                  y[pos] = temp;
              }
  
      for(int i = 0; i &lt; n;){
          for(int j = 0; j &lt; n; j++){
              while(y[i] == y[j]) {
                  counter++;
                  i++;
              }
          }
          m[k] = counter;
          q[k] = y[i];
          i--; //i should be 1 less since it is referring to an array subscript
          k++;
          counter = 0;
      }
  
  }
  </code></pre>