【发布时间】:2021-03-29 22:08:07
【问题描述】:
是的,我写了一个程序,它根据每行中所有正偶数元素的总和对二维数组的行进行升序排序,但它不能正常工作。有时它可以正确交换行,但大多数情况下感觉这个程序只交换两个相邻的行或类似的东西。
可能存在对二维数组错误使用冒泡排序的问题。这是我做最多事情的函数:
void print(int** arr, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << arr[i][j];
}
cout << endl;
}
int* sumRows = new int[rows];
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (arr[i][j] > 0 && arr[i][j] % 2 == 0)
{
//cout << setw(7) << arr[i][j];
sum = sum + arr[i][j];
}
}
cout << endl << "Sum of positive even elements in the row " << i + 1 << " = " << sum;
sumRows[i] = sum;
sum = 0;
}
cout << endl << "Array of sums: ";
for (int i = 0; i < rows; i++) {
cout << setw(7) << sumRows[i];
}
//for (int i = 0; i < r; i++) cout << setw(7) << sumRows[i];
cout << endl;
bool swapped;
for (int i = 0; i < rows - 1; i++)
{
swapped = false;
for (int j = 0; j < columns - 1; j++)
{
for (int k = 0; k < rows - i - 1; k++) {
if (sumRows[k] > sumRows[k + 1])
{
swap(arr[k][j], arr[k + 1][j]);
swapped = true;
}
}
}
if (swapped == false) break;
}
cout << endl << endl << "Swapped array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << arr[i][j];
}
cout << endl;
}
}
完整代码:
#include <iostream>
#include <iomanip>
#include <time.h>
#include <conio.h>
#include <algorithm>
using namespace std;
int** createMalloc(int, int);
int** createCalloc(int rows, int columns);
int** createNew(int rows, int columns);
void deleteNew(int** arr, int rows);
void init(int**, int, int);
void freeMemory(int**, int);
void print(int**, const int, const int);
void initPrint(int** arr, int rows, int columns);
void main() {
int rowCount, colCount;
cout << "Enter number of rows: "; cin >> rowCount;
cout << "Enter number of columns: "; cin >> colCount;
cout << " Array creation algorithm\n";
start:
cout << "Input number : \n1 for malloc\n2 for calloc\n3 for new\n";
int k;
cin >> k;
switch (k) {
case 1: {
int** a = createMalloc(rowCount, colCount);
initPrint(a, rowCount, colCount);
freeMemory(a, rowCount);
break;
}
case 2: {
int** a = createCalloc(rowCount, colCount);
initPrint(a, rowCount, colCount);
freeMemory(a, rowCount);
break;
}
case 3: {
int** a = createNew(rowCount, colCount);
initPrint(a, rowCount, colCount);
deleteNew(a, rowCount);
break;
}
default:cout << "Input 1, 2 or 3, please.";
cout << endl << endl;
goto start;
}
cout << endl << endl;
}
int** createMalloc(int rows, int columns) {
int** arr = (int**)malloc(rows * sizeof(int*));
for (int i = 0; i < rows; i++) {
arr[i] = (int*)malloc(columns * sizeof(int));
}
return arr;
}
int** createCalloc(int rows, int columns) {
int** arr = (int**)calloc(rows, sizeof(int*));
for (int i = 0; i < rows; i++) {
arr[i] = (int*)calloc(columns, sizeof(int));
}
return arr;
}
int** createNew(int rows, int columns) {
int** arr = new int* [rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[columns];
}
return arr;
}
void initPrint(int** arr, int rows, int columns) {
init(arr, rows, columns);
print(arr, rows, columns);
}
void init(int** arr, int rows, int columns) {
const int Low = -10, High = 10;
srand((unsigned)time(NULL));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
arr[i][j] = Low + rand() % (High - Low + 1);
}
}
}
void freeMemory(int** arr, int rows) {
for (int i = 0; i < rows; i++) {
free(arr[i]);
}
free(arr);
}
void deleteNew(int** arr, int rows) {
for (int i = 0; i < rows; i++) {
delete[] arr[i];
}
delete[] arr;
}
void print(int** arr, int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << arr[i][j];
}
cout << endl;
}
int* sumRows = new int[rows];
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (arr[i][j] > 0 && arr[i][j] % 2 == 0)
{
//cout << setw(7) << arr[i][j];
sum = sum + arr[i][j];
}
}
cout << endl << "Sum of positive even elements in the row " << i + 1 << " = " << sum;
sumRows[i] = sum;
sum = 0;
}
cout << endl << "Array of sums: ";
for (int i = 0; i < rows; i++) {
cout << setw(7) << sumRows[i];
}
//for (int i = 0; i < r; i++) cout << setw(7) << sumRows[i];
cout << endl;
bool swapped;
for (int i = 0; i < rows - 1; i++)
{
swapped = false;
for (int j = 0; j < columns - 1; j++)
{
for (int k = 0; k < rows - i - 1; k++) {
if (sumRows[k] > sumRows[k + 1])
{
swap(arr[k][j], arr[k + 1][j]);
swapped = true;
}
}
}
//IF no two elements were swapped by inner loop, then break
if (swapped == false) break;
}
cout << endl << endl << "Swapped array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << setw(7) << arr[i][j];
}
cout << endl;
}
}
附:二维数组必须是动态数组。此外,我需要让用户选择使用malloc、calloc 或new 创建数组。另外,还不能使用vector 来完成这项任务。
P.P.S.我知道你们中的大多数人会觉得这很容易,但对我来说绝对不是,因为这项任务是我在大学的家庭作业,我们还没有学习任何排序算法。无论如何,当我问老师我如何对行进行排序时,他告诉我使用冒泡排序,所以我在这里
【问题讨论】:
-
你对被教 c++ 却不被允许使用深表同情……即使重点是教你资源管理,一个真正的 C++ 程序也会使用算法。无论如何:将您的逻辑拆分为函数,这将使推理和发现问题变得更加容易:)
-
最起码的广告:求和元素本身就是一项任务,所以它应该是它自己的功能。排序也一样。
-
如果练习的目的不是为了避免
std::vector,那么就使用它。如果是,那就自己写吧。手动内存管理不是你应该分散在代码中的东西,它应该封装在一个处理它的类中(而不是别的) -
交换两行时,还需要交换
sumRows中对应的值。否则,总和不再对应行中的实际值。 -
您不需要逐列交换两行。只需简单
std::swap(arr[k], arr[k+1])
标签: c++ arrays multidimensional-array bubble-sort