4

考虑:

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

到目前为止,这是我的代码。我遇到的问题是它打印出最大数量的数组。与将第一个元素分配给最高和最低值有关。如果我分开做,它会起作用。有什么建议么?

4

5 回答 5

11

除非您真的必须实施自己的解决方案,否则您可以使用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";
于 2013-04-30T11:38:34.723 回答
6
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_elementfromC++11来代替编写循环。

于 2013-04-30T11:37:32.670 回答
3
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
}
于 2013-04-30T11:45:53.807 回答
1

您在数组初始化之前分配 big 和 small,即 big 和 small 假定此时堆栈上的任何值。由于它们只是普通值类型并且没有引用,因此一旦通过 cin >> 写入 values[0],它们就不会假定新值。

只需在第一个循环之后移动作业,它应该没问题。

于 2013-04-30T11:38:50.477 回答
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),并且如果这些数字大于或小于数组中的任何其他值,您将它们作为输出。

于 2013-04-30T13:14:21.217 回答