From 9cddb6e97748cac4e13692231dc8bb07101e92af Mon Sep 17 00:00:00 2001 From: Syed Ali Date: Tue, 16 Sep 2025 21:13:56 +0500 Subject: [PATCH 1/2] Create find_max_min.cpp This adds a C++ program to find the maximum and minimum element in an array. - File: arrays/find_max_min.cpp - Uses dynamic array input - Iterates through elements to compute max and min - Includes proper cleanup with delete[] --- arrays/find_max_min.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 arrays/find_max_min.cpp diff --git a/arrays/find_max_min.cpp b/arrays/find_max_min.cpp new file mode 100644 index 0000000000..8d247efb34 --- /dev/null +++ b/arrays/find_max_min.cpp @@ -0,0 +1,36 @@ +/** + * @file + * @brief Program to find maximum and minimum in an array + */ + +#include +using namespace std; + +int main() { + int n; + cout << "Enter size of array: "; + cin >> n; + + int* arr = new int[n]; + + cout << "Enter " << n << " elements: "; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + + int maxVal = arr[0]; + int minVal = arr[0]; + + for (int i = 1; i < n; i++) { + if (arr[i] > maxVal) maxVal = arr[i]; + if (arr[i] < minVal) minVal = arr[i]; + } + + cout << "Maximum: " << maxVal << endl; + cout << "Minimum: " << minVal << endl; + + delete[] arr; + arr = nullptr; + + return 0; +} From ff65e40235ca5322a91e0db7a2ccc2fbc7e729f5 Mon Sep 17 00:00:00 2001 From: Syed Ali Date: Tue, 16 Sep 2025 21:22:45 +0500 Subject: [PATCH 2/2] 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 --- arrays/find_max_min.cpp | 94 +++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 23 deletions(-) diff --git a/arrays/find_max_min.cpp b/arrays/find_max_min.cpp index 8d247efb34..d4bfa1f807 100644 --- a/arrays/find_max_min.cpp +++ b/arrays/find_max_min.cpp @@ -1,36 +1,84 @@ /** * @file - * @brief Program to find maximum and minimum in an array + * @brief Program to find maximum and minimum element in an array + * + * @details + * This program demonstrates how to find the maximum and minimum values + * in an array of elements. It is implemented as a template function so + * it works with any comparable data type. + * + * The algorithm simply iterates through the array once, updating the + * maximum and minimum values as needed. This results in \f$O(n)\f$ time + * complexity and \f$O(1)\f$ space complexity. */ -#include -using namespace std; +#include /// for assert +#include /// for std::cout, std::endl +#include /// for std::vector +#include /// for std::pair -int main() { - int n; - cout << "Enter size of array: "; - cin >> n; - - int* arr = new int[n]; - - cout << "Enter " << n << " elements: "; - for (int i = 0; i < n; i++) { - cin >> arr[i]; +/** + * @namespace arrays + * @brief Array-based algorithms + */ +namespace arrays { +/** + * @namespace find_max_min + * @brief Functions for finding maximum and minimum values + */ +namespace find_max_min { +/** + * @brief Find maximum and minimum in an array + * @tparam T data type of elements (must support comparisons) + * @param arr input array + * @return std::pair containing {max, min} + */ +template +std::pair findMaxMin(const std::vector& arr) { + assert(!arr.empty()); // array must not be empty + T maxVal = arr[0]; + T minVal = arr[0]; + for (size_t i = 1; i < arr.size(); i++) { + if (arr[i] > maxVal) { + maxVal = arr[i]; + } + if (arr[i] < minVal) { + minVal = arr[i]; + } } + return {maxVal, minVal}; +} +} // namespace find_max_min +} // namespace arrays - int maxVal = arr[0]; - int minVal = arr[0]; +/** + * @brief Self-test implementation + */ +static void test() { + using arrays::find_max_min::findMaxMin; - for (int i = 1; i < n; i++) { - if (arr[i] > maxVal) maxVal = arr[i]; - if (arr[i] < minVal) minVal = arr[i]; - } + std::vector vec1 = {3, 1, 7, -5, 9}; + auto result1 = findMaxMin(vec1); + assert(result1.first == 9 && result1.second == -5); - cout << "Maximum: " << maxVal << endl; - cout << "Minimum: " << minVal << endl; + std::vector vec2 = {1.5, 2.5, 0.5}; + auto result2 = findMaxMin(vec2); + assert(result2.first == 2.5f && result2.second == 0.5f); - delete[] arr; - arr = nullptr; + std::vector vec3 = {'a', 'z', 'm'}; + auto result3 = findMaxMin(vec3); + assert(result3.first == 'z' && result3.second == 'a'); + std::vector vec4 = {42}; + auto result4 = findMaxMin(vec4); + assert(result4.first == 42 && result4.second == 42); +} + +/** + * @brief Main function + */ +int main() { + test(); + std::cout << "All tests passed!\n"; return 0; }