我正在做一些练习,为我的测试做准备,在其中一个练习中,我必须从数组中删除重复的int值:
int *eliminaDup(int *vect, int dim, int *dim_nodup){
int i = 0, newDim = 0, found = 0, j = 0;
int *tmpArr = malloc(dim * sizeof(int));
for(i = 0; i < dim; i++){
j = 0; found = 0;
while(j < newDim && !found){
if(vect[i] == tmpArr[j])
found = 1;
j++;
}
if(!found){
tmpArr[newDim] = vect[i];
newDim++;
}
}
*dim_nodup = newDim;
return (realloc(tmpArr, newDim * sizeof(int)));
}
在主方法中是这样调用的:
nodup=eliminaDup(input,dim,&dim_nodup);
printf("Print of the new Array: (%d values)\n", dim_nodup);
for (i=0; i<dim_nodup; i++){
printf("%d\n",nodup[i]);
}
但当我试图执行代码时,This happens:
输入中给定的数组:
[1;2]
输出:
1
2
预期输出:
1
2
...other output from the code...
从屏幕上可以看到,代码应该继续打印其他内容。
我做了一些尝试,我看到密码“锁”正是印刷后,但它从来没有从为出来。
…为什么?我的头撞在键盘上了。
编辑:完整程序
#include <stdio.h>
#include <stdlib.h>
int *leggiInput(int *dim);
int *eliminaDup(int *vect, int dim, int *dim_nodup);
int ugualeASomma(int *vect,int dim);
int *maggioreDeiSuccessivi(int *vect, int dim);
int main()
{
int *input, *nodup, *results;
int dim, dim_nodup, i;
//Legge l'input
input=leggiInput(&dim);
printf("Stampa dei valori in input: (%d valori)\n", dim);
for (i=0; i<dim; i++)
printf("%d\n",input[i]);
//Elimina i duplicati
nodup=eliminaDup(input,dim,&dim_nodup);
printf("Stampa dei valori senza duplicati: (%d valori)\n", dim_nodup);
for (i=0; i<dim_nodup; i++){
printf("%d\n",nodup[i]);
}
//Esegue ugualeASomma
printf("Risultato di ugualeASomma: %d\n", ugualeASomma(nodup,dim_nodup));
//Esegue maggioreDeiSuccessivi
results=maggioreDeiSuccessivi(nodup,dim_nodup);
printf("Risultato maggioreDeiSuccessivi:\n");
for(i=0; i<dim_nodup; i++)
printf("%d\n",results[i]);
return 0;
}
int *leggiInput(int *dim){
int n, i;
scanf("%d", &n);
int *arr = malloc(n * sizeof(int));
for(i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
*dim = n;
return arr;
}
int *eliminaDup(int *vect, int dim, int *dim_nodup){
int i = 0, newDim = 0, trovato = 0, j = 0;
int *tmpArr = malloc(dim * sizeof(int));
while(i < dim){
j = 0; trovato = 0;
while(j < newDim && !trovato){
if(vect[i] == tmpArr[j])
trovato = 1;
j++;
}
if(!trovato){
tmpArr[newDim] = vect[i];
newDim++;
}
i++;
}
*dim_nodup = newDim;
return (realloc(tmpArr, newDim * sizeof(int)));
}
int ugualeASomma(int *vect, int dim){
int somma = 0, i = 0, j, trovato = 0;
while(i < dim)
somma += vect[i];
while(i < dim){
if(vect[i] == somma - vect[i])
trovato = 1;
}
return trovato;
}
int *maggioreDeiSuccessivi(int *vect, int dim){
int i = 0, j, trovato;
while(i < dim){
j = i+1; trovato = 0;
while(j < dim && !trovato){
if(vect[i] <= vect[j])
trovato = 1;
else
j++;
}
if(trovato) vect[i] = 0;
else vect[i] = 1;
i++;
}
return vect;
}
编辑:在将malloc更改为calloc的注释中解决。
最佳答案
当realloc失败时,它返回NULL,因此您应该检查它并返回tmpArr:
int* p = realloc(tmpArr, newDim * sizeof(int));
return p != NULL ? p : tmpArr;
最好初始化所有声明的变量,即使它们稍后将被初始化。以后可能会忘记它,并假设它是在函数增长时初始化的。
这里有一个无限循环
int ugualeASomma(int *vect, int dim){
int somma = 0, i = 0, j, trovato = 0;
while(i < dim)
somma += vect[i];
while(i < dim){
if(vect[i] == somma - vect[i])
trovato = 1;
}
return trovato;
}
我从不增加
关于c - C-我认为我在做重新分配错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34592147/