title: Finding the median value of a vector using C++
body: <p>I'm a programming student, and for a project I'm working on, on of the things I have to do is compute the median value of a vector of int values and must be done by passing it through functions. Also the vector is initially generated randomly using the C++ random generator mt19937 which i have already written down in my code.I'm to do this using  the sort function  and vector member functions such as <code>.begin()</code>, <code>.end()</code>, and <code>.size()</code>.</p>
  
  <p>I'm  supposed to make sure I find the median value of the vector and then output it</p>
  
  <p>And I'm <strong>Stuck</strong>, below I have included my attempt. So where am I going wrong? I would appreciate if you would be willing to give me some pointers or resources to get going in the right direction.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  #include&lt;cstdlib&gt;
  #include&lt;ctime&gt;
  #include&lt;random&gt;
  #include&lt;vector&gt;
  #include&lt;cstdlib&gt;
  #include&lt;ctime&gt;
  #include&lt;random&gt;
  
  using namespace std;
  
  double find_median(vector&lt;double&gt;);
  double find_median(vector&lt;double&gt; len)
  {
      {
          int i;
          double temp;
          int n=len.size();
          int mid;
          double median;
          bool swap;
  
          do
          {
              swap = false;
              for (i = 0; i&lt; len.size()-1; i++)
              {
                  if (len[i] &gt; len[i + 1])
                  {
                      temp = len[i];
                      len[i] = len[i + 1];
                      len[i + 1] = temp;
                      swap = true;
                  }
              }
          }
          while (swap);
  
          for (i=0; i&lt;len.size(); i++)
          {
              if (len[i]&gt;len[i+1])
              {
                  temp=len[i];
                  len[i]=len[i+1];
                  len[i+1]=temp;
              }
              mid=len.size()/2;
              if (mid%2==0)
              {
                  median= len[i]+len[i+1];
              }
              else
              {
                  median= (len[i]+0.5);
              }
          }
          return median;
      }
  }
      int main()
      {
          int n,i;
  
          cout&lt;&lt;"Input the vector size: "&lt;&lt;endl;
          cin&gt;&gt;n;
          vector &lt;double&gt; foo(n);
          mt19937 rand_generator;
          rand_generator.seed(time(0));
  
          uniform_real_distribution&lt;double&gt; rand_distribution(0,0.8);
          cout&lt;&lt;"original vector: "&lt;&lt;" ";
          for (i=0; i&lt;n; i++)
          {
              double rand_num=rand_distribution(rand_generator);
              foo[i]=rand_num;
              cout&lt;&lt;foo[i]&lt;&lt;" ";
  
          }
  double median;
          median=find_median(foo);
  
          cout&lt;&lt;endl;
  
          cout&lt;&lt;"The median of the vector is:  "&lt;&lt;" ";
  cout&lt;&lt;median&lt;&lt;endl;
      }
  </code></pre>