为什么小程序最后结果很奇怪?
程序目的是希望找到 10 个数中最大的那个#
主函数主要录入数字和调用 max_min 函数还有输出结果
max_min 函数用指针来筛选数组中的最大最小值
具体程序如下:#
# include <stdio.h>
# define N 10
void max_min (int a[], int n, int *max, int *min)
{
int i;
*max = a[0];
*min = a[0];
for (i = 1; i < n; i++) {
*max = (*max <= a[i] ? a[i] : *max);
*min = (*min <= a[i] ? *min : a[i]);
}
}
int main (void)
{
int i, a[N], big, small;
printf("\n\t Enter %d numbers. ", N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
max_min(a, N, &big, &small);
printf("\n\t the biggest number is: %d", big);
printf("\n\t the smallest number is: %d", small);
printf("\n\n");
return 0;
}
程序输出结果:#
Enter 10 numbers. 1,2,3,4,5,6,7,8,9,0
the biggest number is: 4199136
the smallest number is: -650977440
问题在哪里呢?
请大家不吝赐教,谢谢。
推荐文章: