C++ 在C++;,寻找阵列中的重复峰值并计算脉冲
首先,我提前道歉,我对编程非常陌生,非常感谢您的帮助。我正在使用mbed对微控制器进行编程: 我的目标是检查数组的峰值:C++ 在C++;,寻找阵列中的重复峰值并计算脉冲,c++,arrays,mbed,C++,Arrays,Mbed,首先,我提前道歉,我对编程非常陌生,非常感谢您的帮助。我正在使用mbed对微控制器进行编程: 我的目标是检查数组的峰值: void peakvals(int array[],const int count);{ //count = size array = 10, meant to find peak vals in array int peakcount=0; //meant to record the number of
void peakvals(int array[],const int count);{ //count = size array = 10, meant to find peak vals in array
int peakcount=0; //meant to record the number of peak vals occurring
for (int i=0;i<peakcount;i++){
for (int j=0;j<i;j++){
if ( array[i]==array[j] ){
peakcount=1;
peakcount++;
}
}
void peakvals(int数组[],常量int计数);{//count=size array=10,用于查找数组中的峰值VAL
int peakcount=0;//用于记录出现的峰值VAL数
对于(int i=0;i,正如我在评论中所建议的,一个简单的函数可以如下所示:
int count_peaks(const int array[], const int arrsize)
{
int peakcount=0;
// NOTE: C++ index start at 0, so (arrsize-1) is the last element you can access in the array.
for (int i= 1; i < arrsize - 2; ++i)
{
// Temporary variables are just to clarify the code, you can do it one-liner below instead.
const int previous = array[i-1];
const int current = array[i];
const int next = array[i+1];
// if( array[i] > array[i-1] && array[i] > array[i+1])
if( current > previous && current > next)
peakcount++;
}
return peakcount;
}
int count_峰值(常量int数组[],常量int数组大小)
{
int peakcount=0;
/Note:C++索引从0开始,因此(ARRSIZE-1)是数组中最后一个元素。
对于(int i=1;i数组[i-1]&数组[i]>数组[i+1])
如果(当前>上一个和当前>下一个)
peakcount++;
}
返回峰值计数;
}
查看数组的上一个和下一个元素。的语法不好(即:代码未编译)。我的建议是:检查数组中从位置1开始到最后-1的每个元素,并检查相邻(上一个、下一个)位置是否低于当前位置。如果是,则增加脉冲数。