2017-10-16 239 views
-5

如何找到數組中的最小值?我認爲我做得很對,但是當我運行這個程序時它輸出爲零。如何輸出輸入到數組中的最小值? (C++)

我在另一個程序中做了同樣的工作,它工作。運行時,顯示最高的元素,但最低的顯示爲零。

#include <iostream> 
#include <iomanip> 
using namespace std; 

int main() 
{ 
    const int ARRAY_SIZE = 12; 
    double rainfall[ARRAY_SIZE]; 
    double total_year, monthly_average; 
    double highest = rainfall[0]; 
    double lowest = rainfall[0]; 

    cout << " Input rainfall for each month: \n" ; 

    for(int index = 0; index < ARRAY_SIZE; index++) 
    { 
     cout << " Month " << index+1 << ": " ; 
     cin >> rainfall[index]; 
     total_year += rainfall[index]; 

     if(rainfall[index] < 0) 
     { 
      cout << " Rainfall must equal to 0 or higher: " ; 
      cin >> rainfall[index]; 
     } 
    } 
    for(int x = 0; x < ARRAY_SIZE; x++) 
    { 
     if(highest < rainfall[x]) 
     { 
      highest = rainfall[x]; 
     } 
     if(lowest > rainfall[x]) 
     { 
      lowest = rainfall[x]; 
     } 
    } 
    cout << fixed << setprecision(2) << endl; 

    cout << " There was " << total_year << " inches" ; 
    cout << " of rainfall this year. \n" ; 

    cout << " The monthtly average was " << total_year/12 ; 
    cout << " inches of rainfall.\n"; 

    cout << " The highest rainfall was " << highest << " inches" << endl; 
    cout << " The lowest rainfall was " << lowest << " inches" << endl; 

    return 0; 
} 
+1

使用您的調試器。 – Charles

+1

@Seyfat Khamidov聲明這些變量時,數組未被初始化。雙倍最高=降雨量[0]; 雙下最低=降雨[0];所以變量有不確定的值。你必須在數組填充後初始化變量。 –

+0

如果您的total_year變量嘗試以降雨量[指數]增加並且其值低於0,那麼您的total_year變量將不準確。然後,您要求用戶輸入相同指數下降的新值。 –

回答

1

嘗試聲明使用它們的變量。否則,代碼的可讀性會降低。

數組雨量不大所以使用它與不定值的元素的變量highestlowest無厘頭尚未初始化

double rainfall[ARRAY_SIZE]; 
//... 
double highest = rainfall[0]; 
double lowest = rainfall[0]; 

在計算它們的循環之前聲明和初始化變量。

double highest = rainfall[0]; 
double lowest = rainfall[0]; 

for(int x = 0; x < ARRAY_SIZE; x++) 
{ 
    if(highest < rainfall[x]) 
    { 
     highest = rainfall[x]; 
    } 
    if(lowest > rainfall[x]) 
    { 
     lowest = rainfall[x]; 
    } 
} 

在這個循環中

for(int index = 0; index < ARRAY_SIZE; index++) 
{ 
    cout << " Month " << index+1 << ": " ; 
    cin >> rainfall[index]; 
    total_year += rainfall[index]; 

    if(rainfall[index] < 0) 
    { 
     cout << " Rainfall must equal to 0 or higher: " ; 
     cin >> rainfall[index]; 
    } 
} 

移動的聲明

total_year += rainfall[index]; 

if語句之後。

for(int index = 0; index < ARRAY_SIZE; index++) 
{ 
    cout << " Month " << index+1 << ": " ; 
    cin >> rainfall[index]; 

    if(rainfall[index] < 0) 
    { 
     cout << " Rainfall must equal to 0 or higher: " ; 
     cin >> rainfall[index]; 
    } 

    total_year += rainfall[index]; 
} 

我是否願意代替像

while (rainfall[index] < 0) 
    { 
     cout << " Rainfall must equal to 0 or higher: " ; 
     cin >> rainfall[index]; 
    } 

while語句,但使用變量之前聲明total_year你必須初始化它

double total_year = 0.0; 

變量monthly_average不使用代碼。所以它的聲明可以被刪除。

考慮到在C++ std::min_elementstd::max_elementstd::minmax_element中有下列算法可用於查找數組或其他容器中的最小值和最大值。

+0

非常感謝您的建議!但我不能使用std :: min_element和其他人,因爲我們還沒有在課堂上了解它。 –

+0

點擊綠色複選標記接受答案。 –

相關問題