Find the Minimum Value in a List of Numbers in C++

This exercise involves implementing a C++ function that identifies the smallest number in a list. Finding the minimum value in a dataset is a basic yet essential operation in many algorithms and applications. Whether you're working on data analysis, statistical computations, or algorithm development, being able to extract the lowest value is a core skill.

You'll write a function that takes an array (or vector) of numbers as input and returns the smallest number from that list. This task will reinforce your understanding of arrays, loops, conditional statements, and function design in C++.

By solving this challenge, you'll practice how to scan through a sequence of values, perform comparisons, and efficiently manage data stored in arrays.

Group

Functions in C++

Objective

1. Create a function that receives an array and its size as parameters.
2. Initialize a variable to hold the minimum value (start with the first element).
3. Iterate through the array and compare each element with the current minimum.
4. Update the minimum if a smaller value is found.
5. Return the final minimum value.

Implement a function that returns the smallest value from a list of numbers.

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 minimum value in an array
int findMinimum(int arr[], int size) {
    int min = arr[0]; // Assume the first element is the minimum

    // Loop through the array starting from the second element
    for (int i = 1; i < size; i++) {
        // If a smaller element is found, update the minimum
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    return min; // Return the smallest value
}

int main() {
    // Define an array of numbers
    int numbers[] = {42, 17, 23, 8, 15, 34};
    int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the array size

    // Call the function and store the result
    int minimum = findMinimum(numbers, size);

    // Display the smallest number in the array
    cout << "The smallest number in the list is: " << minimum << endl;

    return 0; // End of program
}

 Output

The smallest number in the list is: 8

Share this C++ Exercise


More C++ Programming Exercises of Functions 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++.