【发布时间】:2017-06-27 19:16:56
【问题描述】:
我有一个数组,我必须找到其中最大的元素,告诉它出现了多少次以及在什么位置。我必须使用指针和动态内存分配。
随机填充数组时不起作用,但手动填充时效果很好。
这段代码可以正常工作:
#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;
}
但下一个没有。我必须用我决定的特定范围内的随机数填充数组,即使是整数随机数的十进制到十进制极值(是的,我知道这实际上没有多大意义)。用十进制数字填充时搜索有时效果很好,而整数我只有 0。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float rand_float (float *, float *);
int rand_int (float *, float *);
void min_max(float *, float *, float *, float *);
int num_intero(float);
void max_elem (int, float*);
int main () {
int n, i, t;
float x1, x2;
printf ("array size:\t");
scanf ("%d", &n);
printf("\ninterval extremes:\t");
scanf ("%f %f", &x1, &x2);
do {
printf("1 for decimal random numbers, 2 for integers:\t");
scanf("%d", &t);
} while (t!=1 && t!=2);
srand(time(NULL));
switch (t) {
case 1 : {
float *vett;
vett=(float*)calloc(n, sizeof(float));
if (vett==NULL) {printf("BUM\n" ); exit (0);}
for (i=0;i<n;i++) {
*(vett+i)=rand_float(&x1, &x2);
printf("%d__\t%10f\n", i, *(vett+i));
}
max_elem (n, vett);
free(vett);
break;
}
case 2 : {
int *vett;
vett=(int*)calloc(n, sizeof(int));
if (vett==NULL) {printf("BUM\n" ); exit (0);}
for (i=0; i<n; i++) {
*(vett+i)=rand_int(&x1, &x2);
printf("%d__\t%10d\n", i, *(vett+i));
}
max_elem (n, (float*)vett);
free (vett);
break;
}
}
return 0;
}
void min_max (float*x1, float *x2, float *min, float *max) {
if (*x1<*x2) {
*min=*x1;
*max=*x2;
}
else {
*min=*x2;
*max=*x1;
}
}
float rand_float (float *x1, float *x2) {
float min, max;
min_max(x1, x2, &min, &max);
return ((max-min)*(float)rand()/RAND_MAX)+min;
}
int num_intero (float min) {
if (min/(int)min==1)
return 1; //e' intero
else return 0;
}
int rand_int (float *x1, float *x2) {
float min, max;
min_max(x1, x2, &min, &max);
if ((int)min==(int)max) {
return (int)min;
}
else if (min==0) {
return (rand()%((int)(max)+1));
}
else if (max==0) {
return (rand()%((int)(-min)+1)+min); //funziona anche con ((int)(min)-1)+min
}
else if ((int)min!=(int)max) {
if (num_intero (min)==1) {
return (rand()%((int)(max-min)+1)+min);
}
else if (num_intero (max)==0) {
return (rand()%((int)(max-min))+min+1);
}
else return (rand()%((int)(max-min)+1)+min+1);
}
}
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);
}
此外,当数组很大时,我会出现这样的运行时错误,而第一个代码则没有: error
示例: first code, second code and integers error(bottom)
我在 Ubuntu 16.04 (gnome) 上使用 gcc 和 atom 编辑器。 谢谢和抱歉。
【问题讨论】:
-
pos只分配给一个int的大小。如果p曾经 > 0 则*(pos+p)=i;是未定义的行为 -
max_elem在我看来似乎有点过大,无法在数组中找到最大元素... 为什么需要动态 分配像pos这样的局部变量?添加更多可能的错误? -
@yano 所以我必须使用 calloc 吗?抱歉,我是新手,没听懂你说的。
-
我建议你学习一些调试技巧。首先一步一步地写下你希望你的代码做什么。然后使用调试器或 cout 语句来验证您的代码实际执行的操作。最后,找出您所期望的结果与实际结果不同的原因。
-
@EugeneSh。我使用了动态分配,因为我不想创建一个 n 大小的静态数组
标签: c pointers random casting dynamic-allocation