2014-09-30 56 views
-1

我正在嘗試在我的動態數組中找到中間數字。用戶輸入三個數字,然後程序將其從最小到最大排序。它工作正常,直到我輸入數字10,20,30它輸出10,10,30,當我輸入30,20,10輸出10,30,30。在動態數組中查找中間數字C++

這是我的代碼的一部分。 感謝

cout << "Enter 3 values and I will sort them from lowest to highest." << endl; 

for (index = 0; index < 3; index++) 
{ 
    cin >> number[index]; 

    while (cin.fail())//Check that values entered are the correct type. 
    { 
     cout << "You did not enter a number" << endl; 
     cout << "Enter a new number." << endl; 
     cin.clear();//Clears the input if user input non-integer value. 
     cin.ignore(1000, '\n');//Ignores up to 1000 characters or up to new line. 

     cin >> number[index]; 
    }//end while 
}//end for 

cout << "You have entered the values" << endl; 
for (index = 0; index < 3; index++) 
{ 
    cout << number[index] << " "; 
    cout << endl; 
}//end for 

small = number[0]; 
mid = number[0]; 
high = number[0]; 

for (int i = 0; i < 3; i++)//Goes through the array to determine order of values. 
{ 
    if (small > number[i]) 
    { 
     small = number[i]; 
    } 
    else if (high < number[i]) 
    { 
     high = number[i]; 
    } 
    else if (high > mid && mid > small) 
    { 
     mid = number[i]; 
    } 

} 

cout << "Here is the new order of your numbers." << endl; 
cout << small << endl; 
cout << mid << endl; 
cout << high << endl; 
+0

您需要向我們展示你如何申報數量 – 2014-09-30 03:40:43

+0

您應該使用調試器來逐步執行代碼,找出原因如果輸出與您的期望有所不同。 – 2014-09-30 03:46:02

+0

這個問題沒有顯示出太多的研究。用一張紙瀏覽你的循環,看看爲什麼每個變量都以它們的值爲結束。 – jxh 2014-09-30 04:02:36

回答

0

在下面的塊中的邏輯是有缺陷的

for (int i = 0; i < 3; i++) 
{ 
    if (small > number[i]) 
    { 
     small = number[i]; 
    } 
    else if (high < number[i]) 
    { 
     high = number[i]; 
    } 
    else if (high > mid && mid > small) 
    { 
     mid = number[i]; 
    } 

}

因爲smallmedhigh是不排序開始。您可以使用std::sort,正如@kmac的回答中所建議的那樣。如果你想這個代碼自己,嘗試:

for (int i = 0; i < 3; i++) 
{ 
    if (small > number[i]) 
    { 
     small = number[i]; 
    } 

    if (high < number[i]) 
    { 
     high = number[i]; 
    } 
} 
for (int i = 0; i < 3; i++) 
{ 
    if (number[i] > small && number[i] < high) 
    { 
     mid = number[i]; 
    } 
} 
+0

我認爲std :: sort的時間複雜度比這更低? – paulm 2014-09-30 04:22:35

+0

對於大小爲3的數組,使用哪種方法並沒有多大區別。無論如何,對於一個大型數組,'std :: sort'的時間複雜度爲'O(N * log(N))',而這種方法需要'O(N)'時間。 – 2014-09-30 04:26:04

+0

使用額外的for循環修復它。感謝您的幫助,花了數小時的時間來解決這個問題。 – 2014-09-30 05:20:44

1

更一般地,你可以使用來自<algorithm>std::sort,雖然它會修改你的陣列(複印一份,如果你想保持原來的輸入)。

#include <cstdio> 
#include <algorithm> 

int main() 
{ 
    int nums[] = {5, 1, 7}; 
    unsigned int num_size = sizeof(nums)/sizeof(nums[0]); 

    std::sort(nums, nums+num_size); 

    std::cout << "Here is the order of your numbers:" << std::endl; 
    for (unsigned int i=0; i<num_size; ++i) 
     std::cout << nums[i] << std::endl; 

    return 0; 
} 

輸出:

Here is the order of your numbers: 
1 
5 
7 
0

的問題是你的第三個如果情況時您檢查「中」,因爲你承擔高,這將永遠是真實的是30,但你有沒有檢查到的數量[2] =='30'在那一點上。

而是寫這樣的事情

// first find max,min 
for (int i = 0; i < 3; ++i) 
{ 
    if (number[i] < small) 
    { 
    small = number[i]; 
    } 
    if (number[i] > high) 
    { 
    high = number[i]; 
    } 
} 
// then find mid 
for (int i = 0; i < 3; ++i) 
{ 
    if (number[i] > small && number[i] < high) 
    { 
    mid = number[i]; 
    } 
}