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


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2733 siteicon   siteicon   siteicon  

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

TC++ #2733


Source Code

  Related (?) :

Run Output

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

Find position of which number? 17
not found

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.

else cout<<“not found”<<endl;
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.


  • 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|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues