# C Program to Sort Rows and Columns of the Matrix

This is a C Program to sort rows of the matrix in ascending & columns in descending order.

Problem Description

This C Program sorts the rows of the matrix in ascending & columns in descending order.

Problem Solution

This program accepts matrix. Then sorts the row in an ascending order & the columns in descending order.

Program/Source Code

Here is source code of the C program to sort rows of the matrix in ascending & columns in descending order. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C program to accept a matrics of order MxN and sort all rows of the
* matrix in ascending order and all columns in descending order
*/
#include <stdio.h>

void main()
{
static int array1[10][10], array2[10][10];
int i, j, k, a, m, n;

printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
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 (array1[i][j] > array1[i][k])
{
a = array1[i][j];
array1[i][j] = array1[i][k];
array1[i][k] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[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 (array2[i][j] < array2[k][j])
{
a = array2[i][j];
array2[i][j] = array2[k][j];
array2[k][j] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array2[i][j]);
}
printf("\n");
}
}```
Program Explanation

In this C program, we are reading the order of the matrix row and column using ‘m’ and ‘n’ variables respectively. Using for loop the coefficient elements of the matrix is assigned to the variables array1[i][j]. Then assign the value of array1[i][j] variable to array2[i][j] variable.

In this program to sort the elements of the matrix three for loops are used. In first for loop the value of variable ‘i’ is initialized to zero and checks the condition that ‘i’ variable value is smaller than the order of row defined in ‘m’ variable. In second for loop the value of variable ‘j’ is initialized to zero and check the condition that ‘j’ variable value is smaller than the order of column defined in ‘n’ variable.

The third for loop is used to sort all elements present in the rows of the matrix in ascending order. In this, for loop the value of variable ‘k’ is initialized to 0 and checks the condition that ‘k’ variable value is smaller than the order of column defined in ‘n’ variable.

If condition statement is used to sort the elements in the matrix, by checking the condition that the value of array[i][j] variable is greater than the value of array1[i][k] variable. If the condition is true, then it will execute a statement.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

The variable ‘a’ is used as a temporary variable. We are interchanging the value of array1[i][j] variable to ‘a’ variable and the value of array1[i][k] variable to array1[i][j] variable and the value of ‘a’ variable to array1[i][k] variable. Then we are displaying the rows of the matrix in ascending order.

The third for loop is used to sort all elements present in the rows of the matrix in descending order. In this, for loop the value of variable ‘k’ is initialized to 0 and checks the condition that ‘k’ variable value is smaller than the order of row defined in ‘m’ variable.

If condition statement is used to sort the elements in the matrix, by checking the condition that the value of array2[i][j] variable is greater than the value of array2[k][j] variable. If the condition is true, then it will execute the statement.

The variable ‘a’ is used as a temporary variable. We interchanging the value of array2[i][j] variable to ‘a’ variable and the value of array2[k][j] variable to array2[i][j] variable and the value of ‘a’ variable to array2[k][j] variable. Then we are displaying the rows of the matrix in descending order.

Runtime Test Cases
```
\$ cc pgm87.c
\$ a.out
Enter the order of the matrix
3 3
Enter co-efficients of the matrix
3 7 9
2 4 8
5 2 6
The given matrix is
3 7 9
2 4 8
5 2 6
After arranging rows in ascending order
3 7 9
2 4 8
2 5 6
After arranging the columns in descending order
5 7 9
3 4 8
2 2 6```

Sanfoundry Global Education & Learning Series – 1000 C Programs.