【发布时间】:2014-06-10 03:15:31
【问题描述】:
我想从动态整数表中找到最小数字和摘要。我不知道为什么没有显示结果。我在 malloc 上做错了吗?我也可以使用 realloc 吗?
#include <stdlib.h>
#include <stdio.h>
int main()
{
int n,i,min,sum,xronos;
int* array;
printf("Give me how many numbers does the table will have: \n");
scanf("%d",&n);
array=(int*)malloc(n*sizeof(int));
for(i=1;i<=n;i++)
{
printf("Give the number %d",i);
printf("\n");
scanf("%d",&array[i]);
}
for(i=1;i<=n;i++)
{
sum=sum+array[i];
if (i=1)
{
min=array[i];
}
else
{
if (array[i]<min)
{
min=array[i];
}
}
}
printf("%d",sum);
printf("\n The answer is :",(n-2)*min+(sum-min));
getch();
return 0;
}
【问题讨论】:
-
也许 'for(i=1;i
-
两件事:首先don't cast the result of
malloc。其次,索引从零开始,以大小减一结束。 -
另外,你应该将'sum'初始化为零?
-
注意:查看
scanf()的返回值。