I've tried to perform a simple binary/linear search but there's a problem with the output, I suspect the function isn't being called
A small portion of void main:
void main()
{ cout<<"Linear or Binary? (1/2)"<<endl;
cin>>ch;
switch(ch)
{
case '1': pos = linear();
cout<<"Position: "<<pos;
break;
case '2': pos = binary();
cout<<"Position: "<<pos;
break;
default: cout<<"error"<<endl;
}
}
//here is a function:
int linear()
{
int a, n, ar[n], posn;
cout<<"Enter size of array and array"<<endl;
cin>>n;
for(int i =0; i<n; i++)
{
cin>>ar[i];
}
cout<<"enter element to be found"<<endl;
cin>>a;
for(int j=0; j<n; j++)
{
if(ar[j]==a)
{
posn= j+1;
}
}
return posn;
}
The output is just garbage or junk. None of my couts are showing up, simply one random int junk value.