Finding the Smallest Number in an Array Using Sequential Search in C++

This exercise focuses on implementing a sequential search algorithm in C++ to identify the smallest number in a given array. Sequential search, also known as linear search, is one of the most basic algorithms used to search through data. It involves iterating through each element in the array and comparing it to find a particular value or condition—in this case, the minimum value. This task is essential for understanding how to traverse arrays, apply comparisons, and store intermediate results. It serves as a foundational concept for more advanced search and optimization techniques in programming.

Group

Search and Sort Algorithms in C++

Objective

Write a C++ program that uses a function to perform a sequential search for the smallest number in an array. The function should loop through all elements, compare them, and return the smallest value found. In the main function, define a sample array, call the function, and display the result.

Create a program that searches for the smallest number in an array using sequential search.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include the input-output stream library
using namespace std;

// Function to find the smallest number using sequential search
int findSmallest(int arr[], int size) {
    int smallest = arr[0];  // Assume the first element is the smallest

    // Loop through the array to find the smallest element
    for (int i = 1; i < size; i++) {
        if (arr[i] < smallest) {
            smallest = arr[i];  // Update smallest if a smaller value is found
        }
    }

    return smallest;  // Return the smallest value found
}

int main() {
    // Define an array of integers
    int numbers[] = {42, 17, 23, 7, 89, 5, 31};
    int size = sizeof(numbers) / sizeof(numbers[0]);  // Calculate the number of elements

    // Call the function to find the smallest number
    int minNumber = findSmallest(numbers, size);

    // Display the result
    cout << "The smallest number in the array is: " << minNumber << endl;

    return 0;  // End of the program
}

 Output

The smallest number in the array is: 5

Share this C++ Exercise


More C++ Programming Exercises of Search and Sort Algorithms 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++.

  • Linear Search Algorithm in C++

    This exercise focuses on implementing the linear search algorithm in C++. Linear search is a simple searching algorithm that checks each element in a list or array in a sequential ...

  • MergeSort Algorithm in C++

    In this exercise, you will implement the MergeSort algorithm in C++. MergeSort is a highly efficient, divide-and-conquer sorting algorithm that works by dividing the input array in...

  • Bubble Sort Algorithm for Sorting Strings in C++

    In this exercise, you will implement the Bubble Sort algorithm to sort an array of strings alphabetically. Bubble Sort is a simple comparison-based sorting algorithm that repeatedl...

  • Count the Occurrences of a Number in an Array in C++

    In this exercise, you will develop a C++ function that counts how many times a specific number appears in an array. This task involves iterating through the array, comparing each e...

  • Bubble Sort Algorithm Implementation in C++

    This C++ exercise demonstrates the implementation of the Bubble Sort algorithm, a fundamental sorting technique used to order elements in a list. Bubble Sort works by repeatedly st...

  • Implementing Binary Search in a Sorted Array in C++

    This exercise focuses on the implementation of the binary search algorithm, which is used to efficiently find a target value within a sorted array. Binary search operates by repeat...