title: C++ How can I find the mode(s) of an array?
body: <p>I have to write a function that finds the mode of an array of values for class. It takes two arguments: the array of values and a variable equal to the number of valid values in the array. It sounded easy enough but after a week of trying I can't seem to my code work.</p>
  
  <p>The idea is to take the one dimensional array and store it in a 2 dimensional array where the first value is a value from the array of values and the second is the number of times that value occurs. So far it isn't working quite right, or at all. The problem feels like it should be obvious but I've been stumped for a week.</p>
  
  <p>I'm testing it with an array containing the following:
  9.0, 4.0, 4.0, 4.0, 5.0, 5.0, 6.0, 6.0, 6.0, 7.0, 1.0, 9.0, 10.0</p>
  
  <p>Code:</p>
  
  <pre><code>void mode(double x[], const int n)
  {
      int j, k, m=1, p, numofmodes=0;
      bool match=false, breaker;
      double y[100][2]={0}, max=0;
  
  
  
      y[0][0] = x[0];
      y[0][1] = 1;
  
      for(j=1; j&lt;=(n-1); j++)  //
      {
          for (k=0; k&lt;=(m-1); k++)
              if (x[j] == y[k][0])
              {
                  y[k][1]++;
                  match = true;
              }
  
          if (match == false)
          {
              y[m][0] = x[j];
              y[m][1] = 1;
              m++;
          }
          match = false;
      }
  
  
  
      for(j=0; j&lt;=(n-1); j++)
      {
          if (y[j][1] &gt; max)
              max = y[j][1];
      }
  
      for(j=0; j&lt;=(n-1); j++)
      {
          if (y[j][1] = max)
              numofmodes++;
      }
  
  
      for(j=0; j&lt;=(n-1); j++)
      {
          cout&lt;&lt;y[j][0]&lt;&lt;"    "&lt;&lt;y[j][1]&lt;&lt;endl;
      }
  
  
      cout&lt;&lt;"There are "&lt;&lt;numofmodes&lt;&lt; " modes in the data set."&lt;&lt;endl;
  
      for(j=0; j&lt;=(n-1); j++)
      {
          if (y[j][1] = max)
          {
              cout&lt;&lt;y[j][0]&lt;&lt;" appears "&lt;&lt;max&lt;&lt;" times."&lt;&lt;endl;
          }
      }
  
  }
  </code></pre>
  
  <p>Output:
  9    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  0    2
  There are 13 modes in the data set.
  9 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.
  0 appears 2 times.</p>