title: Recursively find min and max value in array
body: <p>I made a recursive function to find the max and min value from an array which may contain arbitrary number of elements. The main reason behind making this was to develop an idea in finding the min max value from the pixel data of a Dicom image. I made this recursive function as a test code where I filled an <code>int</code> type array with random numbers ranging from 0-1000. My code is as below. I presented the whole code, you can run the program very easily in Visual Studio yourself.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  using  namespace std;
  
  void recursMax(int* a, int size, int* maxValue)
  {
      int half = size/2;
      int* newMax = new int[half];
      for(int i=0; i&lt;half; i++)
      {
          newMax[i]=a[i]&gt;a[size-i-1]?a[i]:a[size-i-1];
      }
      if(half&gt;1)
      {
          recursMax(newMax, half, maxValue);
      }
      if(half == 1)
      {
          *maxValue = newMax[0];
          delete [] newMax;
      }
  }
  
  void recursMin(int* a, int size, int* minValue)
  {
      int half = size/2;
      int* newMin = new int[half];
      for(int i=0; i&lt;half; i++)
      {
          newMin[i]=a[i]&lt;a[size-i-1]?a[i]:a[size-i-1];
      }
  
      if(half&gt;1)
      {
          recursMin(newMin, half, minValue);
      }
      if(half == 1)
      {
          *minValue = newMin[0];
          delete [] newMin;
      }
  }
  
  int main ()
  {
      int size = 100;
      int* a = new int[size];
      srand(time(NULL));
      for(int i=0; i&lt;size; i++)
      {
          a[i]=rand()%1000;
          cout&lt;&lt;"Index : "&lt;&lt;i+1&lt;&lt;",  "&lt;&lt;a[i]&lt;&lt;endl;
      }
      cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;"Now we look to find the max!"&lt;&lt;endl;
      int maxValue = 0;
      int minValue = 0;
      recursMax(a, size, &amp;maxValue);
      cout&lt;&lt;maxValue&lt;&lt;endl;
      recursMin(a, size, &amp;minValue);
      cout&lt;&lt;"Now we look for the min value!"&lt;&lt;endl&lt;&lt;minValue&lt;&lt;endl;
      cout&lt;&lt;"Checking the accuracy! First for Max value!"&lt;&lt;endl;
      for(int i=0; i&lt;size; i++)
      {
          cout&lt;&lt;"Index : "&lt;&lt;i+1&lt;&lt;",  "&lt;&lt;maxValue-a[i]&lt;&lt;endl;
      }
      cout&lt;&lt;"Checking the accuracy! Now for min value!"&lt;&lt;endl;
      for(int i=0; i&lt;size; i++)
      {
          cout&lt;&lt;"Index : "&lt;&lt;i+1&lt;&lt;",  "&lt;&lt;a[i]-minValue&lt;&lt;endl;
      }
      delete [] a;
      return 0;
  }
  </code></pre>
  
  <p>My question to you is that, do you think my algorithm works correctly? I'm have some doubt. Also, am I handling or maintaining the memory correctly? Or there will be some memory leakage in the code?</p>