Code:
/* WAP to execute the following function:
-> Read an integer & an integer array & search for that integer in the intger array & return its position, incase the number is not there in the list, function should return -1.*/
int search(int siz, int ele, int arr[]);
void main()
{
clrscr();
int arr[25], siz, i, ele, j;
cout<<"Enter the number of elements:";
cin>>siz;
cout<<"Enter the array:";
for(i=0; i<siz; i++)
{
cin>>arr[i];
cout<<"\t";
}
cout<<"Enter the element to be searched";
cin>>ele;
k=search(siz, ele, arr);
if(j==-1)
cout<<"Element not found";
else
cout<<"The element found at"<<j<<"th position";
getch();
}
int search(int siz, int ele, int arr[])
{
int pos=-1;
for(int i=0; i<siz; i++)
{
if(ele==arr[i])
{
pos=i;
break;
}
else
pos=2;
}
if(pos==1)
{
return(i+1);
}
else
{
return(-1);
}
}