I've tried to write a code on linear search using recursion. the program stores all occurrences of target in a vector. the code is giving incorrect output on vs code. but it is working correctly on online compilers.
#include<iostream>
#include<vector>
using namespace std;
vector<int> linearsearch(int *arr, int target, int size){
static vector<int> v;
static int i=0;
if(arr[i]==target)
v.push_back(i);
if(i>=size)
return v;
i++;
return linearsearch(arr,target, size);
}
int main(){
int arr[7]={3,7,7, 8, 7, 0, 5};
int target=7;
vector<int> q=linearsearch(arr,target, 7);
cout<<"printing all occurences of target"<<endl;
for(int j=0;j<q.size();j++){
cout<<q[j]<<endl;
}
return 0;
}
online compilers are correctly printing 1 2 4. but vs code is printing 1 2 4 7.