C++ 简单数组查找最高和最低值的索引位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15734884/
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
Simple Array Finding Index Position of Highest and Lowest Value
提问by Klinetel
I am trying to write a simple logical program to find the highest and lowest values, which has already been done. The issue is how to find the index position is when it finds the highest and lowest value. See the attached picture.
我正在尝试编写一个简单的逻辑程序来查找已经完成的最高和最低值。问题是如何找到索引位置是什么时候找到最高和最低值。见附图。
#include <iostream>
using namespace std;
int main()
{
int number=0, min=0, max=0;
int *rangeOfNumbers = new int[];
cout<<"Enter 5 numbers: ";
for(int i=0; i<5;i++)
{
cout<<"Enter number "<<i+1<<": ";
cin>>rangeOfNumbers[i];
}
//max min array positions
min=rangeOfNumbers[0];
max=rangeOfNumbers[0];
//find max and mins
for(int j=0; j<5;j++)
{
if(max<rangeOfNumbers[j])
{
max=rangeOfNumbers[j];
}
else if(min>rangeOfNumbers[j])
{
min=rangeOfNumbers[j];
}
}
cout<<"\n\nMin number: "<<min;
cout<<"\nMax number: "<<max;
cin.get();
cin.get();
return 0;
}
回答by maditya
In addition to updating min and max, also keep an index variable and update it.
除了更新 min 和 max 之外,还要保留一个索引变量并更新它。
//max min array positions
min=rangeOfNumbers[0];
max=rangeOfNumbers[0];
int minindex = 0;
int maxindex = 0;
//find max and mins
for(int j=0; j<5;j++)
{
if(max<rangeOfNumbers[j])
{
max=rangeOfNumbers[j];
maxindex = j;
}
if(min>rangeOfNumbers[j])
{
min=rangeOfNumbers[j];
minindex = j;
}
}
maxindex += 1;
minindex += 1;
回答by MostafaR
Change max
and min
to indexOfMax
and indexOfMin
, Because by storing index of maximum you can access both maximum index and maximum value.
更改max
andmin
到indexOfMax
and indexOfMin
,因为通过存储最大值的索引,您可以访问最大索引和最大值。
So you should change the max if
to something like this:
所以你应该把它改成max if
这样:
if(rangeOfNumbers[indexOfMax] < rangeOfNumbers[j])
{
indexOfMax = j;
}
Continue this change for other lines yourself.
自己对其他线路继续此更改。
回答by taocp
Not sure how you can even compile the code:
不确定如何编译代码:
int *rangeOfNumbers = new int[];
You need to specify a size when you new an array of integers.
当您新建一个整数数组时,您需要指定一个大小。
int *rangeOfNumbers = new int[5];
I compiled under gcc 4.5.3, got the following error:
我在gcc 4.5.3下编译,出现如下错误:
error: expected primary-expression before ‘]' token
You also need to remember the indices for max
and min
when you scan the array.
您还需要记住扫描数组的索引max
和min
时间。
For example:
before the for
loop, initialize:
例如:在for
循环之前,初始化:
int maxIndex = -1;
Inside for loop:
for循环内部:
if (max < A[i])
{
maxIndex = i;
max = A[i];
}
Similar stuff should be done for min
.
应该为min
.
回答by taocp
This might be far from what you are looking for right now. However you should get used to stl containers (vectors, lists and all that stuff) if you are going to do some intensive algorithms like finding max and min.
这可能与您现在正在寻找的相去甚远。但是,如果您要执行一些诸如查找最大值和最小值之类的密集算法,则应该习惯 stl 容器(向量、列表和所有这些东西)。
The benefit is that many algorithms are already available and are optimized for performance.
好处是许多算法已经可用并且针对性能进行了优化。
I think your example is just an exercise. anyways her is how to if it wasn't
我认为你的例子只是一个练习。不管怎样,如果不是,她会怎样
For example this problem is ideal for using a vector. Here is an example
例如,这个问题非常适合使用向量。这是一个例子
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int number=0, min=0, max=0;
vector<int> rangeOfNumbers;
cout<<"Enter 5 numbers: ";
for(int i=0; i<5;i++)
{
cout<<"Enter number "<<i+1<<": ";
cin>>number;
rangeOfNumbers.push_back(number);
}
vector<int>::iterator maxelem = max_element(rangeOfNumbers.begin(), rangeOfNumbers.end());
vector<int>::iterator minelem = min_element(rangeOfNumbers.begin(), rangeOfNumbers.end());
cout << endl << "Max number: " << (*maxelem) << " at " << std::distance(rangeOfNumbers.begin(), maxelem) + 1;
cout << endl << "Min number: " << (*minelem)<< " at " << std::distance(rangeOfNumbers.begin(), minelem) + 1;
cin.get();
return 0;
}
回答by Maikel Luis
I would suggest a tiny optimization, if you initialize minand maxat item 0 why ask again for the same element
我会建议一个微小的优化,如果你在项目 0初始化min和max为什么再次要求相同的元素
for (int I = 0;
对于 (int I = 0;
in place
到位
for (int I = 1;
对于 (int I = 1;
The source code:
源代码:
#include <iostream>
using namespace std;
#define SIZE 5
int main()
{
int min=0, max=0;
int *rangeOfNumbers = new int[SIZE];
cout<<"Enter 5 numbers: ";
for(int i=0; i < SIZE; i++)
{
cout<<"Enter number " << i + 1 <<": ";
cin>>rangeOfNumbers[i];
}
//max min array positions
min = rangeOfNumbers[0];
max = rangeOfNumbers[0];
int minindex = 0;
int maxindex = 0;
//find max and mins
for(int j = 1; j < SIZE; j++)
{
if( max < rangeOfNumbers[j])
{
max = rangeOfNumbers[j];
maxindex = j;
}
if( min > rangeOfNumbers[j])
{
min = rangeOfNumbers[j];
minindex = j;
}
}
maxindex += 1;
minindex += 1;
cout<<"\n\nMin number: "<<min;
cout<<"\nMax number: "<<max;
cin.get();
cin.get();
return 0;
}