I cannot find a way to return an array or vector from a function when using recursion in it.
Here’s my code as the solution to this problem.
Link: https://ide.codingblocks.com/s/192194
Please help me retrun a vector from the findIdx() function with the same answer.
Returning an array
HI @kapurm17 you can do 2 things,
- pass the array around as an argument. Since arrays are passed by reference anyway, any changes made during the recursive calls will be reflected in it.
- If you dont want changes to be reflected in the original array, its better to use a vector because vectors are passed as a copy by default, but they can be made to pass be reference by adding & operator
- You can change the return type of the function from void to arr* or vector and return the values during recursion call
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.