排序程序的错误代码

Error code with sorting program

本文关键字:错误代码 程序 排序      更新时间:2023-10-16

我目前正在尝试编写一个按字符串字母顺序排列的程序。我有一个错误。minLocation不接受2个参数。我对编程还很陌生,有人能告诉我为什么我的这部分代码是错误的吗?

int minLocation(string list[], int first, int last)
{
  int mIndex=first;
  int loc = 0;
    for (loc = first+1; loc <= last; loc++)
        if (list[loc] < list [mIndex])
            mIndex = loc;
    return mIndex;

void Sort(string slist[],int length)
{
   int mIndex;
       for (int loc = 0; loc < length-1; loc++)
       {
           mIndex = minLocation (loc,length-1);
           swap (loc, minIndex);
       }
}

如果没有看到minLocation的定义,我们就无法判断。但可以肯定的是,它不需要两个论点——编译器不会为了好玩而对你撒谎,你通常可以假设他们说的是真的:-)

你需要找到定义,比如:

int minLocation (int loc) { ...

并弄清楚实际上是如何调用它的。考虑到它似乎在试图找出两个索引中哪一个的值更低,它可能需要比两个参数更多的

除此之外,您还需要决定是想要名为mIndex还是minIndex的变量。大多数编译器都不够聪明,无法为您解决这个问题。

根据您的编辑,muinFunction定义为:

int minLocation (string list[], int first, int last) { ...

很明显,它还需要字符串数组以及两个索引。您需要将呼叫更改为:

mIndex = minLocation (slist, loc, length-1);

并密切关注swap呼叫。根据编码风格,它可能有类似的要求。