Arranging rows of a matrix.
#include<stdio.h>
int main() {
int r, c, t;
printf("Enter the no. of rows and columns : ");
scanf("%d%d",&r,&c);
int mat[r][c];
// Taking input from the user.
for (int i=0; i<r; i++){
for (int j=0; j<c; j++ ){
printf("matrix[%d][%d] = ", i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
for (int k=j+1; k<c; k++){
if (mat[i][j]>mat[i][k]){
t=mat[i][j];
mat[i][j]=mat[i][k];
mat[i][k]=t;
}
}
}
}
printf("After arranging rows...\n");
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
printf("%d ",mat[i][j]);
}printf("\n");
}
return 0;
}
No comments:
Post a Comment