查找数组中的最大和最小数字

时间:2013-04-30 11:35:21

标签: c++ arrays

考虑:

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

到目前为止,这是我的代码。我遇到的问题是它打印出最大数量的数组。将第一个元素分配给最高和最低值的方法。如果我单独使用它,它可以工作。有什么建议吗?

5 个答案:

答案 0 :(得分:10)

除非您真的必须实施自己的解决方案,否则可以使用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";

答案 1 :(得分: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_element中的std::max_elementstd::minmax_elementC++11,而不是编写循环。

答案 2 :(得分:2)

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
}

答案 3 :(得分:1)

在初始化数组之前分配大小,即大小都假定此时堆栈上的任何值都是。因为它们只是普通值类型而没有引用,所以一旦将值[0]写入via cin&gt;&gt;,它们就不会假设新值。

只需在第一次循环后移动作业,就可以了。

答案 4 :(得分: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),如果这些数字大于或小于任何数字在你的数组中的其他值,你将把它们作为输出。