并行数组如何将int转换为字符串

Parallel Arrays how to turn int into string

本文关键字:转换 字符串 int 数组 并行      更新时间:2023-10-16

我的作业有问题,我的教授没有回应。我如何让我的数组返回getHighestgetLowest函数的月份的字符串名称?

它一直告诉我我必须添加更多的细节,我从中删除了代码,所以我现在只剩下主要和getLowestgetHighest函数。它说我还需要更多的细节,所以我输入这个来添加更多的细节,这样我就可以提交我的问题了。

提前感谢!

#include <iostream>
#include <string>   
#include <iomanip>
using namespace std;
//Function prototypes
void getRainfall(double[], int);  //To retrieve the user input.
double getTotal(double[], int);  //To total the rainfall amounts.
double getAverage(double[], int);  //To get the average rainfall.
double getLowest(double[], int, int&);  //Returns the lowest value, provides                 the index of the lowest value in the last parameter.
double getHighest(double[], int, int&); //Returns the highest value,     provides the index of the highest value in the last parameter.
//Global Variable
const int NUM_MONTHS = 12;
//Array names
double rainfall[NUM_MONTHS];
string month[NUM_MONTHS] = { "January", "February", "March", "April", "May",     "June", "July", "August", "September", "October", "November", "December" };         //Month array to hold names of months
int main()
{
    //Declare variables
    double  total, average;
    int low, high;
    string lowMonth, highMonth;
    //Call Functions
    getRainfall(rainfall, NUM_MONTHS);  //To retrieve the user input.
    total = getTotal(rainfall, NUM_MONTHS); //To total the rainfall amounts.
    average = getAverage(getTotal, NUM_MONTHS);  //To get the average rainfall.
    lowMonth = getLowest(rainfall, NUM_MONTHS, low);  //Returns the lowest value, provides the index of the lowest value in the last parameter.
    highMonth = getHighest(rainfall, NUM_MONTHS, high); //Returns the highest value, provides the index of the highest value in the last parameter.
    //Display the following:
    cout << "The total rainfall for the year is: " << total << endl;
    cout << "The average rainfall for the year is: " << fixed << showpoint << setprecision(2) << average << endl;
    cout << "Least amount of rainfall fell in: " << highMonth << endl;
    cout << "Most amount of rainfall fell in: " << lowMonth << endl;
    return 0;
}

//*******************************************************************************************
//              double getLowest(double amount[], int size)                                 *
//  Returns the lowest value, provides the index of the lowest value in the last parameter. *
//*******************************************************************************************
double getLowest(double amount[], int NUM_MONTHS, int &low)
{
    low = amount[0];  //Variable to hold lowest value.
    int lowMonth = 0; //Set low value to intial rainfall value.  //Variable to return month element location.
    for (int index = 0; index < NUM_MONTHS; index++)  
    {
        if (amount[index] < low)
        {
            low = amount[index];
            lowMonth = index;
        }
    }
    return lowMonth;
}
//*********************************************************************************************
//              double getHighest(double amount[], int size)                                  *
//  Returns the highest value, provides the index of the highest value in the last parameter. *
//*********************************************************************************************
double getHighest(double amount[], int NUM_MONTHS, int &highMonth)
{
    //high = amount[0];  //Variable to hold highest value
    months = amount[0];
    double highMonth = 0; //Variable to hold highest value
    for (int index = 0; index < NUM_MONTHS; index++)    
    {
        if (amount[index] > high)
        {
            highMonth = amount[index];
            months = index;
        }
    }
    return highMonth;
}

所以你的问题是非常不清楚,你的代码是一个完全混乱,但我假设你想函数getHighest也返回一个字符串与月份的名称?
好吧,一个函数只能返回一种类型的变量,但您可以使用引用:

double getHighest(double amount[], int NUM_MONTHS, int &highMonth, string &name)
{
    //high = amount[0];  //Variable to hold highest value
    months = amount[0];
    double highMonth = 0; //Variable to hold highest value
    for (int index = 0; index < NUM_MONTHS; index++)    
    {
        if (amount[index] > high)
        {
            highMonth = amount[index];
            months = index;
            name = month[index];
        }
    }
    return highMonth;
}

但通常你的代码有相当多的缺陷:例如覆盖highMonth可能会导致问题,为什么要传递NUM_MONTHS,即使它是全局定义的?我不太明白为什么这段代码如此复杂。我也认为getHighestgetLowest功能甚至不起作用。作为一种更好的方法,我会尝试这样的函数:

double getHighest(double amount[], string &name)
{
    double highest = 0;
    double highPos = 0;
    for(unsigned int i = 0; i < NUM_MONTHS; i++)
    {
        if(highest <= amount[i])
        {
            highest = amount[i];
            highPos = i;
        }
    }
    name = month[highPos];
    return highest;
}
编辑:或者,如果你只想返回字符串,你可以这样做:

字符串,名称){//high = amount[0];//保存最高值的变量Months = amount[0];double highMonth = 0;//保存最高值的变量

    for (int index = 0; index < NUM_MONTHS; index++)    
    {
        if (amount[index] > high)
        {
            highMonth = amount[index];
            months = index;
            name = month[index];
        }
    }
    return highMonth;
}

但通常你的代码有相当多的缺陷:例如覆盖highMonth可能会导致问题,为什么要传递NUM_MONTHS,即使它是全局定义的?我不太明白为什么这段代码如此复杂。我也认为getHighestgetLowest功能甚至不起作用。作为一种更好的方法,我会尝试这样的函数:

string getHighest(double amount[], string &name)
{
    double highest = 0;
    double highPos = 0;
    for(unsigned int i = 0; i < NUM_MONTHS; i++)
    {
        if(highest <= amount[i])
        {
            highest = amount[i];
            highPos = i;
        }
    }
    name = month[highPos];
    return name;
}