【发布时间】:2020-12-05 21:59:36
【问题描述】:
我有一个二维字符数组,我正在尝试按字母顺序对它们进行排序。在每一行中都有一个字符组成的单词,我正在尝试对其进行排序。 我做了一些东西,但我不明白为什么这不起作用。如果您对我有解决方案,请解释您在做什么,以了解我为什么不成功。 谢谢!
char matrix[4][5] = {
{'h','e','l','l','o'},
{'r','e','a','d','y'},
{'a','p','p','l','e'},
{'p','o','i','n','t'},
};
char temp;
bool flag = false;
display(matrix);
for (int i = 0; i < 4 - 1; i++)
{
for (int rows = 0; rows < 10-1; rows++)
{
flag = false;
for (int cols = 0; cols < 5; cols++)
{
if (matrix[rows][cols] > matrix[rows + 1][cols])
{
flag = true;
break;
}
}
if (flag)
{
for (int index = 0; index < 5; index++)
{
temp=matrix[rows][index];
matrix[rows][index]=matrix[rows+1][index];
matrix[rows+1][index]=temp;
}
}
}
}
【问题讨论】:
-
是否允许使用另一个数组对矩阵进行“排序”?有一些方法可以在不实际对 2D 数组进行排序的情况下做你想做的事情。