Linear search always returning -1

#include
using namespace std;
int Lsearch(int A[],int n,int m)
{
for(int i=0;i<n;i++)
{
if(A[i]==m)
return i;
}

	 		return -1;

}

int main()
{
int n,m,index;
cin>>n;
int A[n];
for(int i=0;i<n;i++)
{
cin>>A[i];

}
cin>>m;
index=Lsearch(A,n,m);
cout<<index<<endl;

return 0;

}

This code is correct according to my understanding of the question. If it is not working, please give the problem link so that i can verify.

1 Like

Hi ,

I think you need to create dynamic array

you can replace with int[] A = new int[n]; instead of int A[n];

1 Like

Static array wont produce any error.

1 Like