【发布时间】:2021-06-18 08:51:33
【问题描述】:
我正在尝试获取数组的最小值和最大值。由用户创建的数组。我不断收到Segmentation fault (core dumped)。我不知道我哪里做错了。
#include <stdio.h>
int main(void){
int n, i;
double sum = 0.0, array[n], avg;
printf("Enter the size of the array:");
scanf("%d", &n);
for (i=0; i<n; ++i){
printf("Enter the number for position %d: ", i + 1);
scanf("%i", &n);
sum += n;
}
avg = (double) sum / n;
printf("Average = %.2f\n", avg);
double largest = array[0], smallest = array[0];
for (i = 0; i < n; i++){
if (array[i] > largest)
{
largest = array[i];
}
else if (array[i] < smallest)
{
smallest = array[i];
}
}
printf("The smallest is %lf and the largest is %lf!\n", smallest, largest);
}
编辑:解决这个问题后,我看到我也无法获得最小值和最大值。我一直为两者提供0.000000。我该如何解决?我尝试将double 更改为float,但没有成功..
【问题讨论】:
标签: c segmentation-fault average coredump