2013-11-01 193 views
0

我應該在數組中找到最小值和最大值,但我似乎無法弄清楚爲什麼答案不正確。例如,如果我輸入「1 2 3 4 5」作爲我的五次,它告訴我1是我的最大值,0是最小值。出於某種原因,無論第一個數字是多少,它都會將其稱爲最大值,並將最小值指定爲0。查找五次數組中的最大值和最小值

#include <iostream> 
using namespace std; 

int find_distance(int j); //a function that returns a distance based on the choice j 
int intmax, intmin; 
int main() 
{ 

int i =0; 
int distance[6]; 
double data[6][5]; 
for(int j = 0; j < 6; j++) 
{ 
    distance[j] = find_distance(j); 
    cout << "\nEnter 5 of your best running times for \n " << distance[j] << " m \n"; 
    for(int i = 0; i < 5; i++) 
    { 
     cout << "Enter a time \n"; cin >> data[j][i]; 
    } 

} 
cout << "Here is your best 5 times: "; 
for(int j = 0; j < 6; j++) 
{ 
cout << "\nDistance : " << distance[j] << " m \n"; 

for(int i = 0; i < 5; i++) 
{ 
    system ("pause"); 
    cout << data[j][i] << "\t"; } cout << endl; 

    if (data[j][i] < intmin) 
    intmin = data[j][i]; 
    else if (data[j][i] > intmax) 
    intmax = data[j][i]; 

    cout << "The maximum time is: " << intmax << endl; 
    cout << "The minimum time is: "<< intmin << endl; 
} 
return 0; 
} 
int find_distance(int j) 
{ 
switch (j) 
{ case 0: // 100 meter 
return 100; 
break; 
case 1: // 150 meter 
return 150; 
break; 
case 2: // 200 meter 
return 200; 
break; 
case 3: // 400 meter 
return 400; 
break; 
case 4: // 500 meter 
return 800; 
break; 
default: // 1600 meter 
    return 1600; 
    } 
} 
+0

你爲什麼不排序值並找到第一個和最後一個元素? – nitinsh99

+0

@ nitinsh99因爲這會使它O(log n)而不是O(n)?可能在很短陣列的情況下,這並不重要,但是我在這裏感覺到了作業,並且可能OP應該提供比給定的更通用的解決方案。 – 2013-11-01 07:16:34

+0

[在數組中找到最大和最小數字]的可能重複(http://stackoverflow.com/questions/16298906/find-largest-and-smallest-number-in-an-array) – Torben

回答

0

最小值爲0,因爲當初始化intmin時,它默認設置爲0。你永遠不會輸入負面的時間,所以在比較中,它總是小於比較值。
由於for循環在奇怪的地方結束並且比較代碼執行不正確,所以最大值已關閉。更改此代碼:

for(int j = 0; j < 6; j++) 
{ 
    cout << "\nDistance : " << distance[j] << " m \n"; 

for(int i = 0; i < 5; i++) 
{ 
system ("pause"); 
cout << data[j][i] << "\t"; } cout << endl; //why does the for loop end here? 

if (data[j][i] < intmin) 
intmin = data[j][i]; 
else if (data[j][i] > intmax) 
intmax = data[j][i]; 

      //move the end bracket to this line and it should work 

cout << "The maximum time is: " << intmax << endl; 
cout << "The minimum time is: "<< intmin << endl; 
} 
0

只是爲了練習:

#include <iostream> 
#include <algorithm> 
#include <string> 
#include <boost/regex.hpp> 

int main() { 
    using namespace std; 

    string input; 
    boost::regex re("-?\\d+"); 
    vector<int> integers; 

    cout << "enter sequence of integers: "; 
    getline(cin, input); 

    boost::sregex_token_iterator begin(input.begin(), input.end(), re, 0); 
    boost::sregex_token_iterator end; 
    while (begin != end) { 
    integers.push_back(stoi(*begin)); 
    ++begin; 
    } 

    if (integers.size()) { 
    auto pair = minmax_element(integers.begin(), integers.end()); 
    cout << "min: " << *pair.first << " max: " << *pair.second << endl; 
    } else { 
    cout << "you didn't enter any integers." << endl; 
    } 
    return 0; 
} 

這是如何編譯和運行:

$ g++ -o lab_2 -std=c++11 -lboost_regex lab_2.cpp 
$ ./lab_2 
$ enter sequence of integers: -10 34 75 101 2 43 
$ min: -10 max: 101 

需要安裝升壓因爲STL正則表達式是不實用而不失。

相關問題