C++ 查找数组中最大和最小的数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16298906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find largest and smallest number in an array
提问by user2204993
Consider:
考虑:
#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
}
This is my code so far. The problem I am having is with it printing out the biggest number of the array. Something to do with assigning the first element to the highest and lowest value. It works if I do them separately. Any suggestions?
到目前为止,这是我的代码。我遇到的问题是它打印出数组的最大数量。与将第一个元素分配给最高和最低值有关。如果我分开做,它会起作用。有什么建议?
回答by juanchopanza
Unless you really must implement your own solution, you can use std::minmax_element. This returns a pair of iterators, one to the smallest element and one to the largest.
除非您真的必须实现自己的解决方案,否则您可以使用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";
回答by ForEveR
big=small=values[0]; //assigns element to be highest or lowest value
Should be AFTER
fill loop
应该是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
since when you declare array - it's unintialized
(store some undefined values) and so, your big
and small
after assigning would store undefined
values too.
因为当您声明数组时 - 它是unintialized
(存储一些未定义的值),因此,您的big
和small
分配后也会存储undefined
值。
And of course, you can use std::min_element
, std::max_element
, or std::minmax_element
from C++11
, instead of writing your loops.
当然,您可以使用std::min_element
,std::max_element
或std::minmax_element
from C++11
,而不是编写循环。
回答by Xiou Raxau
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
}
回答by misberner
You assign to big and small before the array is initialized, i.e., big and small assume the value of whatever is on the stack at this point. As they are just plain value types and no references, they won't assume a new value once values[0] is written to via cin >>.
您在数组初始化之前分配给 big 和 small,即 big 和 small 假定此时堆栈中的任何值。由于它们只是普通值类型并且没有引用,因此一旦通过 cin >> 写入 values[0] ,它们就不会假设新值。
Just move the assignment after your first loop and it should be fine.
只需在您的第一个循环之后移动分配,它应该没问题。
回答by M.A
You can initialize after filling the array or you can write:
您可以在填充数组后初始化,也可以编写:
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
integer can hold.
整数可以容纳。
big =- 1*(small) - 1;
instead of:
代替:
big = small = values[0]
because when you write this line before filling the array, big and small values will equal to a random leftover value (as integer is a POD) from the memory and if those numbers are either bigger or smaller than any other value in you array, you will get them as an output.
因为当您在填充数组之前编写此行时,大小值将等于内存中的随机剩余值(因为整数是POD),并且如果这些数字大于或小于数组中的任何其他值,则您将它们作为输出。