I am solving a problem in recursion to print all the indices of a number. For example: arr[] = {8,8,10,8,8} and element = 8 then we should have output array as: {0,1,3,4}
I am not getting the output as expected. Here is my code solution:
#include<iostream>
using namespace std;
int allIndexes(int input[], int size, int x, int output[]) {
if(size==0){
return 0;
}
if(input[0] == x){
output[0] = 0;
}
int ans = allIndexes(input+1,size-1,x,output);
for(int i=1;i<ans;i++){
output[i] = output[i]+1;
}
ans = ans+1;
return ans;
}
int main(){
int arr[] = {8,8,10,8,8},output[5]={0};
int size = allIndexes(arr,5,8,output);
cout<<size<<endl;
for(int i=0;i<size-1;i++){
cout<<output[i]<<" ";
}
return 0;
}
Why I am getting wrong?
Aucun commentaire:
Enregistrer un commentaire