fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int findLargestElement(const vector<int>& arr) {
  7. int maxElement = arr[0];
  8. for (int i = 1; i < arr.size(); i++) {
  9. if (arr[i] > maxElement) {
  10. maxElement = arr[i];
  11. }
  12. }
  13. return maxElement;
  14. }
  15.  
  16. int main() {
  17. int n;
  18. cout << "Enter the number of elements in the array: ";
  19. cin >> n;
  20.  
  21. vector<int> arr(n);
  22. cout << "Enter the elements of the array: ";
  23. for (int i = 0; i < n; i++) {
  24. cin >> arr[i];
  25. }
  26.  
  27. int maxElement = findLargestElement(arr);
  28. cout << "The largest element in the array is: " << maxElement << endl;
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter the number of elements in the array: Enter the elements of the array: The largest element in the array is: 0