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


March 20, 2022, Learn eTutorial
1792

What is a matrix?

A matrix is a collection of elements of the same data type. It is a two-dimensional array, where 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

How to implement sorting of the matrix in a C program?

In this C program, we need to sort rows & columns according to descending and ascending order.  For that we declare the Matrix ma[][], mb[][] 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 its elements by using nested for loop. Display it using nested for loop. Then for arranging the row elements in ascending order and column elements in descending order, we has 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 using the temp variable. Finally, we get the matrix sorted. Then we display the sorted matrix. 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 an integer.

STEP 3: Read the order 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 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 mb[i][j] using nested For loop.


To implement the array sorting we are using the below C concepts, please refer to these topics for a better understanding

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