C++ 在c+中排序多维数组+;

C++ 在c+中排序多维数组+;,c++,arrays,sorting,C++,Arrays,Sorting,我是编程新手,我有一个问题。 我必须创建二维数组[5][3]。。。 让我们说这是其中一部分的例子: 2 3 4 7 8 9 567 然后我必须得到行的总和,并将其写在下面: 2 3 4 9 7 8 9 24 56718 现在,我必须按照这个和对数组进行排序,结果如下: // Just a type alias for the matrix. template <typename T, std::size_t R, std::size_t C> using Matrix = std::

我是编程新手,我有一个问题。 我必须创建二维数组[5][3]。。。 让我们说这是其中一部分的例子:

  • 2 3 4
  • 7 8 9
  • 567
  • 然后我必须得到行的总和,并将其写在下面:

  • 2 3 4 9
  • 7 8 9 24
  • 56718
  • 现在,我必须按照这个和对数组进行排序,结果如下:

    // Just a type alias for the matrix.
    template <typename T, std::size_t R, std::size_t C>
    using Matrix = std::array<std::array<T, C>, R>;
    
    // Create two dimensional matrix [3][3] of ints.
    Matrix<int, 3, 3> matrix = {{ { 2, 3, 4 },
                                  { 7, 8, 9 },
                                  { 5, 6, 7 } }};
    
  • 7 8 9 24
  • 56718
  • 2 3 4 9
  • 我不知道如何实现这一点,这是我的代码:

        #include <iostream>
        #include <time.h>
        using namespace std;
    
    
        void tocke(int polje[5][3])
        {
          int vsota;
           srand(time(NULL));
           int sums[5];
    for (int i = 0; i < 5; i++)
    {
        vsota = 0;
        cout << endl;
        cout << i + 1 << ". ";
    
        for (int j = 0; j < 3; j++){
            polje[i][j] = (rand() % 10 + 1);
            vsota += polje[i][j];
            sums[i] = vsota;
            cout << polje[i][j] << "  ";
        }
    }   
        }
    
        void urejaj(int polje[5][3])
        {
    cout << "\n\n\n\n" << endl;
    int sums[5];
    int vsota ;
    double temp;
    
    
    
    
    for (int i = 0; i < 5; i++)
    {
        vsota = 0;
        cout << endl;
        cout << i + 1 << ". ";
        for (int j = 0; j < 3; j++)
        {
    
            vsota += polje[i][j];
            sums[i] = vsota;
    
            if (sums[i] < sums[i+1])
            {
    
                temp = polje[i][j];
                polje[i][j] = polje[i + 1][j];
                polje[i + 1][j] = temp;
    
            }
    
            cout << polje[i][j] << " ";
    
        }cout << sums[i];
    
        }
    
        }
    
    
    
    
    
    
    
    
    
    
    
       int main()
        {
    int polje[5][3];
    tocke(polje);
    urejaj(polje);
    cout << "\n";
    system("pause");
    return 0;
    
    
       }
    
    #包括
    #包括
    使用名称空间std;
    void tocke(int polje[5][3])
    {
    int-vsota;
    srand(时间(空));
    整数和[5];
    对于(int i=0;i<5;i++)
    {
    vsota=0;
    
    cout以下是我如何处理这个问题:

    编写一个交换函数,在多维数组中切换两行。类似于:

    // columns is the number of columns; ie array[rows][columns]
    void swap(int[][] array, const int columns, int switch1, int switch2)
    {
        for (int i = 0; i < columns; i++)
            array[switch1][i] ^= array[switch2][i] ^= array[switch1][i] ^ array[switch2][i];
    }
    
    int sumCol(int[][] array, int columns, int index)
    {
        int sum = 0;
        for (int i = 0; i < columns; i++)
            sum += array[index][i];
        return sum;
    }
    
    //columns是列数;即数组[行][列]
    无效交换(int[][]数组、常量int列、int开关1、int开关2)
    {
    对于(int i=0;i
    然后,编写一个求和函数,将行中的所有元素相加。类似于:

    // columns is the number of columns; ie array[rows][columns]
    void swap(int[][] array, const int columns, int switch1, int switch2)
    {
        for (int i = 0; i < columns; i++)
            array[switch1][i] ^= array[switch2][i] ^= array[switch1][i] ^ array[switch2][i];
    }
    
    int sumCol(int[][] array, int columns, int index)
    {
        int sum = 0;
        for (int i = 0; i < columns; i++)
            sum += array[index][i];
        return sum;
    }
    
    int-sumCol(int[][]数组、int列、int索引)
    {
    整数和=0;
    对于(int i=0;i
    最后,编写一个排序函数,该函数能够使用以下操作对多维数组进行排序:

    • 比较两行的总和
    • 交换两行

    我将根据您的喜好决定使用哪种排序算法。

    通过更改数据结构,您的问题可能会更容易解决。与其使用数组,不如使用结构数组。该结构将包含总和和值数组

    struct Row
    {
      int sum;
      std::vector<int> values;
    };
    
    Row data[5];
    

    编辑1--重载运算符如果您仍然丢失,下面是完整的程序:

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    void display_matrix(int**, int, int);
    void gen_matrix(int**, int, int);
    void gen_matrix_sum(int**, int**, int, int);
    void sort_matrix(int**, int**, int, int);
    
    int main()
    {
        srand(time(0));
        int m=5, n=3;
    
        int** my_matrix = (int**)malloc(m*sizeof(int*));
        for (int i=0; i<m; i++) my_matrix[i] = (int*)malloc(n*sizeof(int));
        gen_matrix(my_matrix, m, n);
        display_matrix(my_matrix, m, n);
        cout << endl;
    
        int** my_matrix_sum = (int**)malloc(m*sizeof(int*));
        for (int i=0; i<m; i++) my_matrix_sum[i] = (int*)malloc((n+1)*sizeof(int));
        gen_matrix_sum(my_matrix_sum, my_matrix, m, n);
        display_matrix(my_matrix_sum, m, n+1);
        cout << endl;
    
        int** my_matrix_sorted = (int**)malloc(m*sizeof(int*));
        for (int i=0; i<m; i++) my_matrix_sorted[i] = (int*)malloc((n+1)*sizeof(int));
        sort_matrix(my_matrix_sorted, my_matrix_sum, m, n);
        display_matrix(my_matrix_sorted, m, n+1);   
        cout << endl;
    }
    
    void display_matrix(int** my_matrix, int m, int n)
    {
        for (int i=0; i<m; i++)
        {
            for (int j=0; j<n; j++)
                cout << setw(2) << my_matrix[i][j] << " ";
            cout << endl;
        }
    }
    
    void gen_matrix(int** M, int m, int n)
    {
        int random_limit = 10;
    
        for (int i=0; i<m; i++)
            for (int j=0; j<n; j++)
                M[i][j] = rand()%random_limit + 1;
    }
    
    void gen_matrix_sum(int** M, int** my_matrix, int m, int n)
    {
        int aux[m];
    
        for (int i=0; i<m; i++)
            aux[i] = 0;
    
        for (int i=0; i<m; i++)
            for (int j=0; j<n; j++)
            {
                M[i][j] = my_matrix[i][j];
                aux[i] += M[i][j];
            }
    
        for (int i=0; i<m; i++)
            M[i][n] = aux[i];   
    }
    
    void sort_matrix(int** my_matrix_sorted, int** my_matrix, int m, int n)
    {
        int v_sum_values[m];
    
        for (int i=0; i<m; i++)
            v_sum_values[i] = my_matrix[i][n];
    
        int v[n];   
        int max = v_sum_values[0];
        int index;
        for (int i=0; i<m; i++)
        {
    
            for (int j=0; j<m; j++)
            {
                if (v_sum_values[j]>max)
                {
                    max = v_sum_values[j];
                    index = j;
                }
            }
            v_sum_values[index] = -1;
            v[i] = index;   
            max = v_sum_values[i];
        }
    
        for (int i=0; i<m; i++)
            for (int j=0; j<n+1; j++)
                my_matrix_sorted[i][j] = my_matrix[v[i]][j]; 
    }
    
    #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    无效显示矩阵(int**,int,int);
    void gen_矩阵(int**,int,int);
    无效生成矩阵和(int**,int**,int,int);
    无效排序矩阵(int**,int**,int,int);
    int main()
    {
    srand(时间(0));
    int m=5,n=3;
    int**my_矩阵=(int**)malloc(m*sizeof(int*);
    对于(int i=0;i
    我必须创建二维数组

    我不知道你这样说是什么意思(你必须从一些输入中读取它,还是用数字等填充它?),但假设你创建一个二维矩阵,如下所示:

    // Just a type alias for the matrix.
    template <typename T, std::size_t R, std::size_t C>
    using Matrix = std::array<std::array<T, C>, R>;
    
    // Create two dimensional matrix [3][3] of ints.
    Matrix<int, 3, 3> matrix = {{ { 2, 3, 4 },
                                  { 7, 8, 9 },
                                  { 5, 6, 7 } }};
    
    就这样

    现在让我们输出矩阵:

    for (decltype(matrix.size()) row = 0; row != matrix.size(); ++row) {
        std::cout << row + 1 << ". ";
        for (const auto i : matrix[row]) {
            std::cout << i << " ";
        }
        std::cout << std::accumulate(std::begin(matrix[row]), std::end(matrix[row]), 0);
        std::cout << std::endl;
    }
    
    for(decltype(matrix.size())行=0;行!=matrix.size();++row){
    
    std::cout求和后,尝试将数组复制到新数组,但顺序正确。您可以制作一个函数,比较两个多维数组,然后比较前两个,然后比较前两个和第三个数组的结果?“OOPs”又做了一次;)+1.代替
    Compare_Rows
    ,我们可以有一个重载的`运算符sum
    留给O.P.计算。
    成员也应该由O.P.填写。在内置数组上使用非成员
    开始
    结束
    。使用
     std::sort(std::begin(数据),std::end(数据))
    ,参数变得更通用,可以在以后的阶段更改容器而不更改此代码。这非常有用,但它必须更类似于我的代码,因为它适用于学校,练习规则是,它必须有两个函数,正如我在代码中写的:)谢谢你无论如何这很有帮助,但是它必须更类似于我的代码,因为它是为了学校,锻炼的规则是,它必须有两个功能,就像我在代码中写的:)谢谢你,不管怎样,这非常有用,但它必须更类似于我的代码,因为它是为了学校,锻炼的规则是,它必须有两个功能,就像我在代码中写的:)th不管怎样,我还是要谢谢你
    for (decltype(matrix.size()) row = 0; row != matrix.size(); ++row) {
        std::cout << row + 1 << ". ";
        for (const auto i : matrix[row]) {
            std::cout << i << " ";
        }
        std::cout << std::accumulate(std::begin(matrix[row]), std::end(matrix[row]), 0);
        std::cout << std::endl;
    }