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 manner until the desired element is found or the list is exhausted. It is widely used for small datasets or when other more efficient searching algorithms (like binary search) are not applicable. Understanding linear search is fundamental for learning about algorithmic efficiency and the importance of selecting the right searching technique for different types of problems.

Group

Search and Sort Algorithms in C++

Objective

Write a C++ program that performs a linear search to find an element in an array. The function should return the index of the element if found, or -1 if the element is not in the array. Implement this search algorithm and test it with a sample array.

Implement a linear search algorithm.

Example C++ Exercise

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

// Function to perform linear search on an array
int linearSearch(int arr[], int size, int target) {
    // Loop through each element in the array
    for (int i = 0; i < size; i++) {
        if (arr[i] == target) {  // Check if the current element matches the target
            return i;  // Return the index of the target element if found
        }
    }
    return -1;  // Return -1 if the target element is not found in the array
}

int main() {
    // Define an array of integers
    int numbers[] = {15, 27, 38, 49, 56, 72, 91};
    int size = sizeof(numbers) / sizeof(numbers[0]);  // Calculate the number of elements

    // Define the target element to search for
    int target = 49;

    // Call the linearSearch function
    int result = linearSearch(numbers, size, target);

    // Check if the target was found and display the result
    if (result != -1) {
        cout << "Element found at index: " << result << endl;  // Element found, display its index
    } else {
        cout << "Element not found in the array." << endl;  // Element not found, display message
    }

    return 0;  // End of the program
}

 Output

Element found at index: 3

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++.

  • 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...

  • Sorting an Array Using Insertion Sort Algorithm in C++

    This exercise focuses on implementing the insertion sort algorithm to sort an array of integers. Insertion sort works similarly to how we sort playing cards in our hands. The algor...