Skip to content

Instantly share code, notes, and snippets.

@ronalddas
Last active November 17, 2015 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronalddas/58bb29bffad700d9d077 to your computer and use it in GitHub Desktop.
Save ronalddas/58bb29bffad700d9d077 to your computer and use it in GitHub Desktop.
A program to input, display, and search an element using linear search in an integer array using the concept of OOP
#include <iostream>
using namespace std;
class CLASSONE
{
public:
void LinearSearch(int a[],int key,int size)
{
int i,counter =0;
for(i =0 ; i < size ; i++ )
{
if(a[i]== key)
{
cout<<"Element found! at "<<i+1<<" position\n";
counter =1;
}
}
if(counter ==0)
cout<<"Element not found\n";
}
};
int main(void)
{
int b[50],n,i,j;
cout<<"Enter array size\n";
cin>>n;
cout<<"Enter elements \n";
for(i = 0; i < n; i++)
cin>>b[i];
cout<<"The array is \n";
for( i = 0 ; i < n; i++)
cout<<b[i]<<endl;
cout<<"Enter element to be searched\n";
cin>> j;
CLASSONE objectOne;
objectOne.LinearSearch(b,j,n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment