Find the Largest Number in an Array in C++

In this exercise, you will implement a function that finds the largest number in an array. This task is useful when working with datasets where you need to extract the maximum value.

The function should loop through the array, compare each element, and track the largest number encountered. This will help you practice array manipulation and conditional statements in C++.

Group

Arrays and Vectors in C++

Objective

1. Define an array with several integer values.
2. Implement a function that iterates through the array.
3. Compare each element of the array with the current largest number.
4. Update the largest number whenever a larger value is found.
5. After the loop, return or print the largest number.
6. Test the program with different arrays to ensure its accuracy.

Implement a function that finds the largest number in an array.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace

// Function to find the largest number in the array
int findLargestNumber(int arr[], int size) {
    int largest = arr[0]; // Assume the first element is the largest

    // Loop through the array starting from the second element
    for (int i = 1; i < size; i++) {
        if (arr[i] > largest) { // If the current element is larger than the current largest
            largest = arr[i]; // Update the largest number
        }
    }

    return largest; // Return the largest number found
}

int main() {
    // Define an array of numbers
    int numbers[] = {15, 22, 4, 67, 30, 90, 12};
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array

    // Call the function to find the largest number
    int largest = findLargestNumber(numbers, size);

    // Output the largest number
    cout << "The largest number in the array is: " << largest << endl;

    return 0; // End of program
}

 Output

The largest number in the array is: 90

Share this C++ Exercise


More C++ Programming Exercises of Arrays and Vectors in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Reverse an Array in C++

    In this exercise, you will implement a program that reverses an array. This task is useful in many scenarios such as reversing a list of elements, manipulating data structures, and...

  • Sum Two Arrays Element by Element in C++

    In this exercise, you will implement a function that sums two arrays element by element. This means that for two given arrays of the same size, the function will return a new array...

  • Remove Duplicates from an Array in C++

    In this exercise, you will create a program that removes duplicates from an array of numbers. The function will iterate over the array and remove any duplicate values, leaving only...

  • Check if an Array is Symmetric in C++

    In this exercise, you will create a function to determine if an array is symmetric. An array is considered symmetric if it reads the same forward and backward, meaning the first el...

  • Sort an Array of Numbers in Ascending Order in C++

    In this exercise, you will implement a program that sorts an array of numbers in ascending order. Sorting is one of the most fundamental operations in programming, and there are se...

  • Sum of Array Elements in C++

    In this exercise, you will create a program that calculates the sum of all the elements in an array. Summing the elements of an array is a common task in programming, which is ofte...