Example: Largest Element in an array
Output
Enter the number of elements (1 to 100): 5 Enter number1: 34.5 Enter number2: 2.4 Enter number3: -35.5 Enter number4: 38.7 Enter number5: 24.5 Largest element = 38.70
This program takes n number of elements from the user and stores it in the arr array.
To find the largest element,
- the first two elements of array are checked and the largest of these two elements are placed in
arr[0] - the first and third elements are checked and largest of these two elements is placed in
arr[0]. - this process continues until the first and last elements are checked
- the largest number will be stored in the
arr[0]position
// storing the largest number at arr[0]
for (int i = 1; i < n; ++i) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
}
}