在C中查找数组的峰值数

在C中查找数组的峰值数,c,arrays,algorithm,loops,C,Arrays,Algorithm,Loops,我是C语言的新手。我正在尝试编写一个代码来查找数组中的峰值数字,即大于该数字前后的数字 这是我的密码。它运行没有错误,但没有输出,所以我知道我做错了什么 #include <stdio.h> int main() { int nums[14] = {1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8}; int peaks[4]; for(int i = 0; i < nums[i]; i++){ if(nums[i] >

我是C语言的新手。我正在尝试编写一个代码来查找数组中的峰值数字,即大于该数字前后的数字

这是我的密码。它运行没有错误,但没有输出,所以我知道我做错了什么

#include <stdio.h>
int main() {
 int nums[14] = {1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8};
  int peaks[4];

 for(int i = 0; i < nums[i]; i++){
     if(nums[i] > nums[i-1] && nums[i] > nums[i+1]){
         peaks == nums[i];
     }
     return peaks;
 }

 printf("Peak numbers are %d",peaks);
}
#包括
int main(){
int nums[14]={1,2,3,3,2,4,1,5,6,3,1,10,2,8};
int峰[4];
for(int i=0;inums[i-1]&&nums[i]>nums[i+1]){
峰值==nums[i];
}
返回峰值;
}
printf(“峰值数为%d”,峰值);
}

我怎样才能让它输出这个结果:
[4,6,10,8]

启用编译器的警告!!!你应该得到这样的东西:

prog.c: In function 'main':
prog.c:8:16: warning: comparison between pointer and integer
          peaks == nums[i];
                ^~
prog.c:8:16: warning: statement with no effect [-Wunused-value]
          peaks == nums[i];
          ~~~~~~^~~~~~~~~~
prog.c:10:13: warning: returning 'int *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]
      return peaks;
             ^~~~~
prog.c:10:13: warning: function returns address of local variable [-Wreturn-local-addr]
prog.c:13:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
  printf("Peak numbers are %d",peaks);
                           ~^  ~~~~~
                           %ls
for(i = 0; i < 4; ++i)
    printf("%d\n", peaks[i]);
因此,改变这一点:

 peaks == nums[i];
for(i = 0; i < nums[i]; i++)
为此:

 peaks[j] = nums[i];
for(i = 0; i < 14; i++)
其中
j
是您可以使用的另一个计数器

然后你返回峰值,为什么?它不是自定义函数,因此不应返回

此外,您还可以尝试打印如下数组:

printf("Peak numbers are %d",peaks);
这是不可能的,您需要在数组的元素上循环,如下所示:

prog.c: In function 'main':
prog.c:8:16: warning: comparison between pointer and integer
          peaks == nums[i];
                ^~
prog.c:8:16: warning: statement with no effect [-Wunused-value]
          peaks == nums[i];
          ~~~~~~^~~~~~~~~~
prog.c:10:13: warning: returning 'int *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]
      return peaks;
             ^~~~~
prog.c:10:13: warning: function returns address of local variable [-Wreturn-local-addr]
prog.c:13:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
  printf("Peak numbers are %d",peaks);
                           ~^  ~~~~~
                           %ls
for(i = 0; i < 4; ++i)
    printf("%d\n", peaks[i]);

i
为0且
i
为13时。

启用编译器的警告!!!你应该得到这样的东西:

prog.c: In function 'main':
prog.c:8:16: warning: comparison between pointer and integer
          peaks == nums[i];
                ^~
prog.c:8:16: warning: statement with no effect [-Wunused-value]
          peaks == nums[i];
          ~~~~~~^~~~~~~~~~
prog.c:10:13: warning: returning 'int *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]
      return peaks;
             ^~~~~
prog.c:10:13: warning: function returns address of local variable [-Wreturn-local-addr]
prog.c:13:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
  printf("Peak numbers are %d",peaks);
                           ~^  ~~~~~
                           %ls
for(i = 0; i < 4; ++i)
    printf("%d\n", peaks[i]);
因此,改变这一点:

 peaks == nums[i];
for(i = 0; i < nums[i]; i++)
为此:

 peaks[j] = nums[i];
for(i = 0; i < 14; i++)
其中
j
是您可以使用的另一个计数器

然后你返回峰值,为什么?它不是自定义函数,因此不应返回

此外,您还可以尝试打印如下数组:

printf("Peak numbers are %d",peaks);
这是不可能的,您需要在数组的元素上循环,如下所示:

prog.c: In function 'main':
prog.c:8:16: warning: comparison between pointer and integer
          peaks == nums[i];
                ^~
prog.c:8:16: warning: statement with no effect [-Wunused-value]
          peaks == nums[i];
          ~~~~~~^~~~~~~~~~
prog.c:10:13: warning: returning 'int *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion]
      return peaks;
             ^~~~~
prog.c:10:13: warning: function returns address of local variable [-Wreturn-local-addr]
prog.c:13:28: warning: format '%d' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
  printf("Peak numbers are %d",peaks);
                           ~^  ~~~~~
                           %ls
for(i = 0; i < 4; ++i)
    printf("%d\n", peaks[i]);

i
为0且
i
为13时。

这里是程序的更正版本,您的错误在注释中:

