Find Largest Number in Array in C
To find the largest number in an array, you can follow these steps:
- Define a variable named max, with an initial value equal to the first element in the array.
- Iterate through each element of the array using a loop.
- During the loop, if the current element is greater than the maximum, assign the current element to the maximum.
- After the loop ends, the max variable will hold the largest value in the array.
Here is an example code implemented in C language.
#include <stdio.h>
int main() {
int arr[] = {10, 5, 8, 15, 3};
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 maximum number as 15.