#include <stdio.h> #include <conio.h> int main() { int i, j; int m, n; int x, y; printf("\nEnter number of rows matrix A \n");//Введите количество строк матрицы А column scanf("%d", &m); printf("\nEnter number of columns matrix A \n");//Введите количество столбцов матрицы А scanf("%d", &n); float A[m][n]; printf("\nEnter matrix A %dx %d elements \n",m,n);//Bведите матрицу A for (i=0;i<m; i++) { printf("\nElements of %d row: \n",i+1);//Элементы %d-й строки for (j=0; j<n; j++) { printf("Elem A [%d][%d]", i+1, j+1); scanf("%f", &A[i][j]); } } printf("\nMatrix A: \n");//Матрица А for(i=0; i<m; i++) { for(j=0; j<n; j++) { printf("%f\t",A[i][j]); } printf("\n"); } for(y=0; y<m; y++) { for (i=0; i<(m-1); i++) { if(A[i][0]>A[i+1][0]) { for(j=0; j<n; j++) { x=A[i][j]; A[i][j]=A[i+1][j]; A[i+1][j]=x; } } } } printf("\n"); printf("\nSorted matrix A: \n");//Сортированная матрица for(i=0; i<m; i++) { for(j=0; j<n; j++) { printf("%f\t",A[i][j]); } printf("\n"); } printf("\n"); } The task is to order the matrix in ascending order of values in rows by columns. If there are matching values in the column, then move to the next column and order by it, etc. For example, if the original
1 7 3 4 1 2 4 3 1 2 1 4 it should work
1 2 1 4 1 2 4 3 1 7 3 4 I rummaged through many similar topics, there are similar bubble sortings everywhere, like mine.
But the problem is that if the matrix, for example, 5 x 5 and it has all the numbers in the first, second and third columns, then it does not sort by the fourth and fifth. I checked both on the matrix easier and less, and it turned out that it sorts only by the first column. How can you change this code so that the sorting of the rows is checked for matching and takes place across all columns