In: Computer Science
In C++, write a function to fill an array of size 13 with values
13 11 9 7 5 3 1 2 4 6 8 10 12
recursively, starting with the value 1 in the middle.
Since we need to insert the values recursively in the array. It is important to identify the base condition to terminate the recursion.
If the value we are inserting is less than 1 , we will return . => base case
Now we need to observe that the odd values are inserted first and even values are inserted towards end.
So in every recursive call to our method , we will check if the value is odd than we will insert it in the array and than made a recursive call with value decremented by 1.
After the recursive call we will put the even values in the array.
To keep track of index we are inserting the values at, we could make index as a global variable , and post increment the value of index by 1 at each insertion.
Take a look at the code, its easy enough to understand, if you have basic idea of recursion.
If you still have any doubt, just put a comment on the solution, i would be happy to help you out.