连接2个数组,一个字符串数组和一个数字数组

时间:2016-03-11 14:07:32

标签: c++ arrays

所以,我有一个名为rainfall的数组,其中存储了12个月的降雨数据。还有一个月份的字符串数组(jan,feb ...)。我将不得不将降雨阵列从最高到最低排序。如何连接这2个阵列,这样我就可以在整理降雨阵列后将它们一起打印出来。

这是我的代码:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void selectionSort(double array[],int size)
{
    int startScan, minIndex, maxValue;
    for(startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        maxValue = array[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if (array[index] > maxValue)
            {
                maxValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = maxValue;
    }
}
void highestfunction(double highestrainfall[], string monthtemp[])
{
   double highest;
    highest = highestrainfall[0];
    string highestmonth = "January";
    for (int count = 1; count < 12; count++)
    {
        if (highestrainfall[count] > highest)
            {
            highest = highestrainfall[count];
            highestmonth = monthtemp[count];
            }
    }
    cout << highestmonth << " has highest amount of rainfall: " << highest << " ml" << endl;
}
void lowestfunction(double lowestrainfall[], string monthtemp[])
{
    double lowest;
    lowest = lowestrainfall[0];
    string lowestmonth = "January";
    for (int count = 1; count < 12; count++)
    {
        if(lowestrainfall[count] < lowest)
        {
            lowest = lowestrainfall[count];
            lowestmonth = monthtemp[count];
        }
    }
    cout << lowestmonth << " has lowest amount of rainfall: " << lowest << " ml" << endl;
}
int main()
{
    string months[] = { "January",
                        "February",
                        "March",
                        "April",
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December"};
    double rainfall[12];
    double total = 0;
    double average;
    int count;
    for (count = 0; count < 12; count++)
    {
        cout << "Enter the total rainfall (in milliliter) of " << months[count] << ": ";
        cin >> rainfall[count];
        while(rainfall[count] < 0)
        {
            cout << "Invalid input!!! Please try again. " << endl;
            cout << "Enter the total rainfall (in milliliter) of " << months[count] << ": ";
            cin >> rainfall[count];
        }
    }
    for (count = 0; count <12; count++)
    {
        total += rainfall[count];
    }
    cout << "----------------------------------------" << endl;
    cout << setprecision(2) << showpoint << fixed;
    cout << "The total rainfall for a year is: " << total << " ml" << endl;
    average = (total / 12);
    cout << "The average rainfall monthly rainfall is " << average << " ml" << endl;
    highestfunction(rainfall, months);
    lowestfunction(rainfall, months);
    selectionSort(rainfall, 12);
    cout << "List of the months, sorted in order of rainfall from highest to lowest" << endl;
    for(count = 0; count < 12; count++)
    {
        cout << rainfall[count] <<" ml" << endl;
    }
    return 0;
}

2 个答案:

答案 0 :(得分:2)

据我了解,您希望每个降雨量标记一个月。 因此,您可以定义结构。例如;

struct RainfallDefinition
{ 
   string month; 
   double rainfall;
}

现在你可以使用RainfallDefinition数组并对降雨属性进行排序。

如果您不使用结构,则可以使用pair。但是,在我看来,使用struct更可靠,更易读。您可以随时扩展RainfallDefinition。例如,您还可以添加int year;int day;属性等,然后您可以排序而不会丢失任何信息。

答案 1 :(得分:-1)

我可以为它建议一个简单的算法...... 取两个数组一个字符串数组,其中包含月份......命名它 `

string month[12]; 
/* i.e. 12 months initially give em the month names in regular order...i.e. starting from jan to dec...*/

第二个数组将是一个二维数组... `

int rainfall[12][2];
// rainfall[i][j] likewise you know rows and columns respectively i m considering

`

最初将值放在其中

`

rainfall[0][0]=150; // this is the amount of rainfall in litre or whatever unit
rainfall[0][1]=0; 
/* here 2nd column contains the index of month in string, i.e. for now it is 0 that is January...likewise*/

现在您为rainfall[i][0]中的值对降雨数组进行排序,并在哪里交换两个值进行排序...假设您将rainfall[1][0]交换为rainfall[3][0]然后在你交换它们的下一个陈述中...也交换它们各自的指数...... 所以最后你会有排序的阵雨,所以当你想要rainfall[i][0]的月份时......你可以通过month[rainfall[i][1]]来检查......

**这个东西也可以使用指针和链表完成...甚至更有效但我这样做是因为当我在使用指针使用链接列表和没有它之间做出选择...我更喜欢没有它< / p>

**这也可以使用结构来完成,但这是我找到的最有机的方式......