Search an array using linear search

Linear search

Prerequisite: Basics of C++ and little knowledge about arrays.

Linear search is a part of integer arrays which helps user to find the number entered by him and tells him the exact location of the number.

Let’s start to prepare our very own linear search program.

Firstly, we will include the header file and declare a few variables which will be explained in a while

[sourcecode language=cpp]#include

void main()

{
int list[30],num,n; [/sourcecode]

As you can see above our array size is 30. You can set this to your convenience and according to the size you want. Since, this is our maximum array size which can be attained. We will make sure there isn’t any wastage of resources by asking the user to enter the size of the array (This should be less than 30 or the size you set).

‘list[30]’ is an integer variable and it’s functioning is already explained above. ‘num’ is the number which will be searched by the user. ‘n’ variable will store the integer value which user will enter as the size of the array they want.

Now let’s ask the users to enter the size of the array and allow them to enter the values for the array.

[sourcecode language=cpp]cin>>n;
for(int i=0;i>list[i];

[/sourcecode]

As you can see the we have asked the user to initialize the entire array. Now we will ask the user to enter the number to be searched.

[sourcecode language=cpp]cout<<"Enter the number to be searched"; cin>>num;[/sourcecode]

Now lets run another loop similar to that of the loop above. Here we will check if the value of the ‘num’ matche with the any of the values in the array. If it finds one, it the loop will break.

[sourcecode language=cpp] for(i=0;i

Now we will display the position to the user.

[sourcecode language=cpp]cout<<"the number was found at"<<++i;[/sourcecode]

Note above that we pre-incremented the position. This is to tell the user according to his perception (i.e., array starting with 1, 2…) and assuming user doesn’t know the convention that the array size starts with 0, 1, 2…

End the program with

[sourcecode language=cpp]} //this is to end the void main part[/sourcecode]

Now let’s compile everything and check the entire program.

The final program will look something like this. [sourcecode language=cpp]#include

void main()

{
int list[30],num,n;
cin>>n;

for(int i=0;i>list[i];

cout<<"Enter the number to be searched"; cin>>num;

for(i=0;i

  • its very difficult to understand…………..

    • gyanguru

      It isn't really complicated as I've made it :lol:

      All you need to do is to get the values stored in the array from user and then run a loop to check if the value to be searched matches any of the value in the array. If it finds it, display its location.

  • It isn't really complicated as I've made it :lol:

    All you need to do is to get the values stored in the array from user and then run a loop to check if the value to be searched matches any of the value in the array. If it finds it, display its location.