Find Max in Array C: Quick Tutorial
You can use a loop to iterate through an array and use a variable to keep track of the current maximum value found. Here is an example code:
#include <stdio.h>
int main() {
int arr[] = {10, 5, 20, 15, 25};
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 this example, the array arr contains 5 elements. We assume the first element is the maximum value, then we use a loop to iterate through the array starting from the second element. If the current element being iterated is greater than the previously recorded maximum value, we update the maximum value. In the end, we output the maximum value.