title: Sorting 2d array (matrix) in C
body: <p>I have a task to sort a 2d array but it is really hard for me. I can't figure out where I'm making a mistake.</p>
  
  <p><strong>It will be good to use while loop instead of for loop</strong></p>
  
  <p>Here is the task: </p>
  
  <ol>
  <li>Enter a square matrix of dimensions n.</li>
  <li>Elements below main diagonal sort in ascending order.</li>
  <li>Elements above main diagonal sort in descending order.</li>
  <li>Elements on main diagonal sort:
  
  <ul>
  <li>first even numbers in ascending order.</li>
  <li>then odd numbers in descending order.</li>
  </ul></li>
  </ol>
  
  <p>Matrix before sorting:</p>
  
  <pre><code>1  5  4  7  2
  
  4  8  5  9  0
  
  2  7  6  5  3
  
  3  1  7  4  9
  
  2  5  1  7  3
  </code></pre>
  
  <p>Matrix after sorting :</p>
  
  <pre><code>4  9  9  7  5
  
  1  6  5  5  4 
  
  1  2  8  3  2 
  
  2  3  4  3  0
  
  5  7  7  7  1
  </code></pre>
  
  <p>Here is the code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main () {
  
      int n, i, j, a, k, l, o;
      int m[50][50];
  
      printf ("Enter n of square matrix : \n");
      scanf ("%d", &amp;n);
  
      printf ("Enter rows and columns numbers n :\n");
  
      i = 0;
      while (i &lt; n) {
          j = 0;
          while (j &lt; n) {
              scanf ("%d", &amp;m[i][j]);
              j++;
          }
          i++;
      }
      printf ("Matrix before sorting \n");
      i = 0;
      while (i &lt; n) {
          j = 0;
          while (j &lt; n) {
              printf ("%d  ", m[i][j]);
              j++;
          }
          printf ("\n");
          i++;
      }
      printf ("Matrix after sorting \n");
  
      i = 0;
      while (i &lt; n) {
          j = 0;
          while (j &lt; n) {
              if (i &lt; j) {
                  k = j + 1;
                  while (k &lt; (n - 1)) {
                      if (m[i][k] &lt; m[i][k + 1]) {
                          a = m[i][k];
                          m[i][k] = m[i][k + 1];
                          m[i][k + 1] = a;
                      }
                      k++;
                  }
              } 
              else if (i &gt; j) {
                  l = i + 1;
                  while (l &lt; (n - 1)) {
                      if (m[l][j] &gt; m[l + 1][j]) {
                          a = m[l][j];
                          m[l][j] = m[l + 1][j];
                          m[l + 1][j] = a;
                      }
                      l++;
                  }
              } 
              else {
                  if (m[i][j] % 2 == 0 &amp;&amp; m[i + 1][j + 1] % 2 == 0) {
                      if (m[i][j] &gt; m[i + 1][j + 1]) {
                          a = m[i][j];
                          m[i][j] = m[i + 1][j + 1];
                          m[i + 1][j + 1] = a;
                      }
                  }
                  if (m[i][j] % 2 != 0 &amp;&amp; m[i + 1][j + 1] % 2 != 0) {
                      if (m[i][j] &lt; m[i + 1][j + 1]) {
                          a = m[i][j];
                          m[i][j] = m[i + 1][j + 1];
                          m[i + 1][j + 1] = a;
                      }
                  }
              }
              j++;
          }
          i++;
      }
  
      i = 0;
      while (i &lt; n) {
          j = 0;
          while (j &lt; n) {
              printf ("%d  ", m[i][j]);
              j++;
          }
          printf ("\n");
          i++;
      }
  
      return 0;
  }
  </code></pre>
  
  <p><strong>CHANGES MADE ON 02/02/2018</strong> </p>
  
  <p>I need to make simple code , without things like size_t,buffer and pointers etc...</p>
  
  <p>I made some code but when i use odd n number of matrix it wont work properly (for example 5x5) I don't know where i m making mistake .</p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include &lt;stdlib.h&gt;
  
   int main()
   {
  static int a[500][500];
  int i,j,l,k,m,b,n,t,d,c;
  
  printf("Unesite n kvadratne matrice : \n");
  scanf("%d" , &amp;n);
  printf("Matrica pre sortiranja \n \n");
  
  for ( i = 0; i &lt; n; i++)
  {
      for ( j = 0; j &lt; n ; j++)
      {
          scanf("%d",&amp;a[ i ][ j ]);
      }
  }
  
  
  for ( i = 0; i &lt; n; i++)
  {
      for ( j = 0; j &lt; n; j++)
      {
          if ( i &gt; j )  // ispod dijagonale
          {
                  for ( l = i ; l &lt; n ; l++)
              {
                  for ( k = j ; k &lt; l  ; k++ )
                  {
                      if ( a[ i ][ j ] &gt; a[ l ][ k ] &amp;&amp; k  &lt; l )
                      {
                      t = a[ i ][ j ];
                      a[ i ][ j ] = a[ l ][ k ];
                      a[ l ][ k ] = t ;
                      }
                  }
              }
          }
          if ( i &lt; j ) // iznad dijagonale
          {
              for ( m = i ; m &lt; n  ; m++)
              {
                  for ( b = j ; b &lt; n ; b++)
                  {
                      if ( a[ i ][ j ] &lt; a[ m ][ b ] &amp;&amp; m &lt; b )
                      {
                      t = a[ i ][ j ];
                      a[ i ][ j ] = a[ m ][ b ];
                      a[ m ][ b ] = t ;
                      }
                  }
              }
          }
          if ( i == j ) // dijagonala
          {
              for (d = i ; d &lt; n ; d++)
                  {
                  for ( c = d + 1 ; c &lt; n ; c++)
                      {
                      if ( a[ d ] [ d ] % 2 != 0 &amp;&amp; a[ c ] [ c]%2 == 0 )
                          {
                          t = a[ d ] [ d ] ;
                           a[ d ] [ d ] = a[ c ] [ c] ;
                           a[ c ] [ c] = t ;
                          }
                      }
                  }
                  for (d = i ; d &lt; n ; d++)
                      {
                      for ( c = d + 1 ; c &lt; n ; c++)
                          {
                               if ( a[ d ][ d ] %2 == 0 &amp;&amp; a[ c ][ c ] %2 ==0 
         &amp;&amp; a[ d ][ d ] &gt; a [ c ][ c ])
                              {
                              t = a[ d ] [ d ] ;
                              a[ d ] [ d ] = a[ c ] [ c] ;
                              a[ c ] [ c] = t ;
                              }
                              else if ( a[ d ][ d ] %2 != 0 &amp;&amp; a[ c ][ c ] %2 
     !=0 &amp;&amp; a[ d ][ d ] &lt; a [ c ][ c ])
                              {
                              t = a[ d ] [ d ] ;
                              a[ d ] [ d ] = a[ c ] [ c] ;
                              a[ c ] [ c] = t ;
                              }
                          }
                      }
          }
      }
    }
  
      printf("Posle sortiranja : \n");
      for ( i = 0; i &lt; n; i++)
      {
          for ( j = 0; j &lt; n;j++)
          {
          printf("    %d  ",a[i][j]);
          }
      printf("\n \n");
      }
    return 0;
   }
  </code></pre>