C 在数组中查找最大元素

C 在数组中查找最大元素,c,pointers,random,casting,dynamic-allocation,C,Pointers,Random,Casting,Dynamic Allocation,我有一个数组,我必须找到其中最大的元素,告诉它有多少次在什么位置。我必须使用指针和动态内存分配 随机填充数组时,它不起作用,但手动填充时效果良好 此代码正常工作: #include <stdio.h> #include <stdlib.h> void max_elem (int n, float* vett) { int i=0, *pos, p=0, n_ele_pos; float max=*(vett); pos=(int*)malloc(size

我有一个数组,我必须找到其中最大的元素,告诉它有多少次在什么位置。我必须使用指针和动态内存分配

随机填充数组时,它不起作用,但手动填充时效果良好

此代码正常工作:

#include <stdio.h>
#include <stdlib.h>

void max_elem (int n, float* vett) {

  int i=0, *pos, p=0, n_ele_pos;
  float max=*(vett);

  pos=(int*)malloc(sizeof(int));
  if (pos==NULL) {printf("BUM\n" ); exit (0);}
  *(pos)=0;
  for (i=1; i<n; i++) {
    if (*(vett+i)>max) {
      max=*(vett+i);
      p=0;
      *(pos)=i;
    }
    else  if (*(vett+i)==max) {
      p++;
      *(pos+p)=i;
    }
  }
  if (p==0) {
    printf("\nmax element is %f, in position %d  ", max, *pos+1);
  }
  else {
    printf("\n max element %f, %d times in position\n", max, p+1);
    for (i=0; i<p+1; i++) {
    printf("%d  ", *(pos+i)+1);
    }
  }
  printf("\n\n");
  free(pos);
}

