如何同步两个数组

How to sync two arrays

本文关键字:数组 两个 同步 何同步      更新时间:2023-10-16

我有两个大小相同的数组,它们有不同的类型需要同步。其中一个数组是我按降序排序的双打列表。另一个是与它们各自的ASCII值相对应的字符33-90的列表。在对doubles列表进行排序之前,2个数组排列得很好,但我不能用同样的方式对字符列表进行排序。我需要做的是同步位置,例如:y[i] = ch[i];,但该命令不起作用。

void sortChars(int x[], double y[])
{
    int i;
    int largestIndex;
    int location;
    double temp;
    char ch[58];
    for (int i = 0; i < 58; i++)
        ch[i] = 33 + i;
    for (i = 0; i < 58; i++)
    {
        //Step a
        largestIndex = i;
        for (location = i + 1; location < 58; location++)
            if (x[location] > x[largestIndex])
                largestIndex = location;
            //Step b
        temp = x[largestIndex];
        x[largestIndex] = x[i];
        x[i] = temp;
    }
    for (i = 0; i < 58; i++)
    {
        //Step a
        largestIndex = i;
        for (location = i + 1; location < 58; location++)
            if (y[location] > y[largestIndex])
                largestIndex = location;
            //Step b
        temp = y[largestIndex];
        y[largestIndex] = y[i];
        y[i] = temp;
    }
    for (int i = 0; i < 58; i++)
    {
        cout << ch[i] << " times used: " << left << setw(20) << x[i];
        cout << fixed << showpoint << setprecision(6);
        cout << 't' << "% of file: " << y[i] << endl;
    }
}

我尝试设置ch[i] = y[i],我尝试使用y[]的选择排序,并将步骤b中的所有y部分更改为ch。你将如何匹配这两个数组?请尽量不要使用std::类型的命令,因为我的所有项目都使用using namespace std;

看起来您打算在第一个for循环中嵌套第二个和第三个for循环。像这样:

for (int i = 0; i < 58; i++)
{
    ch[i] = 33 + i;
  for (i = 0; i < 58; i++)
  {
    //Step a
    largestIndex = i;
    for (location = i + 1; location < 58; location++)
        if (x[location] > x[largestIndex])
            largestIndex = location;
        //Step b
    temp = x[largestIndex];
    x[largestIndex] = x[i];
    x[i] = temp;
  }
  for (i = 0; i < 58; i++)
  {
    //Step a
    largestIndex = i;
    for (location = i + 1; location < 58; location++)
        if (y[location] > y[largestIndex])
            largestIndex = location;
        //Step b
    temp = y[largestIndex];
    y[largestIndex] = y[i];
    y[i] = temp;
  }
  for (int i = 0; i < 58; i++)
  {
    cout << ch[i] << " times used: " << left << setw(20) << x[i];
    cout << fixed << showpoint << setprecision(6);
    cout << 't' << "% of file: " << y[i] << endl;
  }
}