How to find the maximum value of an array in C language?
Please refer to the following example of C code for finding the maximum value in an array:
#include <stdio.h>
int main() {
int arr[] = {5, 8, 3, 2, 7, 6};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("数组的最大值是:%d\n", max);
return 0;
}
In the above code, we defined an array of integer type called arr, and used a loop to iterate through each element in the array. We utilized a variable max to store the current maximum value found, initially set to the first element of the array arr[0]. In each iteration of the loop, we compared the current element with max, updating max to the value of the current element if it is greater. After the loop finishes, max contains the maximum value in the array. Lastly, we used the printf function to print the result.
Hope this helps you!