title: k'th largest element in an array
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include&lt;algorithm&gt;
  
  using namespace std;
  
  bool findNthMax(double arr[], int len, int n, double&amp; nth_max)
  {
  
  sort(arr, arr+n);
  
  
  //Counter
  int c = 1;
  
  //Initialize nth max
  nth_max = arr[0];
  
  //Loop in the array while the count is less than (n)
  for (int i = 0; i &lt; len &amp;&amp; c &lt; n; i++)
  {
  
      if (arr[i] != nth_max)
          c++;
  
  
      nth_max = arr[i];
  
  }
  
  return nth_max;
  
  }
  
  int main()
  {
  int n;
  double arr[10];
  double y;
  
  cout &lt;&lt; "The random array elements are:" &lt;&lt; endl;
  
  srand((unsigned)time(0));
  for(int i=0; i&lt;10; i++){
      arr[i] = (rand()%100);
  
      cout &lt;&lt; arr[i] &lt;&lt; ' ' ;
  
  }
  
  cout &lt;&lt; endl;
  cout &lt;&lt; "Input a positive integer n: " ;
  cin  &gt;&gt;  n ;
  
  if(n&gt;10)
  cout &lt;&lt; "The nth largest value does not exist." &lt;&lt; endl;
  else
  cout &lt;&lt; "The nth largest value is: " &lt;&lt; findNthMax(arr, 10, n, y) &lt;&lt; endl;
  
  return 0;
  
  }
  </code></pre>
  
  <p>EDIT: the function returns false if the nth largest value does not exist; the first parameter is the  input array; the second parameter is the array size; the third parameter is used to pass the value of n to the function; the last 
  parameter is nth largest value being found.</p>
  
  <p>The main function generates an array with 10 double random elements (in the range [0, 99]. It prints out those 10 elements. Prompt a user to enter a 
  positive integer n, then print out the nth largest value in the 
  array.</p>
  
  <p>How ever my outputs shows that nth largest: 1 when n = 2</p>