-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays_functions.cpp
More file actions
54 lines (45 loc) · 1.66 KB
/
arrays_functions.cpp
File metadata and controls
54 lines (45 loc) · 1.66 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
49
50
51
52
53
54
#include <iostream>
#include <algorithm> // for sort(), reverse(), max_element(), min_element()
using namespace std;
int main() {
int arr[] = {5, 8, 2, 9, 1, 7};
// -------- SIZE OF ARRAY --------
int size = sizeof(arr) / sizeof(arr[0]); // total elements
cout << "Array size: " << size << endl;
// -------- ORIGINAL ARRAY --------
cout << "Original array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// -------- SORTING ARRAY --------
sort(arr, arr + size); // sorts array in ascending order
cout << "Sorted array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// -------- REVERSING ARRAY --------
reverse(arr, arr + size); // reverses the entire array
cout << "Reversed array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// -------- FIND MAX AND MIN --------
int maxVal = *max_element(arr, arr + size); // returns max value
int minVal = *min_element(arr, arr + size); // returns min value
cout << "Max value: " << maxVal << endl;
cout << "Min value: " << minVal << endl;
// -------- SUM OF ARRAY ELEMENTS --------
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i]; // adding elements
}
cout << "Sum of array: " << sum << endl;
// -------- SEARCHING ELEMENT --------
int key = 9;
bool found = binary_search(arr, arr + size, key); // works only on sorted array
cout << "Is " << key << " present? " << (found ? "Yes" : "No") << endl;
return 0;
}