title: Returning a 2D dynamic array from a fuction
body: <p>I need some help with returning a 2d dynamic array(matrix) from a function. The function takes the matrix inputted and finds the transpose and returns the matrix. My main takes the number of rows and columns the user wants and then takes the entries for the matrix. Then I call my function and try to hold it in another dynamic matrix I created and try to court the matrix but I have a problem doing that. I do not know how to hold the resultant matrix. Any help is much-appreciated thanks.
  Can you also check if my deallocation process is alright?</p>
  <p>Just realized the program works for square matrices but not if rows and cols are different, can you please point out my mistake?</p>
  <pre><code>int** transpose(int** arr, int rows, int cols)
  {
      int** array1 = new int*[cols];
  
      for(int i =0; i &lt; cols; i++)              
      {
          array1[i] = new int[rows];
      }
  
      for(int i = 0; i &lt; rows; i++)
      {
          for(int j = 0; j &lt; cols; j++)
          {
              array1[i][j] = arr[j][i];
          }
      }
      
      return array1;
  for(int i =0; i &lt; cols; i++)
  {
      delete[] array1;
  }
  delete[] array1;
  }
  
  int main()
  {
  int rows, cols;
      cout &lt;&lt; &quot;Please enter the number of rows for the matrix: &quot;;
      cin &gt;&gt; rows;
      cout &lt;&lt; &quot;Please enter the number of columns for the matrix: &quot;;
      cin &gt;&gt; cols;
  
      int** array0 = new int*[rows];
  
      for(int i =0; i &lt; rows; i++)
      {
          array0[i] = new int[cols];
      }
  
      // how to cin the elements for the matrix now
      //basicalliy it is thnesame as any 2d array
      for(int i = 0; i &lt; rows; i++)
      {
          for(int j = 0; j &lt; cols; j++)
          {
              cin &gt;&gt; array0[i][j];
          }
      }
  
      for(int i = 0; i &lt; rows; i++)
      {
          for(int j = 0; j &lt; cols; j++)
          {
              cout &lt;&lt; array0[i][j] &lt;&lt; &quot; &quot;;
          }
          cout &lt;&lt; endl;
      }
  
      int** array2 = new int*[cols];
      for(int i = 0; i &lt; cols; i++)
      {
          array2[i] = new int[cols];
      }
      array2 = transpose(array0, rows, cols);
  
      for(int i = 0; i &lt; rows; i++)
      {
          for(int j = 0; j &lt; cols; j++)
          {
              cout &lt;&lt; array2[i][j] &lt;&lt; &quot; &quot;;
          }
          cout &lt;&lt; endl;
      }
      for(int i =0; i &lt; cols; i++)
      {
          delete[] array2;
      }
      delete[] array2;
  
      for(int i =0; i &lt; rows; i++)
      {
          delete[] array0;
      }
       delete[] array0;
  
  
  
      system(&quot;pause&quot;);
      return 0;
  }
  </code></pre>