forked from Nikhil-2002/Programming_Hactoberfest25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGr8stIntInArr.cpp
More file actions
48 lines (36 loc) · 1.03 KB
/
Gr8stIntInArr.cpp
File metadata and controls
48 lines (36 loc) · 1.03 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
int findLargestElement(int arr[], int size) {
if (size <= 0) {
// Ha the case of an empty array or invalid size
return -1;
}
int largest = arr[0]; // Assuming the first element is the largest
for (int i = 1; i < size; i++) {
if (arr[i] > largest) {
largest = arr[i]; // Updating largest if current element is greater
}
}
return largest;
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
if (size <= 0) {
cout << "Invalid array size." <<endl;
return 1;
}
int arr[size];
cout << "Enter the elements of the array, separated by spaces:" <<endl;
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
int largest = findLargestElement(arr, size);
if (largest != -1) {
cout << "The largest element in the array is: " << largest <<endl;
} else {
cout << "The array is empty or has an invalid size." <<endl;
}
return 0;
}