Sorting a two-dimensional dynamic array by c++strings

Created a dynamic array in c++, filled it in. Now you need to sort by the first element of the rows. That is, so that the row with the largest first element is the first. When trying to do the sorting, it constantly gives an error. As I understood, the memory is overflowing. The reason I did not find out, please help.

#include <iostream>
using namespace std;

int main()
{
    int rows =4;
    int cols = 4;
    int** arr = new int* [rows];
    for (int i = 0; i < rows; i++)
    {
        arr[i] = new int[cols];
    }
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cin >> arr[i][j];
        }
        for (int i = 0; i < rows; i++)
        {
            int j = 0;
                if (arr[i][j] < arr[i + 1][j])
                {
                    arr[i] = arr[i + 1];
                }
        }
    }
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << "\t";
        }
        cout << endl;

    }
    for (int i = 0; i < rows; i++)
    {
        delete[] arr[i];
    }
    delete[] arr;

}
Author: Harry, 2020-05-04

1 answers

You do not have any sorting, there are completely incomprehensible gestures.
Just to close this question, keep a ready-made sorting program. I have it clogs the array with random numbers, read from the console uncomment yourself ...

#include <iostream>
using namespace std;

void swap_rows(int ** a, int r1, int r2)
{
    int * b = a[r1];
    a[r1] = a[r2];
    a[r2] = b;
}

int main()
{
    int rows = 4;
    int cols = 4;
    int** arr = new int* [rows];

    for (int i = 0; i < rows; i++)
        arr[i] = new int[cols];

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            arr[i][j] = rand()%100;

            // cin >> arr[i][j];

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << arr[i][j] << "\t";
        cout << endl;
    }


    for(int r = 0; r < rows-1; ++r)
    {
        // Поиск наибольшего в первом столбце
        int m = arr[r][0], idx = r;
        for(int i = r; i < rows; ++i)
            if (arr[i][0] > m) {
                m = arr[i][0];
                idx = i;
            }
        // Обмен
        swap_rows(arr,r,idx);
    }

    cout << endl << endl;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            cout << arr[i][j] << "\t";
        cout << endl;
    }


    for (int i = 0; i < rows; i++)
    {
        delete[] arr[i];
    }

    delete[] arr;
}
 1
Author: Harry, 2020-05-04 18:14:33