Find the largest number in an array using the C language.
To find the largest number in an array, you can use a loop to iterate through each element of the array, setting the first element as the assumed maximum value. Then, compare each subsequent element to the assumed maximum value, updating the maximum value if a larger number is found. Here is an example code:
#include <stdio.h>
int main() {
int arr[] = {1, 5, 3, 9, 2}; // 示例数组
int size = sizeof(arr) / sizeof(arr[0]); // 数组长度
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 code will output the largest number as 9.