P is one-dimensional array of integers. Write a C++ function to efficiently search for a data VAL from P. If VAL is present in the array then the function should return value 1 and 0 otherwise.

Source Code

#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{
	int Arr[100],n,val;
	bool found;
	cout<<"Enter number of elements you want to insert ";
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cout<<"Enter element "<<i+1<<":";
		cin>>Arr[i];
	}
	cout<<"Enter the number you want to search ";
	cin>>val;
	found=lsearch(Arr,n,val);
	if(found)
		cout<<"\nItem found";
	else
		cout<<"\nItem not found";
	
	return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{
	for(int I=0; I<s; I++)
	{
		if(Arr[I]==VAL)
			return true;
	}
	return false;
}

Output

SAMPLE RUN # 1

Enter number of elements you want to insert 5
Enter element 1: 13
Enter element 2: 11
Enter element 3: 63
Enter element 4: 50
Enter element 5: 67

Enter the number you want to search 50

Item found

SAMPLE RUN # 2

Enter number of elements you want to insert 3
Enter element 1: 33
Enter element 2: 19
Enter element 3: 63
Enter the number you want to search 30

Item not found