title: Finding min and max in 2-D arrays?
body: <p>After running this code , it will generate a random array of desired rows and columns . Then a loop will divide the array by its diagonal into an upper side and lower side.In the upper side the loop will look for a <code>max number</code> and in the lower side the loop will look for a <code>min number</code> . Then in the final stage I need to change the positions of <code>min</code> and <code>max</code> . <code>Max</code> in place of <code>min</code> and vice versa.The code runs and finds the <code>min</code> and <code>max</code>.Don't know how to change their places.</p>
  
  <blockquote>
    <p>Code</p>
  </blockquote>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;limits.h&gt;
  
  
  using namespace std;
  
  int main(){
  int rows, columns;
  int max = INT_MAX;
  int min = INT_MIN;
  int XindexOfMax, YindexOfMax;
  int XindexOfMin, YindexOfMin;
  
  cout &lt;&lt; "Enter rows: ";
  cin &gt;&gt; rows;
  cout &lt;&lt; "Enter columns: ";
  cin &gt;&gt; columns;
  
  int **array = new int *[rows];            //generating random array
  for(int i = 0; i &lt; rows; i++)
      array[i] = new int[columns];
  
  srand((unsigned int)time(NULL));         //generating randoms
  
  for(int i = 0; i &lt; rows; i++){           //loop for the main array
      for(int j = 0; j &lt; columns; j++){
          array[i][j] = rand() % 10;    
          cout &lt;&lt; array[i][j] &lt;&lt; " "; 
      }
      cout &lt;&lt; "\n";
  }
  
  cout &lt;&lt; "For finding Max: " &lt;&lt; endl;
  
  for(int i = 0; i &lt; rows; i++){             //upper half of the diagonal
      for(int j = 0; j &lt; columns - i; j++){
          cout &lt;&lt; array[i][j] &lt;&lt; " ";
          if(array[i][j] &gt; max){
              max = array[i][j];
              XindexOfMax = i;              //find x and y coordinates if max
              YindexOfMax = j;
          }
  
      }
      cout &lt;&lt; "\n";
  }
  cout &lt;&lt; "For finding Min: " &lt;&lt; endl;
  
  for (int i = 0; i &lt; rows; i++){          // lower half of the diagonal
      for (int j = 0; j &lt; columns; j++){
          if (j &lt; columns - i - 1){
              cout &lt;&lt; "  ";
          }
          else{
              cout &lt;&lt; array[i][j] &lt;&lt; " "; 
              if(array[i][j] &lt; min){
                  min = array[i][j];
                  XindexOfMin = i;       //find x and y coordinates if min
                  YindexOfMin = j;
              }
          }
      }
  cout &lt;&lt; "\n";
  }
  cout &lt;&lt; "Result" &lt;&lt; endl;
  //swapping positions of min and max 
  std::swap(array[XindexOfMax][YindexOfMax], array[XindexOfMin][YindexOfMin]);
  
  for(int i = 0; i &lt; rows; i++){        
      for(int j = 0; j &lt; columns; j++){
          cout &lt;&lt; array[i][j] &lt;&lt; " ";   //Printing the final array
      }
      cout &lt;&lt; "\n";
  }
  return 0;
  }
  </code></pre>