title: Swapping rows in 2-D array C++
body: <p>So yeah, I wrote a program which sort rows of two-dimensional array in ascending order according to sum of all positive even elements in every row, BUT it does not work properly. Sometimes it can swap rows correctly but mostly it feels like this program only swap two adjacent rows or something like that.</p>
  <p>Probably, there is a problem with incorrect using bubble sort for 2-d arrays. Here is the function where I did most things:</p>
  <pre><code>void print(int** arr, int rows, int columns) {
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              cout &lt;&lt; setw(7) &lt;&lt; arr[i][j];
          }
          cout &lt;&lt; endl;
      }
      int* sumRows = new int[rows];
      int sum = 0;
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              if (arr[i][j] &gt; 0 &amp;&amp; arr[i][j] % 2 == 0)
              {
                  //cout &lt;&lt; setw(7) &lt;&lt; arr[i][j];
                  sum = sum + arr[i][j];
              }
          }
          cout &lt;&lt; endl &lt;&lt; &quot;Sum of positive even elements in the row &quot; &lt;&lt; i + 1 &lt;&lt; &quot; = &quot; &lt;&lt; sum;
          sumRows[i] = sum;
          sum = 0;
      }
      cout &lt;&lt; endl &lt;&lt; &quot;Array of sums: &quot;;
      for (int i = 0; i &lt; rows; i++) {
          cout &lt;&lt; setw(7) &lt;&lt; sumRows[i];
      }
  
      //for (int i = 0; i &lt; r; i++) cout &lt;&lt; setw(7) &lt;&lt; sumRows[i];
      cout &lt;&lt; endl;
  
      bool swapped;
      for (int i = 0; i &lt; rows - 1; i++)
      {
          swapped = false;
          for (int j = 0; j &lt; columns - 1; j++)
          {
              for (int k = 0; k &lt; rows - i - 1; k++) {
                  if (sumRows[k] &gt; sumRows[k + 1])
                  {
                      swap(arr[k][j], arr[k + 1][j]);
                      swapped = true;
                  }
              }
          }
  
          if (swapped == false) break;
      }
  
      cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; &quot;Swapped array:&quot; &lt;&lt; endl;
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              cout &lt;&lt; setw(7) &lt;&lt; arr[i][j];
          }
          cout &lt;&lt; endl;
      }
  }
  
  </code></pre>
  <p>Full code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;time.h&gt;
  #include &lt;conio.h&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  int** createMalloc(int, int);
  int** createCalloc(int rows, int columns);
  
  int** createNew(int rows, int columns);
  void deleteNew(int** arr, int rows);
  
  void init(int**, int, int);
  void freeMemory(int**, int);
  void print(int**, const int, const int);
  
  void initPrint(int** arr, int rows, int columns);
  
  void main() {
      int rowCount, colCount;
      cout &lt;&lt; &quot;Enter number of rows: &quot;; cin &gt;&gt; rowCount;
      cout &lt;&lt; &quot;Enter number of columns: &quot;; cin &gt;&gt; colCount;
      cout &lt;&lt; &quot; Array creation algorithm\n&quot;;
  start:
      cout &lt;&lt; &quot;Input number : \n1 for malloc\n2 for calloc\n3 for new\n&quot;;
      int k;
      cin &gt;&gt; k;
      switch (k) {
      case 1: {
          int** a = createMalloc(rowCount, colCount);
          initPrint(a, rowCount, colCount);
  
          freeMemory(a, rowCount);
          break;
      }
      case 2: {
          int** a = createCalloc(rowCount, colCount);
          initPrint(a, rowCount, colCount);
  
          freeMemory(a, rowCount);
          break;
      }
      case 3: {
          int** a = createNew(rowCount, colCount);
          initPrint(a, rowCount, colCount);
  
          deleteNew(a, rowCount);
          break;
      }
      default:cout &lt;&lt; &quot;Input 1, 2 or 3, please.&quot;;
          cout &lt;&lt; endl &lt;&lt; endl;
          goto start;
      }
      cout &lt;&lt; endl &lt;&lt; endl;
  }
  
  int** createMalloc(int rows, int columns) {
      int** arr = (int**)malloc(rows * sizeof(int*));
      for (int i = 0; i &lt; rows; i++) {
          arr[i] = (int*)malloc(columns * sizeof(int));
      }
      return arr;
  }
  
  int** createCalloc(int rows, int columns) {
      int** arr = (int**)calloc(rows, sizeof(int*));
      for (int i = 0; i &lt; rows; i++) {
          arr[i] = (int*)calloc(columns, sizeof(int));
      }
      return arr;
  }
  
  int** createNew(int rows, int columns) {
      int** arr = new int* [rows];
      for (int i = 0; i &lt; rows; i++) {
          arr[i] = new int[columns];
      }
      return arr;
  }
  
  void initPrint(int** arr, int rows, int columns) {
      init(arr, rows, columns);
      print(arr, rows, columns);
  }
  
  void init(int** arr, int rows, int columns) {
      const int Low = -10, High = 10;
      srand((unsigned)time(NULL));
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              arr[i][j] = Low + rand() % (High - Low + 1);
          }
      }
  }
  
  void freeMemory(int** arr, int rows) {
      for (int i = 0; i &lt; rows; i++) {
          free(arr[i]);
      }
      free(arr);
  }
  
  void deleteNew(int** arr, int rows) {
      for (int i = 0; i &lt; rows; i++) {
          delete[] arr[i];
      }
      delete[] arr;
  }
  
  void print(int** arr, int rows, int columns) {
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              cout &lt;&lt; setw(7) &lt;&lt; arr[i][j];
          }
          cout &lt;&lt; endl;
      }
      int* sumRows = new int[rows];
      int sum = 0;
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              if (arr[i][j] &gt; 0 &amp;&amp; arr[i][j] % 2 == 0)
              {
                  //cout &lt;&lt; setw(7) &lt;&lt; arr[i][j];
                  sum = sum + arr[i][j];
              }
          }
          cout &lt;&lt; endl &lt;&lt; &quot;Sum of positive even elements in the row &quot; &lt;&lt; i + 1 &lt;&lt; &quot; = &quot; &lt;&lt; sum;
          sumRows[i] = sum;
          sum = 0;
      }
      cout &lt;&lt; endl &lt;&lt; &quot;Array of sums: &quot;;
      for (int i = 0; i &lt; rows; i++) {
          cout &lt;&lt; setw(7) &lt;&lt; sumRows[i];
      }
  
      //for (int i = 0; i &lt; r; i++) cout &lt;&lt; setw(7) &lt;&lt; sumRows[i];
      cout &lt;&lt; endl;
  
      bool swapped;
      for (int i = 0; i &lt; rows - 1; i++)
      {
          swapped = false;
          for (int j = 0; j &lt; columns - 1; j++)
          {
              for (int k = 0; k &lt; rows - i - 1; k++) {
                  if (sumRows[k] &gt; sumRows[k + 1])
                  {
                      swap(arr[k][j], arr[k + 1][j]);
                      swapped = true;
                  }
              }
          }
  
          //IF no two elements were swapped by inner loop, then break 
          if (swapped == false) break;
      }
  
      cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; &quot;Swapped array:&quot; &lt;&lt; endl;
      for (int i = 0; i &lt; rows; i++) {
          for (int j = 0; j &lt; columns; j++) {
              cout &lt;&lt; setw(7) &lt;&lt; arr[i][j];
          }
          cout &lt;&lt; endl;
      }
  }
  </code></pre>
  <p>P.S. Two-dimensional arrays must be necessarily dynamic ones. Besides, I needed to give user a choice of creating an array with <code>malloc</code>, <code>calloc</code> or <code>new</code>. Also, cannot do this task using <code>vector</code> yet.</p>
  <p>P.P.S. I know that most of you will find it easy but for me it is definitely not as this task is my homework in a university and we have not learnt any sorting algorithms. Anyway, when I asked the teacher how can I sort rows like that, he told me to use bubble sort so here I am</p>