How to find the maximum value in a one-dimensional arra…

To find the maximum value of a one-dimensional array, you can iterate through each element of the array and compare it to the current maximum value, updating the maximum value. Below is an example code:

#include <stdio.h>

#define SIZE 5  // 数组大小

int main() {
    int arr[SIZE] = {5, 2, 8, 4, 1};

    int max = arr[0];  // 假设第一个元素为最大值

    for (int i = 1; i < SIZE; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    printf("最大值为:%d\n", max);

    return 0;
}

The running result is:

最大值为:8
bannerAds