codescracker
c++

C++ Program for Linear Search



« Previous Program Next Program »

In this article, you will learn and get code to implement linear search in C++. Here are the list of programs on linear search available in this article:

Before going through these programs, if you're not aware about the logic used behind linear search, then you can refer to linear search algorithm and examples to get every required things.

Linear Search in C++

To search any element present inside the array in C++ programming using linear search technique, you have to ask from user to enter any 10 numbers as 10 array elements and then ask to enter a number to search as shown in the program given below.

This program doesn't allows user to define the size of an array. Later on, you will go through the program that allows user to define the size and also prints all the indexes of an element, if found multiple times.

This is the simplest program to implement linear search in C++.

// C++ Program to Implement Linear Search
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int arr[10], i, num, index;
    cout<<"Enter 10 Numbers: ";
    for(i=0; i<10; i++)
        cin>>arr[i];
    cout<<"\nEnter a Number to Search: ";
    cin>>num;
    for(i=0; i<10; i++)
    {
        if(arr[i]==num)
        {
            index = i;
            break;
        }
    }
    cout<<"\nFound at Index No."<<index;
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program linear search

Now supply any 10 numbers say 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and a number say 5 to search it from the given array. Here is the sample output produced after providing these inputs:

linear search c++

Note - If user enters a number that doesn't exist in the array, then the program given above is not a correct one with this type of inputs. Therefore we've another program for you, as given below.

Note - If user enters a number that presents more than one time in the array, then in this case also, above program is not a correct program for you. Because that program will only print its index of first occurrence only. Rest of its index gets skipped. Therefore go with the second program, as given below.

Linear Search with Duplicate Element

This program has many extra features than previous program:

Let's have a look at the program and its sample run:

// C++ Modified Version of Linear Search Program
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, num, arrTemp[50], j=0, chk=0;
    cout<<"Enter the Size for Array Size: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nEnter the Number to Search: ";
    cin>>num;
    for(i=0; i<tot; i++)
    {
        if(arr[i]==num)
        {
            arrTemp[j] = i;
            j++;
            chk++;
        }
    }
    if(chk>0)
    {
        cout<<"\nNumber Found at Index No. ";
        tot = chk;
        for(i=0; i<tot; i++)
            cout<<arrTemp[i]<<" ";
    }
    else
        cout<<"\nNumber doesn't Found!";
    cout<<endl;
    return 0;
}

Here is its sample run with following user inputs:

After providing these inputs, here is the sample output you will see:

c++ linear search with duplicate elements

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker