Find Maximum Value in Array in C++

  1. Use The iterative Method to Find Maximum Value in a C++ Array
  2. Use the std::max_element Algorithm to Find Maximum Value in a C++ Array
  3. Use the std::minmax_element Algorithm to Find Maximum Value in a C++ Array

This article will introduce how to find maximum value in an array in C++.

Use The iterative Method to Find Maximum Value in a C++ Array

The straightforward way to implement a custom function for maximum value searching is using the iterative method. The following example code has the for loop structure that cycles through each element of the array and checks if the current value is greater than the current maximum value. Note that the current maximum value is initialized with the value of the first element in the array and modified when the if condition evaluates true.

#include <iostream>
#include <ctime>
#include <sys/time.h>

using std::cout;
using std::endl;

void generateNumbers(int arr[], size_t &width){

    std::srand(std::time(nullptr));
    for (size_t i = 0; i < width; i++) {
        arr[i] = std::rand();
    }
}

template<typename T>
T FindMax(T *arr, size_t n)
{
    int max = arr[0];

    for (size_t j = 0; j < n; ++j) {
        if (arr[j] > max) {
            max = arr[j];
        }
    }
    return max;
}

float time_diff(struct timeval *start, struct timeval *end){
    return (end->tv_sec - start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
}

int main() {
    struct timeval start{};
    struct timeval end{};

    size_t width = 100000;
    int *arr = new int[width];

    generateNumbers(arr, width);

    gettimeofday(&start, nullptr);
    cout << "Maximum element is: " << FindMax(arr, width) << endl;
    gettimeofday(&end, nullptr);

    printf ("FindMax: %0.8f sec\n",
            time_diff(&start, &end));

    delete [] arr;
    return EXIT_SUCCESS;
}

Output:

Maximum element is: 2147460568
FindMax: 0.00017500 sec

Use the std::max_element Algorithm to Find Maximum Value in a C++ Array

std::max_element is another method to find the maximum value in the given range. It’s part of the STL algorithms, and the simplest overload takes only two iterators to denote range borders to search for. std::max_element returns an iterator to the maximum value element. If multiple elements have the same value and simultaneously happen to be maximums, the function returns the iterator that points to the first one.

#include <iostream>
#include <ctime>
#include <sys/time.h>

using std::cout;
using std::endl;

void generateNumbers(int arr[], size_t &width){

    std::srand(std::time(nullptr));
    for (size_t i = 0; i < width; i++) {
        arr[i] = std::rand();
    }
}

template<typename T>
T FindMax2(T *arr, size_t n)
{
    return *std::max_element(arr, arr+n);
}

float time_diff(struct timeval *start, struct timeval *end){
    return (end->tv_sec - start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
}

int main() {
    struct timeval start{};
    struct timeval end{};

    size_t width = 100000;
    int *arr = new int[width];

    generateNumbers(arr, width);

    gettimeofday(&start, nullptr);
    cout << "Maximum element is: " << FindMax2(arr, width) << endl;
    gettimeofday(&end, nullptr);

    printf ("FindMax2: %0.8f sec\n",
            time_diff(&start, &end));

    delete [] arr;
    return EXIT_SUCCESS;
}

Output:

Maximum element is: 2147413532
FindMax2: 0.00023700 sec

Use the std::minmax_element Algorithm to Find Maximum Value in a C++ Array

Alternatively, we can use the std:minmax_element algorithm from STL to find both minimum and maximum elements in the given range and return them as std::pair. minmax_element function can optionally take custom binary comparison function as the third argument. Otherwise, it has the same parameters as the max_element and it behaves similarly when several min/max elements are found in the range.

#include <iostream>
#include <ctime>
#include <sys/time.h>

using std::cout;
using std::endl;

void generateNumbers(int arr[], size_t &width){

    std::srand(std::time(nullptr));
    for (size_t i = 0; i < width; i++) {
        arr[i] = std::rand();
    }
}

template<typename T>
auto FindMinMax(T *arr, size_t n)
{
    return std::minmax_element(arr, arr+n);
}

float time_diff(struct timeval *start, struct timeval *end){
    return (end->tv_sec - start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
}

int main() {
    struct timeval start{};
    struct timeval end{};

    size_t width = 100000;
    int *arr = new int[width];

    generateNumbers(arr, width);

    gettimeofday(&start, nullptr);
    auto ret = FindMinMax(arr, width);
    gettimeofday(&end, nullptr);
    cout << "MIN element is: " << *ret.first
         << " MAX element is: " << *ret.second << endl;

    printf ("FindMinMax: %0.8f sec\n",
            time_diff(&start, &end));

    delete [] arr;
    return EXIT_SUCCESS;
}

Output:

MIN element is: 3843393 MAX element is: 2147251693
FindMinMax: 0.00000400 sec
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Array

  • Create Array of Structs in C++
  • Compare Arrays in C++