2014-12-07 28 views
0

在我当前的代码中,我在顶部附加了提示符。我的高或低值都很难。我相信问题出在函数被调用,而不是我的int main(),但我可能是错的。最低值不是正在记录的值。这似乎默认无论是在中间或数组的结尾(六月或十二月是常见的几个月对我的低输出,我的高往往是正确的)丢失数组​​和数组调用

对我怎么能解决这个问题的任何想法?

/* 
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; 
} 
+2

欢迎的StackOverflow!请发布一个[SSCE *](http://sscce.org/),它仍然表明您的问题。此外,请详细说明代码中发生了什么,以及您尝试解决的问题。 :) – Qix 2014-12-07 21:26:47

回答

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; 
} 

你也可以只删除您的自定义代码,并使用<算法>从STD库std::min_elementstd::max_element

+0

你也不需要在零开始你的循环,因为你已经在做的是,在初始化。换句话说,我相信你可以通过做一些优化:“int count = 1”。 – Amadeus 2014-12-07 21:33:55

0

尝试更新LowPoint和HighPoint公司的变量:

if (RainInput[count] <= LowPoint) 
{ 
     LowCount = count; 
     LowPoint = RainInput[count]; 
} 

...

if (RainInput[count] >= HighPoint) 
{ 
     HighCount = count; 
     HighPoint = RainInput[count]; 
}