int main()
{
    int i,n;
    float *element;
    printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n");
    printf("-------------------------------------------------------------------------\n");
    printf(" Input total number of elements(1 to 100): ");
    scanf("%d",&n);
    element=(float*)calloc(n,sizeof(float));  // Memory is allocated for 'n' elements
    if(element==NULL)
    {
        printf(" No memory is allocated.");
        exit(0);
    }
    printf("\n");
    for(i=0;i<n;++i)
    {
       printf(" Number %d: ",i+1);
       scanf("%f",element+i);
    }

    max_elem (n, element);
    return 0;
}
#包括
#包括
无效最大元素(内部n,浮动*vett){
int i=0,*pos,p=0,n_ele_pos;
浮动最大值=*(vett);
pos=(int*)malloc(sizeof(int));
如果(pos==NULL){printf(“BUM\n”);退出(0);}
*(pos)=0;
对于(i=1;imax){
最大值=*(vett+i);
p=0;
*(pos)=i;
}
如果(*(vett+i)=最大值){
p++;
*(pos+p)=i;
}
}
如果(p==0){
printf(“\n最大元素为%f,在位置%d”,最大,*pos+1);
}
否则{
printf(“\n最大元素%f,%d次在位置\n”,最大,p+1);

对于(i=0;i让问题简化一点

我有一个数组,我必须找到其中最大的元素,告诉它有多少次,在什么位置。我必须使用指针和动态内存分配

因此,在最坏的情况下,当数组包含相同的元素时,您将拥有与元素相同的位置数。如果您不需要优化代码到内存的使用,那么您可以分配两个长度相同的数组(一个用于数字,一个用于位置)

下一个简化选项是在填充数组时查找最大值。(无论fillig方法使用随机读取数还是用户输入数,都可以在将元素逐个插入数组时确定最大值。)

也许您可以找到更多的简化选项,但有了这些选项,您可以将
max\u elem
功能抛在脑后:

#include <stdio.h>
#include <stdlib.h>
#include <float.h> /* for FLT_MIN */

void assert_alloc(void* ptr) {
    if(ptr == NULL) {
        printf("No memory is allocated.\n");
        exit(0);
    }
}

int main() {
    int i, n, k = 0, *positions;
    float max = FLT_MIN, *elements; /* max is now the smallest possible float */

    printf("Input total number of elements(1 to 100): ");
    scanf("%d", &n);

    /* allocate array of same size for numbers and for positions */
    elements = (float*)calloc(n, sizeof(float));
    assert_alloc(elements);

    positions = (int*)calloc(n, sizeof(int));
    assert_alloc(positions);

    /* fill the array and find the maximum meanwhile */
    for(i = 0; i < n; ++i) {
        printf("Number %d: ", i + 1);
        scanf("%f", elements + i);

        if(max < *(elements + i))
            max = *(elements + i);
    }
    printf("\nmax element is %f, ", max); /* max is already known */

    /* find the positions of the maximal number */
    for(i = 0; i < n; ++i) {
        if(max == *(elements + i)) {
            *(positions + k) = i;
            k++;
        }
    }

    /* print the positions of the maximal number */
    if(k > 1) {
        printf("%d times in position\n", k);
        for(i = 0; i < k; i++)
            printf("%d  ", *(positions + i) + 1);
    }
    else {
        printf("in position %d", *positions + 1);
    }    
    printf("\n");

    free(elements);
    free(positions);
    return 0;
}
完成此操作后,您的第二个示例将适用于
float
类型。它不适用于
int
类型的原因是
max_elem
只接受
float
指针。您将
int
指针作为
max_elem(n,(float*)vett)传递给该函数;
这是错误的,因为内存中以不同的方式表示了
int
float
。要解决此问题,请将
vett
指针声明为
float*
,而不管是用
int
还是
float
值填充它


pos
仅分配给一个
int
的大小。如果
p
为0,则
*(pos+p)=i;
是未定义的行为吗
max_elem
在数组中找到max元素对我来说似乎有点太大了…为什么你需要动态分配局部变量,比如
pos
?来添加更多可能的bug?@yano所以我必须使用calloc?对不起,我是个新手,我不太理解你所说的。我建议你学习一些调试编程技巧。首先一步一步地写下您希望代码执行的操作。然后使用调试器或cout语句验证您的代码实际执行的操作。最后,找出您期望的操作与实际结果不同的原因。@EugeneSh。我使用动态分配是因为我不想创建一个n大小的静态ArrayTanks!无论如何,这不是一个她编写代码的方法。你能帮我修改我的代码吗?我编辑了我的答案,并指出了你第二个例子的主要问题。我认为它还没有结束。我做了很多测试(所有测试都使用相同的输入)似乎在出现一次以上的“max”时,我们都会得到错误或不完整的输出。不管怎样,现在我可以使用大数组。我也测试了它,对我来说它工作得很好。正如我所看到的,示例失败是因为
float
。您可能会在输出上看到相同的数字,因为打印的数字的数量是比较的它们可以是不同的数字。要将
float
值四舍五入到小数点后的特定位数,请使用。
#include <stdio.h>
#include <stdlib.h>
#include <float.h> /* for FLT_MIN */

void assert_alloc(void* ptr) {
    if(ptr == NULL) {
        printf("No memory is allocated.\n");
        exit(0);
    }
}

int main() {
    int i, n, k = 0, *positions;
    float max = FLT_MIN, *elements; /* max is now the smallest possible float */

    printf("Input total number of elements(1 to 100): ");
    scanf("%d", &n);

    /* allocate array of same size for numbers and for positions */
    elements = (float*)calloc(n, sizeof(float));
    assert_alloc(elements);

    positions = (int*)calloc(n, sizeof(int));
    assert_alloc(positions);

    /* fill the array and find the maximum meanwhile */
    for(i = 0; i < n; ++i) {
        printf("Number %d: ", i + 1);
        scanf("%f", elements + i);

        if(max < *(elements + i))
            max = *(elements + i);
    }
    printf("\nmax element is %f, ", max); /* max is already known */

    /* find the positions of the maximal number */
    for(i = 0; i < n; ++i) {
        if(max == *(elements + i)) {
            *(positions + k) = i;
            k++;
        }
    }

    /* print the positions of the maximal number */
    if(k > 1) {
        printf("%d times in position\n", k);
        for(i = 0; i < k; i++)
            printf("%d  ", *(positions + i) + 1);
    }
    else {
        printf("in position %d", *positions + 1);
    }    
    printf("\n");

    free(elements);
    free(positions);
    return 0;
}
pos = (int*)malloc(n * sizeof(int));