forked from mrigaankzoro/Hacktoberfest24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_find_largest_num.cpp
57 lines (45 loc) · 1.38 KB
/
algo_find_largest_num.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
*****************************************************************
Algorithm
1. Initialize a variable max with the first element of the array.
2. Loop through the entire array.
3. In each iteration, compare the current element with max.
4. If the current element is greater than max, update max.
5. After the loop, max will hold the largest number in the array.
*****************************************************************
*/
#include <iostream>
#include <vector>
using namespace std;
// Function to find the largest element in the array
int findLargest(const vector<int>& arr) {
int max = arr[0];
for (int i = 1; i < arr.size(); i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
int main() {
int n;
// Input number of elements
cout << "Enter the number of elements: ";
cin >> n;
// Check for valid input
if (n <= 0) {
cout << "Number of elements must be positive." << endl;
return 1;
}
vector<int> arr(n);
// Input array elements
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Find the largest element
int max = findLargest(arr);
// Output the largest element
cout << "The largest number is: " << max << endl;
return 0;
}