title: Recursively obtaining kth smallest element in an array
body: <p>I'm trying to "translate" this algorithm from the pseudocode provided in my textbook. My program keeps crashing though, and I'm not really sure where I went wrong with my implementation. Here's the pseudocode in image with my code right below it: <img src="https://i.stack.imgur.com/5Tzcz.png" alt="enter image description here"></p>
  
  <pre><code>int kSmallFirst (int k, int anArray[], int first, int last) {
      int pivotIndex = 0;
  
      if (k &lt; pivotIndex - first + 1)
          return kSmallFirst(k, anArray, first, pivotIndex - 1);
      else if (k == pivotIndex - first + 1)
          return pivotIndex;
      else
          return kSmallFirst(k - (pivotIndex - first + 1), anArray, pivotIndex + 1, last);
  }
  
  int main () {
      int i = 0;
      int arr[512];
      fstream data;
      data.open("data.txt");
  
      while (!data.eof()) {
          data &gt;&gt; arr[i];
          i++;
      }
  
      data.close();
  
      cout &lt;&lt; kSmallFirst(42, arr, 0, i-1);
  
      return 0;
  }
  </code></pre>
  
  <p>Thank you very much!</p>