linear search code in c++ language

perform linear search in c++.c++ is the best object oriented programming and you can use linear search in c++ to search any number in the given array.if you are given an array or collection of data and are asked to find a number in that data,then you can perform linear search method to determine that number in c++ language.there are many searches in c++ among them linear search and binary search are the most used searches that are compromised of code that is used to find any number in array.so following is the code to perform linear search in c++ language.

for dev c++ compiler version 5.92

linear search code in dev c++ 5.92
#include<iostream>
using namespace std;
{
 int array[20],num,d,i,flag=0;

 cout<<"enter the total number of array element";
 cin>>n;
 cout<<"Enter Elements of the Array";

 for(i=0;i<num;i++)
  { cin>>array[i];}

 cout<<"Enter Element to search in the given array by performing linear search";
 cin>>d;


 for(i=0;i<n;++i)
 {
  if(a[i]==d)
  {
   flag=1;
   break;
  }
 }

 if(flag)                   //for flag=1
   cout<<"\nElement is Found at position "<<i+1;
 else
  cout<<"\nElement not found";
}

for turbo c++ compiler,linear search code in c++


#include<iostream.h>
#include<conio.h>

{
 int array[20],num,d,i,flag=0;

 cout<<"enter the total number of array element";
 cin>>n;
 cout<<"Enter Elements of the Array";

 for(i=0;i<num;i++)
  { cin>>array[i];}

 cout<<"Enter Element to search in the given array by performing linear search";
 cin>>d;


 for(i=0;i<n;++i)
 {
  if(a[i]==d)
  {
   flag=1;
   break;
  }
 }

 if(flag)                   //for flag=1
   cout<<"\nElement is Found at position "<<i+1;
 else
  cout<<"\nElement not found";
getch();

}

screen shot of linear search code when compiled in dev 5.92



Post a Comment

 
Top