在我当前的代码中,我将提示附加在顶部。我的高值或低值都很难。我相信问题出在函数被调用,而不是我的int main(),但我可能是错的。最低值不是记录的值。它似乎默认在数组的中间或结尾(6月或12月是我的低输出的常见月份,我的高位通常是正确的)
有关如何解决此问题的任何想法?
/*
Write a program that lets the user enter the total
rainfall for each of 12 months into an array of
doubles. The program should calculate and display
the total rainfall for the year, the average monthly
rainfall, and the months wit hthe highest and lowest
amounts.
Input Validation: Do not accept negative numbers
for monthly rainfall figures.
*/
include <iostream>
include <iomanip>
include <string>
using namespace std;
//Function Prototypes
double RainTotal(double[], int);
double RainAverage(double[], int);
double RainHighpoint(double[], int);
double RainLowpoint(double[], int);
int main()
{
int count; //counting variable for loops
const int Size = 12;
double RainInput[Size]; //Numerical variables
int RAINHIGHEST, RAINLOWEST;
double RAINTOTAL, RAINAVERAGE;
string Month[] = { "January", "Febuary", "March", "April", //Must list string names for each month
"May", "June", "July", "August", "September", "October",
"November", "December" };
cout << "Please enter the average rainfall with the corresponding month\n\n";
for (count = 0; count < Size; count++)
{
cout << Month[count] << " : "; //User prompted to enter rainfall values
cin >> RainInput[count];
while (RainInput < 0)
{
cout << "Please enter a positive number for " << Month[count] << endl;
cin >> RainInput[count];
}
}
RAINTOTAL = RainTotal(RainInput, Size); //Call Total Rainfall
RAINAVERAGE = RainAverage(RainInput, Size); //Call Average Rainfall
string LowMonth, HighMonth; //String value for given High/Low Months
double LowPoint, HighPoint; //Values stored for highest and lowest rainfall
RAINLOWEST = RainLowpoint(RainInput, Size); //Call Lowest Array Subscript Value
LowMonth = Month[RAINLOWEST];
LowPoint = RainInput[RAINLOWEST];
RAINHIGHEST = RainHighpoint(RainInput, Size); //Call Highest Array Subscript Value
HighMonth = Month[RAINHIGHEST];
HighPoint = RainInput[RAINHIGHEST];
cout << endl << endl;
cout << "The Total Rainfall is: " << RAINTOTAL<<endl;
cout << "The Average Rainfall is: " << RAINAVERAGE << endl;
cout << LowMonth << " had the least rainfall all year with " << LowPoint << endl;
cout << HighMonth << " had the most rainfall all year with " << HighPoint << endl;
return 0;
}
double RainTotal(double RainInput[], int size)
{
double Total = 0;
for (int count = 0; count < size; count++) //Find the Total Rainfall
{
Total += RainInput[count];
}
return Total;
}
double RainAverage(double RainInput[], int size)
{
double Total = 0;
double Average;
for (int count = 0; count < size; count++) //Find the Rainfall Average
{
Total += RainInput[count];
}
Average = Total / size;
return Average;
}
double RainLowpoint(double RainInput[], int size)
{
double LowPoint = RainInput[0];
int LowCount = 0;
for (int count = 0; count < size; count++) //Find Lowest rainfall month through numerical comparison
{
if (RainInput[count] <= LowPoint)
{
LowCount = count;
}
}
return LowCount;
}
double RainHighpoint(double RainInput[], int size)
{
double HighPoint = RainInput[0];
int HighCount = 0;
for (int count = 0; count < size; count++) //Find Highest rainfall month through numerical comparison
{
if (RainInput[count] >= HighPoint)
{
HighCount = count;
}
}
return HighCount;
}
答案 0 :(得分:1)
你只是忘了更新循环中当前的最高点和最低点,重写下面的两个函数应该解决问题。
double RainLowpoint(double RainInput[], int size)
{
double LowPoint = RainInput[0];
int LowCount = 0;
for (int count = 0; count < size; count++) //Find Lowest rainfall month through numerical comparison
{
if (RainInput[count] <= LowPoint)
{
LowCount = count;
LowPoint = RainInput[count]; // was missing
}
}
return LowCount;
}
double RainHighpoint(double RainInput[], int size)
{
double HighPoint = RainInput[0];
int HighCount = 0;
for (int count = 0; count < size; count++) //Find Highest rainfall month through numerical comparison
{
if (RainInput[count] >= HighPoint)
{
HighCount = count;
HighPoint = RainInput[count]; // was missing
}
}
return HighCount;
}
您也可以删除自定义代码并使用&lt; algorithm&gt;来自标准库std::min_element和std::max_element。
答案 1 :(得分:0)
尝试更新您的LowPoint和HighPoint变量:
if (RainInput[count] <= LowPoint)
{
LowCount = count;
LowPoint = RainInput[count];
}
...
if (RainInput[count] >= HighPoint)
{
HighCount = count;
HighPoint = RainInput[count];
}