C Program to sort rows & columns of matrix in ascending & descending order

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this c program, we need to sort rows & columns of a matrix according to descending and ascending order. 

What is a matrix in C?

A matrix is a collection of elements of the same data type. In a two-dimensional array, we arrange the elements in a table of rows and columns. It is also called the array of arrays. An example of a two-dimensional array is shown below.

5 8
3 4

So declare the Matrix ma[][] ,b[][] and the variables,i, j, k, a, m, n. Then we have to read the Order of the Matrix into the variables m and n.

Then read the matrix elements by using nested for loop. Display the matrix using nested for loop. Then for arranging the row elements in ascending order and column elements in descending order, what we are doing inside the program is to open three nested for loop. Inside the loop, we compare each element with the next element in the matrix and replace that with each other and using the temp variable. Finally, we get the matrix sorted. Then we display the sorted matrix. In this c program, the same procedure is used in both ascending and descending orders.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the variable i, j, k, a, m, n, ma[10][10], mb[10][10] as integer .

STEP 3: Read the order of the Matrix from the user into the variables m and n.

STEP 4: Read the Coefficients of the Matrix by using for loop into the Matrix ma[i[j] and assign mb[i][j]=ma[i][j].

STEP 5: Display the given Matrix as each element of ma[i]j] using for loop.

STEP 6: Display After arranging Rows in ascending order using printf function.

STEP 7: By using nested for loop check ma[i][j]>ma[i][k] if true then interchange ma[i][j] to the postition of ma[i][k].

STEP 8: Then display each element of Matrix ma[i][j] using nested For loop.

STEP 9: Display After arranging columns in descending order using printf function.

STEP 10: By using nested For loop check mb[i][j]

STEP 11: Then display each element of Matrix mb[i][j] using nested For loop.

C Source Code

                                          #include <stdio.h>

void main()
{
  static int ma[10][10], mb[10][10];
  int i, j, k, a, m, n;
  printf("Enter the order of the matrix \n"); /* enters the order of matrix */
  scanf("%d %d", & m, & n);
  printf("Enter co-efficient of the matrix \n");
  for (i = 0; i < m; ++i)
  {
    for (j = 0; j < n; ++j)
    {
      scanf("%d", & ma[i][j]); /* prints the matrix  */
      mb[i][j] = ma[i][j];
    }
  }
  printf("The given matrix is \n");
   for (i = 0; i < m; ++i)
  {
    for (j = 0; j < n; ++j)
    {
      printf(" %d", ma[i][j]); /* print the given matrix  */
    }
    printf("\n");
  }
  printf("After arranging rows in ascending order\n");
  for (i = 0; i < m; ++i)
  {
    for (j = 0; j < n; ++j)
    {
      for (k = (j + 1); k < n; ++k)
      {
        if (ma[i][j] > ma[i][k]) /* sorting in the ascending order */
         {
          a = ma[i][j];
          ma[i][j] = ma[i][k];
          ma[i][k] = a;
        }
      }
    }
  } /* End of outer for loop*/
  for (i = 0; i < m; ++i)
  {
    for (j = 0; j < n; ++j)
    {
      printf(" %d", ma[i][j]);
    }
    printf("\n");
  }
  printf("After arranging the columns in descending order \n");
  for (j = 0; j < n; ++j)
  {
    for (i = 0; i < m; ++i)
    {
      for (k = i + 1; k < m; ++k)
      {
        if (mb[i][j] < mb[k][j])
        {
          a = mb[i][j];
          mb[i][j] = mb[k][j];
          mb[k][j] = a;
        }
      }
    }
  } /* End of outer for loop*/
  for (i = 0; i < m; ++i)
  {
  for (j = 0; j < n; ++j)
  {
    printf(" %d", mb[i][j]);
  }
  printf("\n");
 }
 } /*End of main() */
                                      

OUTPUT

Enter the order of the matrix
2*2

Enter co-efficient s of the matrix
3 1
5 2

The given matrix is
 3 1
 5 2

After arranging rows in ascending order
 1 3
 2 5

After arranging the columns in descending order
 5 2
 3 1