Development

How to Find the Maximum Value in Array in C++

In this tutorial, we will explore all the methods you can use to determine the max value in C++ and provide examples to help us better understand each method's workings.
Captain Salem 3 min read
How to Find the Maximum Value in Array in C++

Whether you are new to programming or have years of experience, you have or are bound to work with arrays in C++. Arrays are some of the most fundamental and shared data structures in many programming languages.

Arrays allow us to store elements of similar type in contiguous memory locations, providing a fast and efficient method of accessing individual elements using indexes.

One typical operation when working with arrays is determining the maximum value.

Arrays in C++

In C++, an array is a collection of elements of the same type stored in contiguous memory locations.

We can access each element of an array using its index, as demonstrated in the example below:

int arr[5] = {1, 2, 3, 4, 5};

In this case, we define an array of five integers.

With that out of the way, let us proceed and discuss how to determine the maximum value in a C++ array.

Method 1 - Using a For Loop

As you can guess, the most basic method of finding the max value in an array is by iterating over each element and comparing it with a temporary maximum value.

Consider the example code shown below:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
#include <iostream>
using namespace std;

int main() {
    int arr[] = {10, 324, 45, 90, 988};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    int max_val = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max_val) {
            max_val = arr[i];
        }
    }
    
    cout << "max value: " << max_val << endl;
    return 0;
}

As demonstrated above, we use a for loop to iterate over each element in the array. The resulting output is as shown:

max value: 988

Method 2 - Using the STL max_element

In the C++ Standard Template Library (STL), we have access to a function called max_element which allows us to find the maximum value in an array or any other container.

An example is as shown:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int arr[] = {10, 324, 45, 90, 988};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    int max_val = *max_element(arr, arr + n);
    
    cout << "max value: " << max_val << endl;
    return 0;
}

This is a more efficient and easy method of finding the max value in an array without manual iteration.

Method 3 - Using Recursion.

Recursion is another method we can use to find the max value in an array. If you are not familiar, recursion is a technique where a function calls itself to break down a problem into smaller sub-problems that are easier to manage.

NOTE: Recursion can cause issues such as infinite loops and performance degradation if not appropriately implemented.

An example of how to use recursion to find the max value in an array is shown below:

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
#include <iostream>
using namespace std;

int findMax(int arr[], int n) {
    // If the array has one element, return it
    if (n == 1) {
        return arr[0];
    }

    // Recursively find the maximum of the remaining elements
    return max(arr[n-1], findMax(arr, n-1));
}

int main() {
    int arr[] = {10, 324, 45, 90, 988};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    int max_val = findMax(arr, n);
    
    cout << "max value: " << max_val << endl;
    return 0;
}

In this case, we define a findMax() function that recursively finds the max value of the elements in an array.

Method 4 - Using Divide and Conquer

The divide and conquer method is the most efficient method of finding the max value in an array, regardless of the array size. This method splits the array into two halves and finds the max value in each half. It then returns the max value of each half, which we can sort to get the max of the entire array.

//
//  main.cpp
//  cpp
//
//  Created by captain salem on 2023-08-10.
#include <iostream>
using namespace std;

int findMax(int arr[], int low, int high) {
    // If there's only one element, return it
    if (low == high) {
        return arr[low];
    }

    int mid = (low + high) / 2;

    // Find maximum in the first half
    int max1 = findMax(arr, low, mid);

    // Find maximum in the second half
    int max2 = findMax(arr, mid + 1, high);

    // Return the maximum of the two
    return max(max1, max2);
}

int main() {
    int arr[] = {10, 324, 45, 90, 988};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    int max_val = findMax(arr, 0, n-1);
    
    cout << "max value: " << max_val << endl;
    return 0;
}

Conclusion

In this post, we explored the methods that we can use to find the max value in a given array. We hope you enjoyed this tutorial. To get more tutorials like this, join us in the newsletter where we will be sending you a list of the articles published each month. You will love it!

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.