按行的总和对矩阵进行排序

问题描述 投票:-2回答:2

嘿,我是编程新手,所以您可以帮我完成我的程序吗?我必须按行总和对矩阵进行排序,小数总和必须是第一个,较大的数,最后必须是最大的和。我已经做到了,但是我做不完:

#include <iostream>

using namespace std;

int main ()
{
  int **matrix;

  int i, j, count, row, col, sum, temp;

  cout << "\n Enter the number of rows and columns";
  cin >> row >> col;

  matrix = new int *[row];

  for (count = 0; count < row; count++)
      matrix[count] = new int[col];

  cout << "\nNow enter the element for the matrix.";

  for (i = 0; i < row; i++)
  {
      for (j = 0; j < col; j++)
      {
      cout << "\nRow " << (i + 1) << " Col " << (j + 1) << " :";
      cin >> *(*(matrix + i) + j);
      }
  }

  for (i = 0; i < row; i++)
  {
      sum = 0;
      for (j = 0; j < col; j++)
      sum = sum + matrix[i][j];
      cout << sum << endl;
  }

  for (int count = 0; count < row; count++)
      delete[]matrix[count];

  delete[]matrix;

  matrix = 0;

  return 0;
}
c++ matrix sum rows
2个回答
-1
投票

创建一些助手功能

#include <iostream>
#include <algorithm>
using namespace std;

int sum(int *array, const int &size)
{
  int a = array[0];
  for (int i = 1; (i < size); ++i) a += array[i];
  return a;
}

void sort(int **matrix, const int &row, const int &col, const bool &ascending=true)
{
  int *tmp = new int[col];
  for (int i = 0; i < (row - 1); ++i)
  {
    int a = sum(matrix[i], col);
    for (int j = (i + 1); (j < row); ++j)
    {
      int b = sum(matrix[j], col);
      if ((b < a) == ascending)
      {
        std::copy(&matrix[i][0], &matrix[i][col], tmp);
        std::copy(&matrix[j][0], &matrix[j][col], matrix[i]);
        std::copy(&tmp[0], &tmp[col], matrix[j]);
        a = b;
      }
    }
  }
  delete[] tmp;
}

void print(int **matrix, const int &row, const int &col)
{
  cout << "............" << endl;
  for (int i = 0; i < row; ++i)
  {
    for (int j = 0; j < col; ++j) { cout.width(4); cout << matrix[i][j]; }
    cout << endl;
  }
  cout << "............" << endl;
}

将以下行添加到主要功能] >>

int main()
{
  ...
  print(matrix, row, col);

  for (i = 0; i < row; ++i) cout << sum(matrix[i], col) << endl;

  sort(matrix, row, col);

  print(matrix, row, col);

  for (i = 0; i < row; ++i) delete[] matrix[i];
  delete[] matrix;
  matrix = nullptr;

  return 0;
}

样本输出

] >>
............
   1   2   3   4
   0   6   0   1
   1   2   1   1
   3   3   3   3
............
10
7
5
12
............
   1   2   1   1
   0   6   0   1
   1   2   3   4
   3   3   3   3
............

您可以使用标准算法std::sortstd::accumulate。下面有一个演示程序,显示了如何将这些算法一起使用。

#include <iostream>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <ctime>

int main() 
{
    const size_t M = 3, N = 5;

    std::srand( ( unsigned int )std::time( nullptr ) );

    int **matrix = new int *[M];

    for ( size_t i = 0; i < M; i++ ) matrix[i] = new int[N];

    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ ) matrix[i][j] = std::rand() % ( M * N );
    }


    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ ) std::cout << matrix[i][j]  << ' ';
        std::cout << '\n';
    }

    std::cout << '\n';

    auto sort_by_sum = [N]( const auto &left, const auto &right )
    {
        return std::accumulate( left, left + N, 0ll ) < 
               std::accumulate( right, right + N, 0ll );
    };

    std::sort( matrix, matrix + M, sort_by_sum );

    for ( size_t i = 0; i < M; i++ )
    {
        for ( size_t j = 0; j < N; j++ ) std::cout << matrix[i][j]  << ' ';
        std::cout << '\n';
    }

    std::cout << '\n';

    return 0;
}

程序输出可能看起来像

11 2 4 14 0 
9 7 9 4 14 
10 7 5 0 7 

10 7 5 0 7 
11 2 4 14 0 
9 7 9 4 14 

0
投票

您可以使用标准算法std::sortstd::accumulate。下面有一个演示程序,显示了如何将这些算法一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.