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


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 <iostream>
using namespace std;
bool search(int [],int, int);
int main()
int st;
int na[]={25,10,32,44,99};
cout<<"Enter search term ";
cout<<"Element found"<<endl;
cout<<"Element not found"<<endl;

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

Test it !

Run Output

Enter search term 32
Element found


Enter search term 12
Element not found

Code 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


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