簡體   English   中英

查找數組中的最大和最小數字

[英]Find largest and smallest number in an array

考慮:

#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
}

到目前為止這是我的代碼。 我遇到的問題是它打印出最大數量的數組。 將第一個元素分配給最高和最低值的方法。 如果我單獨使用它,它可以工作。 有什么建議么?

除非您真的必須實現自己的解決方案,否則可以使用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";
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值了。

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

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]寫入via cin >>,它們就不會假設一個新值。

只需在第一次循環后移動賦值即可。

您可以在填充數組后進行初始化,也可以編寫:

 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 ),如果這些數字大於或小於數組中的任何其他值,則將它們作為輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM