Passing integer array to a function for linear search – Computer Sir Ki Class

Code Learning #CPP#2733

## Passing integer array to a function for linear search

Program to pass an integer array, length of array and the member to be found to a function and return the position of the member found.

## Learning Objectives

• Passing an integer array to a function.
• Learning how length(count of members) of integer array members is needed to be passed.

## Source Code

 #include using namespace std; int pos_in_arr(int [],int,int); int main() { int arr[]={19,20,34,55,99,102,12}; int len=sizeof(arr)/sizeof(arr[0]); int n; cout<<"Find position of which number? " ; cin>>n; int pos=pos_in_arr(arr,len,n); if(pos>0) cout<<"Position of given input = "<

## Run Output

``````Find position of which number? 55
Position of given input = 4

-OR-
Find position of which number? 17
``````

## Code Understanding

int pos_in_arr(int [],int,int);
Function prototype of a function with an integer array passed as reference using [] notation and two other variables, one for length of array and other for member to be searched.

int arr[]={19,20,34,55,99,102,12};
An array has been initialised with given integer values.

int len=sizeof(arr)/sizeof(arr[0]);
The length or count of array members is found by calculating size of entire array divided by size of first member of array. This is required as each member takes up 2 or 4 bytes for an integer as per the operating system and compiler specifications.

int n; cout<<“Find position of which number? ” ; cin>>n;
The number to be searched is collected from the user.

int pos=pos_in_arr(arr,len,n);
Here the function is called with the actual parameters. The name of array arr is actually an address of first member of array in the memory, so it is passed by reference. The variable len and n are passed by value. The return of function as specified would be either the actual position of searched member or 0 as the case may be.

if(pos>0) cout<<“Position of given input = “<<pos<<endl;
Prints position if above function return more than 0 value. The position in array begins from 1 although the index begins from 0.

If the input does not match 0 is returned, thus not found is printed.

int pos_in_arr(int array[],int len,int num)
Function header with first argument as array which is always passed as reference and second argument as length of array and third argument as member value to be searched.
{
for(int i=0;i<len;i++) //Loop runs till the length len passed as argument.
{
if(array[i]==num) return i+1;
Position of member is 1 more than the index, so as soon match is found the position is returned.
}
return 0;
This return works when the search fails.
}

## Notes

• Some programmers prefer to return -1 when the search fails. This is because if someone wants to return index instead of position, it can be returned as 0.

## Common Errors

• Some student make function call of array as pos_in_arr(arr[ ],len,n) by giving [ ] notation at the time of calling. This is incorrect. The name itself is a reference so [ ] sign is not required at the time of passing.
• This method of linear search can be used for any data type long, float, double, char etc.. If it is a null terminated string in form of a char array, then passing the length of array is not required as the target function can also use the strlen() function as shown in the example #2701.

Suggested Filename(s): cbr-integer-array.cpp, cbr-iarr.cpp

sunmitra| Created: 17-Jan-2018 | Updated: 17-Jan-2018|

×