Function Array Search in C Plus Plus

By | July 17, 2021

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. This 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 like 5 is shown on the screen. Otherwise it will show a message of “Item not fond in this array”.

Source Code of C++ Program using Function Array Search

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.

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 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";
      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.

--------------------------------

 249 total views,  1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *