计算数组中当前元素右侧的更大元素

编程入门 行业动态 更新时间:2024-10-26 08:27:57
本文介绍了计算数组中当前元素右侧的更大元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试计算大于数组右侧元素的元素数。

I am trying to count the number of elements greater than the element on the right side of the array. Here my function goes.

int* SurpassersKing(int input1_size, int* input1,int* output_size) { int i,k; int count[input1_size]; for (i = 0; i < input1_size; i++) count[i] = 0; for ( i = 0; i < input1_size; i++) { for ( k = i + 1; k <input1_size; k++) { if (input1[i] < input1[k]) { count[i]++; } } } return count; }

这是我在函数中计算数组中更大元素的函数。

This is my function where I am counting greater elements in an array.

因此,在下面的代码段中,我编写了main函数,声明了所有像veriable一样的output_size,counting array,i,k作为数组的索引并打印了东西,并调用计数函数。

So in this following code snippet i have wriiten the main function , declaring all the veriable like output_size,counting array ,i ,k as an index for the arrays and printing the stuff , and calling counting function .

int main() { int output_size; int* output; int ip1_size = 0; int ip1_i; scanf("%d\n", &ip1_size); int ip1[ip1_size]; for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) { int ip1_item; scanf("%d", &ip1_item); ip1[ip1_i] = ip1_item; } output = SurpassersKing(ip1_size,ip1,&output_size); int output_i; for(output_i=0; output_i < output_size; output_i++) { printf("%d\n", output[output_i]); } return 0; }

但是我没有得到所需的输出,所以我该怎么做才能改善这一点

but i am not getting the output required so what can i do to improve this.

推荐答案

因此,您的代码在尝试编译时发现了一些错误。

Oso your code has a few errors i found when i tried to compile.

  • 这是交流代码,因此请在函数SurpassersKing中使用#include

  • This is a c code so use #include

    返回不允许的数组计数。除非它是动态创建的,否则切勿返回局部变量。

    Inside the function SurpassersKing you are trying to return array count which is not allowed. Never return local variables unless it is dynamically created.

    output_size不会初始化。

    The output_size never gets initilized.

    这是最终代码:

    #include <stdio.h> #include<stdlib.h> int* SurpassersKing(int input1_size, int* input1) { int i,k; int * count = (int*)malloc(input1_size*sizeof(int)); for (i = 0; i < input1_size; i++) *(count + i) = 0; for ( i = 0; i < input1_size; i++) { for ( k = i + 1; k <input1_size; k++) { if (input1[i] < input1[k]) { count[i]++; } } } return count; } int main() { // your code goes here int output_size; int* output; int ip1_size = 0; int ip1_i; int output_i; printf("Enter the size:\n"); scanf("%d",&ip1_size); int ip1[ip1_size]; for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) { scanf("%d",%ip1[ip1_i]); } output = SurpassersKing(ip1_size,ip1); output_size = ip1_size; for(output_i=0; output_i < output_size; output_i++) { printf("%d\n",output[output_i]"); } return 0; }
  • 更多推荐

    计算数组中当前元素右侧的更大元素

    本文发布于:2023-11-30 05:43:15,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648919.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:元素   更大   组中

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!