Sort the matrix rows and columns
C program to sort the matrix rows and columns.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 10
void read_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Element [%d][%d]\t:",i,j);
scanf("%d",&mat[i][j]);
}
}
}
void print_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void copy_matrix(int mat[MAX][MAX], int temp[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
temp[i][j] = mat[i][j];
}
}
}
void sort_row_ascending(int mat[MAX][MAX], int row, int col)
{
int i, j, k, temp;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
for(k=j+1;k<col;k++)
{
if(mat[i][j] > mat[i][k])
{
temp = mat[i][j];
mat[i][j] = mat[i][k];
mat[i][k] = temp;
}
}
}
}
}
void sort_row_descending(int mat[MAX][MAX], int row, int col)
{
int i, j, k, temp;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
for(k=j+1;k<col;k++)
{
if(mat[i][j] < mat[i][k])
{
temp = mat[i][j];
mat[i][j] = mat[i][k];
mat[i][k] = temp;
}
}
}
}
}
void sort_col_ascending(int mat[MAX][MAX], int row, int col)
{
int i, j, k, temp;
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
for(k=j+1;k<row;k++)
{
if(mat[j][i] > mat[k][i])
{
temp = mat[j][i];
mat[j][i] = mat[k][i];
mat[k][i] = temp;
}
}
}
}
}
void sort_col_descending(int mat[MAX][MAX], int row, int col)
{
int i, j, k, temp;
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
for(k=j+1;k<row;k++)
{
if(mat[j][i] < mat[k][i])
{
temp = mat[j][i];
mat[j][i] = mat[k][i];
mat[k][i] = temp;
}
}
}
}
}
void main()
{
int mat[MAX][MAX], temp[MAX][MAX];
int row, col;
printf("Enter the number of rows\t:");
scanf("%d",&row);
printf("Enter the number of columns\t:");
scanf("%d",&col);
printf("Enter the elements of the matrix\n");
read_matrix(mat, row, col);
printf("\nMatrix entered is\n");
print_matrix(mat, row, col);
copy_matrix(mat, temp, row, col);
sort_row_ascending(temp, row, col);
printf("\nMatrix after sorting rows in ascending order\n");
print_matrix(temp, row, col);
copy_matrix(mat, temp, row, col);
sort_row_descending(temp, row, col);
printf("\nMatrix after sorting rows in descending order\n");
print_matrix(temp, row, col);
copy_matrix(mat, temp, row, col);
sort_col_ascending(temp, row, col);
printf("\nMatrix after sorting columns in ascending order\n");
print_matrix(temp, row, col);
copy_matrix(mat, temp, row, col);
sort_col_descending(temp, row, col);
printf("\nMatrix after sorting columns in descending order\n");
print_matrix(temp, row, col);
getch();
}
Output
Enter the number of rows :3
Enter the number of columns :4
Enter the elements of the matrix
Element [0][0] :12
Element [0][1] :45
Element [0][2] :21
Element [0][3] :34
Element [1][0] :43
Element [1][1] :67
Element [1][2] :43
Element [1][3] :23
Element [2][0] :45
Element [2][1] :43
Element [2][2] :98
Element [2][3] :65
Matrix entered is
12 45 21 34
43 67 43 23
45 43 98 65
Matrix after sorting rows in ascending order
12 21 34 45
23 43 43 67
43 45 65 98
Matrix after sorting rows in descending order
45 34 21 12
67 43 43 23
98 65 45 43
Matrix after sorting columns in ascending order
12 43 21 23
43 45 43 34
45 67 98 65
Matrix after sorting columns in descending order
45 67 98 65
43 45 43 34
12 43 21 23