C++ 将数组传递给函数时出现问题
我应该使用一个字符串数组来输出行“为x输入降雨”,x是一个月的名称,并将输入发送到一个单独的数组进行计算。目前,当我运行代码时,我看到的是“输入1的降雨量”、“输入2的降雨量”等,而不是“输入1月份的降雨量”、“输入2月份的降雨量”。除此之外,代码还需要显示总降雨量、平均降雨量、最低降雨量和最高降雨量。我能够让程序输出正确的总数和平均数,但是,最高和最低月份只输出一个随机数,而不是月份名称 我曾尝试创建原型并调用一个函数的数组,但我认为这个问题可能是因为我的字符串数组有问题。我尝试过使用for循环,我尝试过修改语法,但没有效果。我目前在调试过程中没有收到任何错误,只看到不正确的输出,而不是字符串输出C++ 将数组传递给函数时出现问题,c++,arrays,function,C++,Arrays,Function,我应该使用一个字符串数组来输出行“为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;
}
#包括
#包括
使用名称空间std;
//功能原型
void getMonthlyRainfall(双精度[],整数);
双getTotal(constdouble[],int);
双getHighestAmount(constdouble[],int);
双GetLowsTamount(常数双[],整数);
int main()
{
const int MONTHS=12;
字符串monthNames[月]={“一月”、“二月”、“三月”、“四月”,
“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”,
“12月”};
双降雨量[月],//数组
全部的
平均的
Lowstamount,
高地山;
//从用户处获取降雨输入
getMonthlyRainfall(降雨量,月);
//获取一年的总降雨量
总计=总降雨量(降雨量,月);
//得到平均降雨量
平均数=总数/月;
//获取降雨量最低的月份
LowstaMount=GetLowstaMount(降雨量,月);
//获取降雨量最高的月份
highestAmount=getHighestAmount(降雨量,月);
cout这只是名称上的一个简单混淆。您使用的是函数的名称(将以“随机”数字打印),而不是所使用变量的名称
cout << "Least rainfall in: " << getLowestAmount << endl;
及
及
编辑
因此,要同时输出最低月降雨量和最低降雨量月份的名称,您应该更改GetLowsTamount
函数以返回最低降雨量月份的索引。您还应该更改此函数的名称,因为它现在执行的操作与原始函数不同on和旧名称不能准确描述新函数,但为了清楚起见,我将保持不变。您可以稍后决定新名称
// 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;
}
编译时不要忽略编译器警告。编译器警告是指编译器告诉您,虽然代码在语法上是正确的,但它在编译时可能在逻辑上是不正确的,并且会崩溃、行为不正常,甚至看起来好像它在正常运行,直到突然它没有。我建议:请不要使用纯C风格的arrays。请尝试使用STL容器。这一建议对于了解我在函数中的错误有很大帮助,并且我能够正确地获得月名的输出,我仍然对输出月名的函数参数有问题。关于在函数中提供月名的建议帮助我o能够输出最高降雨量,但我应该输出降雨量最高和最低的月份,而不是实际降雨量。例如,如果9月降雨量为1英寸,并且是最低月份,则输出会显示“9月降雨量最少”我相信我应该取任意元素作为降雨量,并将其与相应月份的并行数组中的元素进行比较,但我不知道如何对并行数组进行命名,因为一个数组是字符串,另一个是双精度的。@Sydniewyatt好的,那么您应该更改GetLowsTamount
函数因此,它不会返回最低降雨量,而是返回降雨量最低月份的指数。一旦您知道最低月份指数,您可以使用该指数打印月份名称和降雨量。清楚吗?如果不清楚,我将更新我的答案。我知道我需要降雨量最低月份的指数b但是我一直在尝试使用一个并行数组来比较monthNames[count]和numbers[count]与输出monthNames[count]。这就是我在函数/循环中尝试获得输出的内容:if(rainue[count]cout << "Enter rainfall for " << monthNames[index] << ": ";
void getMonthlyRainfall(double[], string[], int);
//Get rainfall input from user
getMonthlyRainfall(rainfall, monthNames, MONTHS);
void getMonthlyRainfall(double rain[], string monthNames[], int size)
{
...
// 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;
}
// Get the month with the lowest rainfall
lowestIndex = getLowestAmount(rainfall, MONTHS);
...
cout << "Least rainfall in: " << monthNames[lowestIndex] <<
" is " << rainfall[lowestIndex] << endl;