Function Array Search in C Plus Plus. This is a C Plus Plus programming language program to input size of an array of integer numbers. The program will take item from the user to be searched in this array. If it finds the specified element in array, a suitable message of Item found on location is shown on the screen. Otherwise it will show a message of “Item not fond in this array”.
How this program works?
- First of all the user will enter size n of the array.
- Enter n items in array
- Input the item to be searched.
- Call the function search() with three arguments, array name, size n and item to be searched in array.
- The function search() will display location if item found or a ‘not found’ message.
- This function uses a for loop from 0 to n-1 for traversing he whole array.
- It compares each element of the array with given item to be searched.
- The sample run output of this program is at end of this tutorial.
Explanation of the User defined C Function
void search(int a[], int n, int item) { int i; for (i = 0; i < n; i++) { if (a[i] == item) /* If required element is found */ { cout<<item<<" is present at location "<<i+1<<" in the given array"; break; } } if (i == n) cout<<item<<" isn't present in the array.\n"; }
Accept the parameters array a, size n and the item to search.
Write down a for loop from 0 to last element of the array.
Check if the current array item is equal to the item.
If item is equal to the array element, display item found on the location.
Use break statement to terminate the loop.
After loop check if i is equal to n then show the message like “Item not found in array”.
The Source Code of C++ Program using Function Array Search
#include<iostream> using namespace std; void search(int a[], int n , int item); int main() { int a[100], item, i, n; cout<<"Enter number of elements in array[Maximum 100]="; cin>>n; for (i = 0; i < n; i++) { cout<<"Enter the Element Number "<<i+1<<" in Array="; cin>>a[i]; } cout<<"Enter a number to search in the array="; cin>>item; // call to function search(a,n,item); return 0; } void search(int a[], int n, int item) { int i; for (i = 0; i < n; i++) { if (a[i] == item) /* If required element is found */ { cout<<item<<" is present at location "<<i+1<<" in the given array"; break; } } if (i == n) cout<<item<<" isn't present in the given array.\n"; }
Second Version of Program Search Array
//Program to search an element from an array of integer numbers // www.EasyCodeBook.com #include<iostream> using namespace std; void search(int a[], int n , int item); int main() { int a[100], item, i, n; cout<<"Enter number of elements in array[Maximum 100]="; cin>>n; for (i = 0; i < n; i++) { cout<<"Enter Element Number "<<i+1<<" in Array="; cin>>a[i]; } cout<<"Enter a number to search in array="; cin>>item; // call to function search(a,n,item); return 0; } void search(int a[], int n, int item) { int i; for (i = 0; i < n; i++) { if (a[i] == item) /* If required element is found */ { cout<<item<<" is present at location "<<i+1<<" in the given array"; return; } } cout<<item<<" isn't present in the given array.\n"; }
Output of Search an Item in Array Program in C++
Enter number of elements in array[Maximum 100]=5 Enter Element Number 1 in Array=4 Enter Element Number 2 in Array=7 Enter Element Number 3 in Array=89 Enter Element Number 4 in Array=34 Enter Element Number 5 in Array=23 Enter a number to search in array=34 34 is present at location 4 in the given array
Enter number of elements in array[Maximum 100]=10 Enter Element Number 1 in Array=1 Enter Element Number 2 in Array=2 Enter Element Number 3 in Array=3 Enter Element Number 4 in Array=4 Enter Element Number 5 in Array=5 Enter Element Number 6 in Array=78 Enter Element Number 7 in Array=90 Enter Element Number 8 in Array=32 Enter Element Number 9 in Array=12 Enter Element Number 10 in Array=45 Enter a number to search in array=100 100 isn't present in the given array. --------------------------------
- C Program Binary Search in Array
- C Program Linear Search in Array
-
Bubble Sort Algorithm Trace Steps on Sample Data and C++ Program