使用排序功能对并行数组进行排序

时间:2019-07-17 20:54:31

标签: c++ arrays sorting

我应该编写一个排序功能,将每月(用户输入的)英寸雨量按升序排序。该输出应该每月输出英寸及其对应月份的并行数组。它看起来应该类似于“ x英寸的降雨(月)”。 X和(月)是并行数组的输出。但是,当我输入几英寸的雨水时,没有输出。

我可能会忽略某些东西,但是我觉得它可能很简单,例如我以错误的顺序调用函数,或者我没有正确输出排序后的数组。我尝试过在代码中移动排序和显示功能,并且还尝试过更改无用。

#include <iostream>
#include <string>
using namespace std;

// Function Prototypes
void getMonthlyRainfall(double[], string[], int);
double getTotal(const double[], int);
int getHighestAmount(const double[], int);
int getLowestAmount(const double[], int);
void dualSort(double[], string[], int);
void swap(double&, double&);
void swap(string&, string&);
void showOrder(const double[], string[], int);

int main()
{
    const int MONTHS = 12;
    string monthNames[MONTHS] = { "January", "February", "March", 
"April", "May", "June", "July", "August", "September", "October", 
"November", "December" };
    double rainfall[MONTHS], // Array
        total,
        average;
    int lowestIndex,
        highestIndex;

    //Get rainfall input from user
    getMonthlyRainfall(rainfall, monthNames, MONTHS);

        // Get the total amount of rain for the year
    total = getTotal(rainfall, MONTHS);

    // Get the average rainfall
    average = total / MONTHS;

    // Get the month with the lowest rainfall
    lowestIndex = getLowestAmount(rainfall, MONTHS);

    // Get the month with the highest rainfall
    highestIndex = getHighestAmount(rainfall, MONTHS);



    cout << "Total rainfall: " << total << endl;
    cout << "Average rainfall: " << average << endl;
    cout << "Least rainfall in: " << monthNames[lowestIndex] << endl;
    cout<< "Most rainfall in: "<< monthNames[highestIndex] << endl;

    // Sort the array.
    dualSort(rainfall, monthNames, MONTHS);

    // Display sorted numbers
    showOrder(rainfall, monthNames, MONTHS);



    system("pause");
    return 0;
}

这是代码的主要部分,但问题特别在于showOrder和dualSort函数。我将在下面添加它们。

//*************************************************
// This function sorts an array into ascending    *
// order.                                         *
//*************************************************

void dualSort(double rainfall[], string monthNames[], int size)
{
    int start, minIndex;
    double minValue;
    string tempId;

    for (start = 0; start < (size - 1); start++); 
    {
        minIndex = start;
        minValue = rainfall[start];
        tempId = monthNames[start];

        for (int index = start + 1; index < size; index++) 
        {
        if (rainfall[index] < minValue)
        {
            minValue = rainfall[index];
            tempId = monthNames[index];
            minIndex = index;
        }
        }

    swap(rainfall[minIndex], rainfall[start]);
    swap(monthNames[minIndex], monthNames[start]);
    }
}

//*************************************************
// The swap function swaps two int's in           *
// memory.                                        *
//*************************************************

void swap(double &a, double &b)
{
double temp;

temp = a;
a = b;
b = temp;
}

//***********************************************
// The swap function swaps two strings in       *
// memory.                                      *
//***********************************************

void swap(string &a, string &b)
{
string temp;

temp = a;
a = b;
b = temp;
}

//************************************************
// showOrder function displays sorted values     *
//************************************************

void showOrder(const string monthNames[],const double rainfall[], int 
num)
{
for (int index = 0; index < num; index++) 
{
    cout << rainfall[index] << "inches in " << monthNames[index] 
 << endl;
}
cout << endl;
}

我没有收到任何编译器错误或消息,并且能够执行程序的其余部分而没有任何问题。唯一的问题是,当我期望输出以升序排列时,就没有任何输出。

编辑-我可能应该更清楚自己的代码了。选择排序的代码是直接来自我的教科书的算法,因此应该是正确的。因为此代码是基于我之前编写的代码构建的,该代码需要降雨和monthNames的并行数组,所以我需要在此实例中使用并行数组对字符串数组和双精度数组进行排序。我的代码中包含更多的代码和更多的功能,因为它们工作正常,所以未在此处发布。这些功能之一是用于并行数组以关联降雨和monthNames。

除非我使用不正确,否则调试器不会提供太多帮助。是否存在以其他方式对并行数组进行升序排序的其他方式,因为如果存在,我将无法在线或在书中找到它?在这一点上,我什至不确定问题是什么。即使在逐行讨论并尝试弄清楚它的逻辑之后,我仍然看不到这个问题。我能够输出我正确编写的其他函数,因此,除非我需要在dualSort函数中再次将两个数组相关联,或者我在某处缺少return语句,否则我不知道还要尝试什么。

1 个答案:

答案 0 :(得分:0)

您可以将引用配对,然后进行排序以对数组进行排序。

void dualSort(double rainfall[], string monthNames[], int size)
{
    std::vector<std::pair<double&, std::string&> > tied;
    tied.reserve(size);
    auto tie = [](double & rain, std::string & month){ return std::tie(rain, month); };
    std::transform(rainfall, rainfall + size, monthNames, std::back_inserter(tied), tie);
    std::sort(tied.begin(), tied.end());
}
相关问题