查找数组中的最小值
Finding smallest value in an array
我正试图编写一个函数来查找数组中的最小值,但我无法找出我所犯的错误。谁能帮我看一下吗?谢谢! !
void smallest(int array[],int size)
{
int smallest=array[0];
for (int i=1;i<size-1;i++)
{
if (array[i]<smallest)
smallest=array[i];
}
cout<<smallest<<'n';
}
代码没有执行,因为有断点,我看不出问题在哪里。
你可以在这里找到一个更有效的解决方案:
查找数组中最小值的最有效方法
代码片段:
int smallest = array[0];
for (int i = 0; i < array_length; i++) {
if (array[i] < smallest) {
smallest = array[i];
}
}
你的循环不是指向数组的最后一个元素。你应该去掉条件的- 1。不要犹豫,测试接收到的参数的值:
void smallest(int array[], int size)
{
if (size <= 0 || !array)
return;
int smallest = array[0];
for (int i = 1; i < size ; i++)
if (array[i] < smallest)
smallest = array[i];
cout << smallest << 'n';
}
相关文章:
- 使用递归的数组的最小值.这是怎么回事
- 在二维数组中查找最小值和最大值?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 需要使用模板查找数组的第二个最小和最小值
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 查找包含 N 个元素的数组的最小值和最大值
- 在不进行排序的情况下查找数组中n个最小值的索引
- 在索引0为0的数组中查找非零最小值C++
- 使用数组将最后一个最大值更改为第一个最小值
- 以 C/C++ 为单位的数组中最小值的地址
- 如何在给定数组的任何子数组(任何大小)中找到最大值(或最小值)?
- 有没有办法在递归中使用指针来查找数组的最小值?
- 构造大小为 N 的数组 A 和大小为 1 的数组 A,使得所有 A[i]*A[j] 的总和为最小值和正数。1 <= i < j <= N
- C++数组的最大值、最小值、平均值函数
- 数组中最小值的总和
- C 递归找到数组的最小值
- 从二维数组中查找最小值和最大值
- 使用函数从数组中索引最小值的问题
- 查找最小字典数组