在编写去=求最大值程序时需要我们注意一点,需要给max的初值赋为数组中的第一个元素,而不是0,这样考虑到了,数组中全为负数的情况。
欢迎各位大佬的指点。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int arr[10] = { 11,45,1,78,23,90,7,45,67,4};
int max = arr[i];
int len = sizeof(arr) / sizeof(arr[0]);
for (i = 1; i < len; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
printf("最大值为:%d", max);
system("pause");
return 0;
}
该博客介绍了如何使用C语言编写求解数组最大值的程序,特别强调了初始最大值应设为数组首元素,以避免全负数数组的情况。
1071

被折叠的 条评论
为什么被折叠?



