Skip to content

Instantly share code, notes, and snippets.

@jamesgeorge007
Created March 24, 2018 04:52
Show Gist options
  • Save jamesgeorge007/e654bf112a22522e928b872a6a93fa4a to your computer and use it in GitHub Desktop.
Save jamesgeorge007/e654bf112a22522e928b872a6a93fa4a to your computer and use it in GitHub Desktop.
Sorts a matrix row-wise of order m by n.
#include<stdio.h>
int main()
{
int arr[20][20], i, j, k, m, n, temp;
printf("Number of rows and columns:\n");
scanf("%d%d", &m, &n);
printf("\n\nInput elements to the matrix:\n\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
scanf("%d", &arr[i][j]);
}
printf("\n\nPrinting out the Matrix of order %d x %d.\n\n", m, n);
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%d\t", arr[i][j]);
printf("\n\n");
}
for(i=0;i<m;i++){
for(j=0;j<n;j++)
{
for(k=0;k<n;k++){
if(arr[i][j] < arr[i][k]){
temp = arr[i][j];
arr[i][j] = arr[i][k];
arr[i][k] = temp;
}
}
}
}
printf("\n\nRow wise sorted Matrix is:\n\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
printf("%d\t", arr[i][j]);
printf("\n\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment