2014-12-07 48 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]; 
}