gpt4 book ai didi

C++ 问题冒泡排序二维字符数组

转载 作者:太空狗 更新时间:2023-10-29 23:18:36 25 4
gpt4 key购买 nike

这个问题是关于我为作业编写的代码,所以是的,我必须使用冒泡排序,是的,我必须使用数组。

好吧,我正在对一维数组进行精细的冒泡排序,但尝试对二维数组进行排序让我很伤心。

我想做的是按字母顺序对名称列表(即 Joe、Bob、Betty、Jake)进行排序。该程序使用以下代码尝试对事物进行排序(名称四处移动),但它通常只是部分正确。

#include <iostream>
#include <fstream>
using namespace std;
void AlphaSort(char [][50]);
void GetInput(char [][50]);
void PrintArray(char [][50]);

int main()
{
char name[4][50];
cout << "Please enter 4 names:\n";
GetInput(name);
cout << endl << "The following names were received:\n";
PrintArray(name);
cout << endl << "The names will now be sorted.\n\n"
<< "Calling Sort Function....\n";
AlphaSort(name);
cout << endl << "The sorted name order is now:\n";
PrintArray(name);

return 0;
}

void GetInput(char name[][50])
{
int i;

for(i=0; i<4; i++)
cin.getline(name[i], 50);
}

void PrintArray(char name[][50])
{
int i;

for(i=0; i<4; i++)
cout << name[i] << endl;
}

void AlphaSort(char name[][50]) //I'm pretty sure the problem is in here.
{
int Nnames = 4, pass, column, row, letter, sorted;
char temp[50];

cout << "Sorting Function Successfully Called...\n"
<< endl << "Names recieved by sort function:\n";

PrintArray(name);

//Specifically in this attempt at a bubble sort.
for(pass=0; pass<(Nnames-1); pass++)
{
for(row=0; row<(Nnames-pass-1); row++)
{
for(column=0, sorted=0; sorted==0; column++)
{
if(name[row][column]>name[row+1][column])
{
for(letter=0; letter<50; letter++)
temp[letter] = name[row][letter];
for(letter=0; letter<50; letter++)
name[row][letter]= name[row+1][letter];
for(letter=0; letter<50; letter++)
name[row+1][letter] = temp[letter];
sorted = 1;
}
else if (name[row][column] == name[row+1][column]);
}
}
}

cout << endl << "Name order post sort:\n";

PrintArray(name);
}

目前运行这个通常看起来像这样。

Please enter 4 names: Hammy Harry Ashe Aaron

The following names were received: Hammy Harry Ashe Aaron

The names will now be sorted.

Calling Sort Function.... Sorting Function Successfully Called...

Names recieved by sort function: Hammy Harry Ashe Aaron

Name order post sort: Aaron Ashe Harry Hammy

The sorted name order is now: Aaron Ashe Harry Hammy

我一直在寻找类似的问题,但我不明白为什么它很愚蠢。

最佳答案

我的问题似乎在于我的 if 语句如何没有解决它已经处于正确顺序的可能性,这使得它循环直到它稍后找到一个它想要切换的字母。固定代码如下所示:

void AlphaSort(char name[][50])
{
int Nnames = 4, pass, column, row, letter, sorted;
char temp[50];

cout << "Sorting Function Successfully Called...\n"
<< endl << "Names recieved by sort function:\n";

PrintArray(name);

for(pass=0; pass<(Nnames-1); pass++)
{
for(row=0; row<(Nnames-pass-1); row++)
{
for(column=0, sorted=0; sorted==0; column++)
{
if(name[row][column]>name[row+1][column])
{
for(letter=0; letter<50; letter++)
temp[letter] = name[row][letter];
for(letter=0; letter<50; letter++)
name[row][letter]= name[row+1][letter];
for(letter=0; letter<50; letter++)
name[row+1][letter] = temp[letter];
sorted = 1;
}
else if (name[row][column]<name[row+1][column])//This is the fix.
sorted = 1;
}
}
}

cout << endl << "Name order post sort:\n";

PrintArray(name);
}

关于C++ 问题冒泡排序二维字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13391289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com