#include <stdio.h>
int main() {
  int nums[14] = {1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8};
  int peaks[4];
  int ctr=0;          // counter to count the amount of peaks

  for(int i = 1; i < 13; i++){   // start with one and end with 12, so you don't access elements outside of your array (-1 and 14)
                                 // You need to implement special cases for the first and the last element
    if(nums[i] > nums[i-1] && nums[i] > nums[i+1]){
      peaks[ctr] = nums[i];   // use an assignment instead of a comparison
      ctr++;                  // increment counter
    }
                              // No return here, it will end your main and the print will never be reached
  }

  printf("Peak numbers are ");
  for(int i = 0; i<ctr; i++)
    printf("%d ", peaks[i]);   // Print all the peaks you found, you need to print every element separate 

  return 0;  // main needs a return value
}
#包括
int main(){
int nums[14]={1,2,3,3,2,4,1,5,6,3,1,10,2,8};
int峰[4];
int ctr=0;//计数器用于计算峰值的数量
对于(inti=1;i<13;i++){//以1开头,以12结尾,因此您不会访问数组(-1和14)之外的元素
//您需要为第一个和最后一个元素实现特殊情况
if(nums[i]>nums[i-1]&&nums[i]>nums[i+1]){
峰值[ctr]=nums[i];//使用赋值而不是比较
ctr++;//递增计数器
}
//在这里不返回,它将结束您的主要和打印将永远无法达到
}
printf(“峰值数为”);

对于(int i=0;i这里您有一个程序的更正版本,您的错误在注释中:

#include <stdio.h>
int main() {
  int nums[14] = {1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8};
  int peaks[4];
  int ctr=0;          // counter to count the amount of peaks

  for(int i = 1; i < 13; i++){   // start with one and end with 12, so you don't access elements outside of your array (-1 and 14)
                                 // You need to implement special cases for the first and the last element
    if(nums[i] > nums[i-1] && nums[i] > nums[i+1]){
      peaks[ctr] = nums[i];   // use an assignment instead of a comparison
      ctr++;                  // increment counter
    }
                              // No return here, it will end your main and the print will never be reached
  }

  printf("Peak numbers are ");
  for(int i = 0; i<ctr; i++)
    printf("%d ", peaks[i]);   // Print all the peaks you found, you need to print every element separate 

  return 0;  // main needs a return value
}
#包括
int main(){
int nums[14]={1,2,3,3,2,4,1,5,6,3,1,10,2,8};
int峰[4];
int ctr=0;//计数器用于计算峰值的数量
对于(inti=1;i<13;i++){//以1开头,以12结尾,因此您不会访问数组(-1和14)之外的元素
//您需要为第一个和最后一个元素实现特殊情况
if(nums[i]>nums[i-1]&&nums[i]>nums[i+1]){
峰值[ctr]=nums[i];//使用赋值而不是比较
ctr++;//递增计数器
}
//在这里不返回,它将结束您的主要和打印将永远无法达到
}
printf(“峰值数为”);
对于(int i=0;i此程序适用于
nums
数组中的任意数量的元素,它正确地考虑了第一个和最后一个元素

[sizeof(nums)/sizeof(nums[0])
nums
数组中的实际元素数,因此yolu可以将任意数量的元素放入
nums
数组中,程序将始终正常工作

另外,
peaks
的元素数不再硬编码为
4
,而是与
num
中的元素数相同,因此我们可以确保不会出现任何索引越界问题。但是,由于大小为
n
的数组的最大峰值数为
n/2+1
(不太确定),我们可能可以编写
int peaks[sizeof(nums)/sizeof(nums[0])/2+1]

#include <stdio.h>

int main() {
  int nums[] = { 1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8 };
  int peaks[sizeof(nums)/sizeof(nums[0])];
  int pi = 0;

  for (int i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) {
    if (
        (i == 0 && nums[i + 1] < nums[i]) ||   /* first element */
        (i == sizeof(nums) / sizeof(nums[0]) && nums[i - 1] < nums[i]) || /* last element */
        (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) /* elements in the middle */
      )
    {
      peaks[pi++] = nums[i];
    }
  }

  for (int i = 0; i < pi; i++)
    printf("%d ", peaks[i]);
}
#包括
int main(){
int nums[]={1,2,3,3,2,4,1,5,6,3,1,10,2,8};
整数峰值[sizeof(nums)/sizeof(nums[0]);
int-pi=0;
对于(int i=0;i
但是,
if
语句中的条件可以用更优雅的方式编写。

此程序适用于
nums
数组中的任意数量的元素,它正确地考虑了第一个和最后一个元素

[sizeof(nums)/sizeof(nums[0])
nums
数组中的实际元素数,因此yolu可以将任意数量的元素放入
nums
数组中,程序将始终正常工作

另外,
peaks
的元素数不再硬编码为
4
,而是与
num
中的元素数相同,因此我们可以确保不会出现任何索引越界问题。但是,由于大小为
n
的数组的最大峰值数为
n/2+1
(不太确定),我们可能可以编写
int peaks[sizeof(nums)/sizeof(nums[0])/2+1]

#include <stdio.h>

int main() {
  int nums[] = { 1, 2, 3, 3, 2, 4, 1, 5, 6, 3, 1, 10, 2, 8 };
  int peaks[sizeof(nums)/sizeof(nums[0])];
  int pi = 0;

  for (int i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) {
    if (
        (i == 0 && nums[i + 1] < nums[i]) ||   /* first element */
        (i == sizeof(nums) / sizeof(nums[0]) && nums[i - 1] < nums[i]) || /* last element */
        (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) /* elements in the middle */
      )
    {
      peaks[pi++] = nums[i];
    }
  }

  for (int i = 0; i < pi; i++)
    printf("%d ", peaks[i]);
}
#包括
int main(){
int nums[]={1,2,3,3,2,4,1,5,6,3,1,10,2,8};
整数峰值[sizeof(nums)/sizeof(nums[0]);
int-pi=0;
对于(int i=0;i