数组内容显示不正确
Array contents are not displaying properly
这里是 C 初学者,我有一个正在开发的程序,不幸的是,该程序要么没有正确读取数组,要么没有正确显示内容。
这是我的
int main()
{
int size; // the number of elements
printf("Enter size of the array: \n");
scanf("%d", &size);
double *array = malloc(size*sizeof(int)); //memory allocated using malloc
if (array == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: \n");
for (int i = 0; i<size; ++i)
{
scanf("%d", &array[i]);
}
printf("Results:");
double min = array[0];
printf("\nmin = %.3lf ", min);
double max = array[size - 1];
printf("\nmax = %.3lf", max);
这是我的输出
double array = malloc(sizesizeof(int));
应该是:
double *array = malloc( size * sizeof(double) );
scanf("%d", &array[i]);
这里控制字符串错误,double应该是%lf
,不是%d
这里是 C 初学者,我有一个正在开发的程序,不幸的是,该程序要么没有正确读取数组,要么没有正确显示内容。
这是我的
int main()
{
int size; // the number of elements
printf("Enter size of the array: \n");
scanf("%d", &size);
double *array = malloc(size*sizeof(int)); //memory allocated using malloc
if (array == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: \n");
for (int i = 0; i<size; ++i)
{
scanf("%d", &array[i]);
}
printf("Results:");
double min = array[0];
printf("\nmin = %.3lf ", min);
double max = array[size - 1];
printf("\nmax = %.3lf", max);
这是我的输出
double array = malloc(sizesizeof(int));
应该是:
double *array = malloc( size * sizeof(double) );
scanf("%d", &array[i]);
这里控制字符串错误,double应该是%lf
,不是%d