【问题标题】:Trouble with passing array to a function将数组传递给函数时遇到问题
【发布时间】:2019-07-13 06:34:34
【问题描述】:

我应该使用字符串数组来输出“输入降雨量为 x”行,x 是月份的名称,并将输入发送到单独的数组进行计算。目前,当我运行我的代码时,我看到“输入 1 的降雨量”、“输入 2 的降雨量”等,而不是“输入 1 月的降雨量”、“输入 2 月的降雨量”。除此之外,代码还需要显示总降雨量、平均降雨量以及最低和最高月份的降雨量。我可以让程序输出正确的总数和平均值,但是,最高和最低月份只输出一个随机数而不是月份名称。

我试图创建原型并将数组调用到函数中,但我认为问题可能是由于我的字符串数组存在问题。我试过使用 for 循环,我试过改变我的语法无济于事。我目前在调试过程中没有收到任何错误,只看到不正确的输出而不是字符串输出。

#include <iostream>
#include <string>

using namespace std;

// Function Prototypes
void getMonthlyRainfall(double[], int);
double getTotal(const double[], int);
double getHighestAmount(const double[], int);
double getLowestAmount(const double[], 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,
        lowestAmount,
        highestAmount;

    //Get rainfall input from user
    getMonthlyRainfall(rainfall, 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
    lowestAmount = getLowestAmount(rainfall, MONTHS);

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

    cout << "Total rainfall: " << total << endl;
    cout << "Average rainfall: " << average << endl;
    cout << "Least rainfall in: " << getLowestAmount << endl;
    cout << "Most rainfall in: " << getHighestAmount << endl;
    return 0;
}

void getMonthlyRainfall(double rain[], int size) 
{
    int index;
    for (index = 0; index < 12; index++)
    {
        cout << "Enter rainfall for " << (index + 1) << ": ";
        cin >> rain[index];
    }
}

double getTotal(const double numbers[], int size) 
{
    double total = 0; // Accumulator
    for (int count = 0; count < size; count++)
        total += numbers[count];
    return total;
}

double getHighestAmount(const double numbers[], int size) 
{
    double highest; // Holds highest value
    // Get array's first element
    highest = numbers[0];
    // Step through array
    for (int count = 0; count < size; count++) 
    {
        if (numbers[count] > highest)
            highest = numbers[count];
    }
    return highest;
}

double getLowestAmount(const double numbers[], int size) 
{
    double lowest; // Holds lowest value
    // Get array's first element
    lowest = numbers[0];
    // Step through array
    for (int count = 0; count < size; count++)
    {
        if (numbers[count] < lowest)
            lowest = numbers[count];
    }
    return lowest;
}

正如我所说,第一个输出应该是一个月的实际名称,并且应该是有序的。例如,提示应该首先要求用户输入一月份的总降雨量,用户输入一个数字。然后提示继续要求用户输入 2 月的数字,依此类推,直到 12 月。相反,我看到提示要求用户输入“1”的总降雨量,用户输入一个数字,然后提示要求用户输入“2”的降雨量,直到它达到 12。程序进行计算并输出正确的总降雨量和平均值,但是当它应该输出“降雨量最高(或最低)的月份:(月份名称)”时,它会给我一个随机数,例如 01201686。

总而言之,字符串数组输出月份名称,用户输入存储在单独的数组中进行计算。这些计算是针对总计和平均值输出的,但降雨总量需要与相应实体的月份进行比较,并且最高和最低的输出需要是字符串而不是数字。

【问题讨论】:

  • 编译时不要忽略编译器警告。编译器警告是编译器告诉你,虽然代码在语法上是正确的,但它可以编译,但它可能在逻辑上不正确,并且会崩溃、行为不端甚至看起来像它的行为一样直到突然不正确。
  • 我想推荐:请不要使用普通的 C 样式数组。请尝试使用 STL 容器。

标签: c++ arrays function


【解决方案1】:

这是对名称的简单混淆。您有函数的名称(将打印为“随机”数字)而不是您正在使用的变量的名称。

cout << "Least rainfall in: " << getLowestAmount << endl;

应该是

cout << "Least rainfall in: " << lowestAmount << endl;

至于你的第一个问题,改变

cout << "Enter rainfall for " << (index + 1) << ": ";

cout << "Enter rainfall for " << monthNames[index] << ": ";

显然第一个版本打印一个数字(index + 1 是一个数字)。要获取月份名称,您必须使用该数字作为月份名称数组的索引。

要完成这项工作,您还需要在getMonthlyRainfall 函数中提供monthNames(目前它仅在main 中可见)。您应该像这样将monthNames 传递给getMonthlyRainfall 函数

void getMonthlyRainfall(double[], string[], int);

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

void getMonthlyRainfall(double rain[], string monthNames[], int size) 
{
    ...

编辑

因此,要同时输出最低月降雨量和最低降雨量月份的名称,您应该更改 getLowestAmount 函数以返回降雨量最低月份的索引。您还应该更改此函数的名称,因为它现在执行的操作与原始函数不同,并且旧名称不能准确描述新函数,但为了清楚起见,我将保持不变。您可以稍后再决定一个新名称。

// this function returns a month index, not a rainfall amount,
// so it's return type is int not double
int getLowestAmount(const double[], int);

这是更新后的功能

int getLowestAmount(const double numbers[], int size) 
{
    double lowest; // Holds lowest value
    int lowestIndex; // Holds the index of the lowest value
    // Get array's first element
    lowest = numbers[0];
    lowestIndex = 0;
    // Step through array
    for (int count = 0; count < size; count++)
    {
        if (numbers[count] < lowest)
        {
            lowest = numbers[count];
            lowestIndex = count;
        }
    }
    return lowestIndex;
}

除了我添加了lowestIndex之外,函数是相同的,这就是我返回的值。

现在在main 中,您可以使用最低索引来打印您想要查看的两个值

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

...

cout << "Least rainfall in: " << monthNames[lowestIndex] << 
    " is " << rainfall[lowestIndex] << endl;

【讨论】:

  • 这个建议有助于我了解我在函数中做错了什么,并且我能够正确获取月份名称的输出,但函数参数仍然存在问题输出月份名称。关于在我的函数中提供 monthNames 的建议帮助我能够输出最高降雨量的数量,但我应该输出最高和最低降雨量的月份,而不是实际数量。例如,如果 9 月的降雨量为 1 英寸并且是最低的月份,则输出将显示“9 月降雨量最少”。
  • 我相信我应该将任何元素用于降雨量并将其与相应月份的并行数组中的元素进行比较,但我无法弄清楚如何用平行词数组,因为一个数组是字符串,另一个是双精度数组。
  • @Sydniewyatt 好的,那么您应该更改您的getLowestAmount 函数,以便返回最低降雨量而不是返回最低降雨量发生月份的索引。然后,一旦您知道最低月份指数,就可以使用它来打印月份名称和降雨量。明白了吗?如果没有,我会更新我的答案。
  • 我知道我需要降雨量最低的月份的索引,但我一直在尝试使用并行数组来比较月份名称 [count] 和数字 [count] 以输出月份名称 [count]。这就是我一直在我的函数/循环中尝试获得输出的内容: if (rainfall[count]
  • @Sydniewyatt 不,这不对,我会更新我的答案。
猜你喜欢
  • 1970-01-01
  • 2015-01-07
  • 2012-06-17
  • 2018-08-28
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 2021-10-18
相关资源
最近更新 更多