forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shashank.cpp
29 lines (22 loc) · 780 Bytes
/
shashank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <vector>
int findLargestElement(const std::vector<int>& arr) {
if (arr.empty()) {
std::cerr << "Array is empty." << std::endl;
return -1; // Return an error code
}
int largest = arr[0]; // Assume the first element is the largest
// Iterate through the array to find the largest element
for (const int& num : arr) {
if (num > largest) {
largest = num; // Update largest if current element is greater
}
}
return largest; // Return the largest element found
}
int main() {
std::vector<int> arr = {10, 5, 20, 8};
int largestElement = findLargestElement(arr);
std::cout << "The largest element in the array is: " << largestElement << std::endl;
return 0;
}