Skip to content

Commit ff65e40

Browse files
authored
feat: add generic find_max_min implementation with tests
- Added a template-based implementation to find maximum and minimum in an array - Placed inside arrays/find_max_min.cpp with proper namespaces - Includes detailed Doxygen documentation - Added multiple test cases with assert() for ints, floats, chars, and single-element arrays - Matches project contribution and coding style
1 parent 9cddb6e commit ff65e40

File tree

1 file changed

+71
-23
lines changed

1 file changed

+71
-23
lines changed

arrays/find_max_min.cpp

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,84 @@
11
/**
22
* @file
3-
* @brief Program to find maximum and minimum in an array
3+
* @brief Program to find maximum and minimum element in an array
4+
*
5+
* @details
6+
* This program demonstrates how to find the maximum and minimum values
7+
* in an array of elements. It is implemented as a template function so
8+
* it works with any comparable data type.
9+
*
10+
* The algorithm simply iterates through the array once, updating the
11+
* maximum and minimum values as needed. This results in \f$O(n)\f$ time
12+
* complexity and \f$O(1)\f$ space complexity.
413
*/
514

6-
#include <iostream>
7-
using namespace std;
15+
#include <cassert> /// for assert
16+
#include <iostream> /// for std::cout, std::endl
17+
#include <vector> /// for std::vector
18+
#include <utility> /// for std::pair
819

9-
int main() {
10-
int n;
11-
cout << "Enter size of array: ";
12-
cin >> n;
13-
14-
int* arr = new int[n];
15-
16-
cout << "Enter " << n << " elements: ";
17-
for (int i = 0; i < n; i++) {
18-
cin >> arr[i];
20+
/**
21+
* @namespace arrays
22+
* @brief Array-based algorithms
23+
*/
24+
namespace arrays {
25+
/**
26+
* @namespace find_max_min
27+
* @brief Functions for finding maximum and minimum values
28+
*/
29+
namespace find_max_min {
30+
/**
31+
* @brief Find maximum and minimum in an array
32+
* @tparam T data type of elements (must support comparisons)
33+
* @param arr input array
34+
* @return std::pair<T, T> containing {max, min}
35+
*/
36+
template <typename T>
37+
std::pair<T, T> findMaxMin(const std::vector<T>& arr) {
38+
assert(!arr.empty()); // array must not be empty
39+
T maxVal = arr[0];
40+
T minVal = arr[0];
41+
for (size_t i = 1; i < arr.size(); i++) {
42+
if (arr[i] > maxVal) {
43+
maxVal = arr[i];
44+
}
45+
if (arr[i] < minVal) {
46+
minVal = arr[i];
47+
}
1948
}
49+
return {maxVal, minVal};
50+
}
51+
} // namespace find_max_min
52+
} // namespace arrays
2053

21-
int maxVal = arr[0];
22-
int minVal = arr[0];
54+
/**
55+
* @brief Self-test implementation
56+
*/
57+
static void test() {
58+
using arrays::find_max_min::findMaxMin;
2359

24-
for (int i = 1; i < n; i++) {
25-
if (arr[i] > maxVal) maxVal = arr[i];
26-
if (arr[i] < minVal) minVal = arr[i];
27-
}
60+
std::vector<int> vec1 = {3, 1, 7, -5, 9};
61+
auto result1 = findMaxMin(vec1);
62+
assert(result1.first == 9 && result1.second == -5);
2863

29-
cout << "Maximum: " << maxVal << endl;
30-
cout << "Minimum: " << minVal << endl;
64+
std::vector<float> vec2 = {1.5, 2.5, 0.5};
65+
auto result2 = findMaxMin(vec2);
66+
assert(result2.first == 2.5f && result2.second == 0.5f);
3167

32-
delete[] arr;
33-
arr = nullptr;
68+
std::vector<char> vec3 = {'a', 'z', 'm'};
69+
auto result3 = findMaxMin(vec3);
70+
assert(result3.first == 'z' && result3.second == 'a');
3471

72+
std::vector<int> vec4 = {42};
73+
auto result4 = findMaxMin(vec4);
74+
assert(result4.first == 42 && result4.second == 42);
75+
}
76+
77+
/**
78+
* @brief Main function
79+
*/
80+
int main() {
81+
test();
82+
std::cout << "All tests passed!\n";
3583
return 0;
3684
}

0 commit comments

Comments
 (0)