Linear search using array reference passing to function – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #CPP#1628 siteicon   siteicon   siteicon  

Linear search using array reference passing to function

Search for presence of element in an integer array by using a function with a call by reference of array, size and search term.

Source Code

TC++ #1628

Source Code

#include 
using namespace std;
bool search(int [],int, int);
int main()
{
int st;
int na[]={25,10,32,44,99};
cout<<"Enter search term ";
cin>>st;
if(search(na,5,st))
cout<<"Element found"< else
cout<<"Element not found"< }

bool search(int AR[],int size,int item)
{
for(int i=0;i {
if(AR[i]==item) return true;
}
return false;
}

Test it !

Run Output

Enter search term 32
Element found

or

Enter search term 12
Element not found

Notes

When array is passed as a reference in a target function, its size can not be automatically determined using sizeof operator as only the location of first member of array is passed. Therefore the size of array has to be essentially passed to the target function.


Suggested Filename(s): linearsearchfunc.cpp, searchfunc.cpp, arrrefpassing.cpp



Share

sunmitra| Created: 4-Dec-2017 | Updated: 8-Dec-2017|






Back