title: Trying to tranpose matrix using pointer arithmetic
body: <p>I am trying to write a program that
  takes inputs on the amount of rows and columns
  of a given matrix. Then I would like to ask the user to input the values on each indice, then print out the array.</p>
  <p>I accomplished this by doing:</p>
  <pre><code>int main() {
      int rows, columns;
      int i, j;
      printf(&quot;Enter amount of rows: &quot;);
      scanf(&quot;%d&quot;, &amp;rows);
      printf(&quot;Enter amount of columns: &quot;);
      scanf(&quot;%d&quot;, &amp;columns);
     
      int array[rows][columns];
      for (i = 0; i &lt; rows; i++) {
          for (j = 0; j &lt; columns; j++){
              printf(&quot;Enter element of row %d and column %d: &quot;, i + 1, j + 1);
              scanf(&quot;%d&quot;, &amp;array[i][j]);
          }
      }
     
      printf(&quot;You entered:\n&quot;);
      for (i = 0; i &lt; rows; i++) {
          for (j = 0; j &lt; columns; j++) {
              if (j == 0)
                  printf(&quot;[ &quot;);
              printf(&quot;%d &quot;, array[i][j]);
              if (j == columns - 1)
                  printf(&quot;]\n&quot;);
          }
      }
      printf(&quot;-----------------------------\n&quot;);
  </code></pre>
  <p>This part of the code gives expected output. Whenever the amount of rows and columns are the same, I get my expected output as well. e.g:</p>
  <pre><code>You entered:
  [ 1 2 3 ]
  [ 4 5 6 ]
  [ 7 8 9 ]
  -----------------------------
  Transposed matrix:
  [ 1 4 7 ]
  [ 2 5 8 ]
  [ 3 6 9 ]
  </code></pre>
  <p>Whenever there are more columns than rows, I get expected output as well.</p>
  <pre><code>You entered:
  [ 1 2 3 4 5 ]
  [ 6 7 8 9 10 ]
  [ 11 12 13 14 15 ]
  -----------------------------
  Transposed matrix:
  [ 1 6 11 ]
  [ 2 7 12 ]
  [ 3 8 13 ]
  [ 4 9 14 ]
  [ 5 10 15 ]
  </code></pre>
  <p>However, whenever there are more rows than columns, that's where the issue seems to arise.
  For example:</p>
  <pre><code>You entered:
  [ 1 2 ]
  [ 3 4 ]
  [ 5 6 ]
  -----------------------------
  Transposed matrix:
  [ 1 3 2 ]
  [ 2 4 5 ]
  </code></pre>
  <p>This is the code responsible for the transposing. It is right under the print statement with multiple dashes. Any help or insight as to why this isn't working would be great.</p>
  <pre><code>for (i = 0; i &lt; columns; i++){
          for (j = 0; j &lt; i; j++) {
              int *transposedRow = &amp;array[0][0] + (i * columns + j);
              int *transposedCol = &amp;array[0][0] + (j * columns + i);
              int temp;
              temp = *transposedRow;
              *transposedRow = *transposedCol;
              *transposedCol = temp;
          }
      }
      printf(&quot;Transposed matrix:\n&quot;);
      for (i = 0; i &lt; columns; i++) {
          for (j = 0; j &lt; rows; j++) {
              if (j == 0)
                  printf(&quot;[ &quot;);
              printf(&quot;%d &quot;, array[i][j]);
              if (j == rows - 1)
                  printf(&quot;]\n&quot;);
          }
      } 
  </code></pre>