title: C++: Mean Median and Mode
body: <p>I've recently created a C++ program to find the mean median and mode of an array of values. I realize this would be much better to do within a class. However, my function to generate the mean is not spitting out the right number, although I'm pretty certain the logic is fine. </p>
  
  <p>Also, I was able to modify a snipbit from something I found online to create a function that generates the mode, or at least the 1st most occurring values it can find, that I was able to implement. However, I am not 100% sure of how to wrap my head around what is actually happening within the function.</p>
  
  <p>A better understanding of what is happening in the mode function and what the hell is going wrong in my mean function would be greatly appreciated.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  void mode(int[], int);
  void mean(int[], int);
  void sort(int[], int);
  void median(int[], int);
  
  int main()
  {
  
     int array[15];
     float total, mode;
     int n = 15;//number of elements in array
  
      //fill in the value of array
      for(int i=0; i&lt;n; i++){
          cout &lt;&lt; "fill in the "&lt;&lt; i+1 &lt;&lt; " number. :";
          cin &gt;&gt; array[i];
      }
  
      sort(array, n);
      return 0;
  }
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  void mean(int new_array[], int num){
   //GET TOTAL &amp; CALCULATE MEAN
      float total;
      for(int i=0;i&lt;num; i++){
          total += new_array[i];
      }
      cout &lt;&lt; "The mean is " &lt;&lt; total/num &lt;&lt; endl;
      mode(new_array, num);
      }
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  void median(int new_array[], int num){
      //CALCULATE THE MEDIAN (middle number)
      if(num % 2 != 0){// is the # of elements odd?
          int temp = ((num+1)/2)-1;
          cout &lt;&lt; "The median is " &lt;&lt; new_array[temp] &lt;&lt; endl;
      }
      else{// then it's even! :)
          cout &lt;&lt; "The median is "&lt;&lt; new_array[(num/2)-1] &lt;&lt; " and " &lt;&lt; new_array[num/2] &lt;&lt; endl;
      }
      mean(new_array, num);
  }
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  void mode(int new_array[], int num) {
      int* ipRepetition = new int[num];
      // alocate a new array in memory of the same size (round about way of defining number of elements by a variable)
      for (int i = 0; i &lt; num; i++) {
          ipRepetition[i] = 0;//initialize each element to 0
          int j = 0;//
          while ((j &lt; i) &amp;&amp; (new_array[i] != new_array[j])) {
              if (new_array[i] != new_array[j]) {
                  j++;
              }
          }
          (ipRepetition[j])++;
      }
      int iMaxRepeat = 0;
      for (int i = 1; i &lt; num; i++) {
          if (ipRepetition[i] &gt; ipRepetition[iMaxRepeat]) {
              iMaxRepeat = i;
          }
      }
      cout&lt;&lt; "The mode is " &lt;&lt; new_array[iMaxRepeat] &lt;&lt; endl;
  
  }
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  
  void sort(int new_array[], int num){
       //ARRANGE VALUES
      for(int x=0; x&lt;num; x++){
           for(int y=0; y&lt;num-1; y++){
               if(new_array[y]&gt;new_array[y+1]){
                   int temp = new_array[y+1];
                   new_array[y+1] = new_array[y];
                   new_array[y] = temp;
               }
           }
       }
      cout &lt;&lt; "List: ";
      for(int i =0; i&lt;num; i++){
          cout &lt;&lt; new_array[i] &lt;&lt; " ";
      }
      cout &lt;&lt; "\n";
      median(new_array, num);
  }
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////////////////////////////
  </code></pre>