简单小程序——求最大值

在编写去=求最大值程序时需要我们注意一点,需要给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;
}

猜你喜欢

转载自blog.csdn.net/ffsiwei/article/details/80217531