Given a real matrix of size (n, m), arrange (rearrange) the rows of the matrix, by not decreasing the values of the first elements of the rows using simple selection. Here's what happened:
#include "stdafx.h" #include <iostream> #include <conio.h> #include <ctime> #include <stdlib.h> using namespace std; void main() { srand(time(NULL)); setlocale(LC_ALL, "rus"); int **a = NULL; int n, m, x = 0, y; cout << "Введите число строк массива: "; cin >> n; cout << "\n"; cout << "Введите число столбцов массива: "; cin >> m; cout << "\n"; if (n <= 0 || m <= 0) { do { cout << "Невозможно создать массив нулевой размерности. Введите другой размер массива \n"; cout << "Введите число строк массива: "; cin >> n; cout << "\n"; cout << "Введите число столбцов массива: "; cin >> m; cout << "\n"; } while (n <= 0 || m <= 0); } try { a = new int*[n]; for (int i = 0; i < n; i++) a[i] = new int[n]; } catch (...) { cout << "Ошибка! Недостаточно памяти! \n"; system("pause"); return; } cout << "Заполним массив случайныйм образом.. \n"; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = -5 + rand() % 10;// ввод элементов //------------------------------------------------------ cout << "Был введен следующий массив : \n"; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << a[i][j] << "\t"; cout << "\n"; } cout << "\n"; ---------------------------------------------------------------------------- int b,i,j,k,min; не работает правильно сортировка простым выбором! for (i=0; i<n-1; i++){ min=i; for (k=0; k<n-1; k++){ if (a[min][0]>a[min+1][0]){ min=k; for (j=0; j<m; j++){ b=a[min][j]; a[min][j]=a[min+1][j]; a[min+1][j]=b; } } } } ---------------------------------------------------------------------------- cout << "Отсортированный массив : \n"; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << a[i][j] << "\t"; cout << "\n"; } for (int i = 0; i < n; i++) delete[] a[i] ; delete[] a; system("pause"); } This is the code for sorting strings by simply selecting a two-dimensional array by the first element of each row. It works and rearranges the rows, but does not sort by the first column of the matrix. I do not know why it does not work, I did a lot of manipulations with the code, but it did not help.