将数组传递给函数时出现问题

Trouble with passing array to a function

本文关键字:问题 函数 数组      更新时间:2023-10-16

我应该使用一个字符串数组来输出"输入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;
}

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

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

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

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;