2013-04-30 32 views
3

考慮查找最大和最小數:陣列中的

#include <iostream> // Include header file 

using namespace std; 

int main() //start of main function 
{ 

    int values[20]; // Declares array and how many elements 
    int small, big; // Declares integer 
    big = small = values[0]; // Assigns element to be highest or lowest value 

    for (int i = 0; i < 20; i++) // Counts to 20 and prompts the user for a value and stores it 
    { 
     cout << "Enter value " << i << ": "; 
     cin >> values[i]; 
    } 

    for (int i = 0; i < 20; i++) // Works out the biggest number 
    { 
     if(values[i] > big) // Compare biggest value with current element 
     { 
      big = values[i]; 
     } 
    } 

    for (int i = 0; i < 20; i++) // Works out the smallest number 
    { 
     if (values[i] < small) // Compares smallest value with current element 
     { 
      small = values[i]; 
     } 
    } 

    cout << "The biggest number is " << big << endl; // Prints outs the biggest number 
    cout << "The smallest number is " << small << endl; // Prints out the smallest number 
} 

這是到目前爲止我的代碼。我遇到的問題是打印出最大數量的數組。將第一個元素分配給最高值和最低值有關。它是有效的,如果我分開做它們。有什麼建議麼?

+9

你知道,我全部都是爲了評論。但像這樣的評論// include頭文件是一個障礙。將閱讀或評級你的作品的人知道包含指令的作用。 – StoryTeller 2013-04-30 11:39:10

+0

'big = small = values [0]'未定義的行爲,因爲元素沒有定義值。如果在編譯時打開警告,編譯器將生成相應的錯誤消息:'-Wall -Wexra -Werror' – 2013-04-30 12:52:23

+1

@StoryTeller以及其他許多註釋:'// main start',// // delcares array和多少個元素','//聲明整數'... – Shoe 2013-04-30 13:16:41

回答

5
big=small=values[0]; //assigns element to be highest or lowest value 

應該AFTER填充循環

//counts to 20 and prompts user for value and stores it 
for (int i = 0; i < 20; i++) 
{ 
    cout << "Enter value " << i << ": "; 
    cin >> values[i]; 
} 
big=small=values[0]; //assigns element to be highest or lowest value 

,因爲當你聲明陣列 - 這是unintialized(存儲一些未定義的值),因此,您bigsmall分配將存儲undefined值過之後。

當然,您可以使用std::min_elementstd::max_elementstd::minmax_elementC++11,而不是編寫循環。

+0

要添加到此。通過在填充數組之前分配大和小的值,可以分配未定義的值,最終可以將其存儲爲任意高或低的數字。這可能會導致您的輸出結果不正確。 – Boumbles 2013-04-30 11:39:13

+0

感謝您的幫助,您能否解釋爲什麼 – user2204993 2013-04-30 11:39:31

9

除非您確實需要實施您自己的解決方案,否則您可以使用std::minmax_element。這將返回一對迭代器,一個到最小的元素,一個到最大的元素。

#include <algorithm> 

auto minmax = std::minmax_element(std::begin(values), std::end(values)); 

std::cout << "min element " << *(minmax.first) << "\n"; 
std::cout << "max element " << *(minmax.second) << "\n"; 
+0

另請注意,'minmax_element'最大限度地減少了所需的比較次數,因此如果比較代價昂貴,它仍然比天真的手寫循環更快。 – ComicSansMS 2013-04-30 13:14:18

1

您在數組初始化之前分配給大和小,即大和小在此處假設堆棧上的任何值。由於它們只是簡單的值類型,並且沒有引用,所以一旦通過cin >>寫入值[0],它們就不會假設新值。

只需在第一次循環後移動賦值,它應該沒問題。

1
int main() //start of main fcn 
{ 

    int values[ 20 ]; //delcares array and how many elements 
    int small,big; //declares integer 
    for (int i = 0; i < 20; i++) //counts to 20 and prompts user for value and stores it 
    { 
     cout << "Enter value " << i << ": "; 
     cin >> values[i]; 
    } 
    big=small=values[0]; //assigns element to be highest or lowest value 
    for (int i = 0; i < 20; i++) //works out bigggest number 
    { 
     if(values[i]>big) //compare biggest value with current element 
     { 
      big=values[i]; 
     } 
     if(values[i]<small) //compares smallest value with current element 
     { 
      small=values[i]; 
     } 
    } 
    cout << "The biggest number is " << big << endl; //prints outs biggest no 
    cout << "The smallest number is " << small << endl; //prints out smalles no 
} 
0

可以填充數組初始化之後,或者你可以寫:

small =~ unsigned(0)/2; // Using the bit-wise complement to flip 0's bits and dividing by 2 because unsigned can hold twice the +ve value an 

整數可以容納。代替

big =- 1*(small) - 1; 

big = small = values[0] 

因爲當你填充所述陣列之前寫這條線,大和小的值將等於一個隨機剩餘值(如整數爲POD)從存儲器和如果這些數字大於或小於數組中的任何其他值,則會將它們作爲輸出。

+2

我可以看到有人陷入混淆。 – 2013-04-30 14:34:59

+1

你不覺得使用['std :: numeric_limits'](http://en.cppreference.com/w/cpp/types/numeric_limits)會更聰明嗎? – Blastfurnace 2013-04-30 14:48:44

+0

嗯,對不起,但我想指出這種方法,我試圖澄清儘可能。 – 2013-04-30 14:59:33