Hi guys, I m going through chapther 15 of jumping into C++, and the 2nd exercise says :
"Write a recursive function that takes an array and display the elements in reverse order without starting the index of the array at the end (in other word the opposite of what you'd do with a loop)"
Well, if you're passing an array then you should probably write the function to receive an array. Compare the signature of NameYourFriends to the other functions.
Do you realize that you seem to be trying to create an array of an undefined size?
Also do you realize that in C++ array sizes must be compile time constants. If you want a runtime "array" I suggest you consider std::vector.
friends [size]= NameYourFriends (friends,size);
Do you realize that you passed a pointer to the array into the function and that any change made to the array is reflected in the calling function? There is really no need to try to return an "array", just use the parameter.
Lastly for now: return array [size] ;
Do you realize that this line is trying to return a single element of your array? And remember if the size of the array is the same as "size" that element doesn't exist. Remember arrays start at zero and end at size minus one.
@gdz98
1. Use dummy data and solve a simple version of the problem. You're just distracting yourself with cin input, displays, etc.
2. Simplest is to index i to the function and keep incrementing it until the size is reached
I think you missed one of the points here -- you can use recursion itself to produce the reversal if you recur before output. You end up with a bunch of function calls on the function stack that end up resolving before getting to the output.
In effect we called f(0), then piled f(1) on top of that, then f(2) on top of that, etc..., finally returning when f(5) because 5 is the array size. So it looks like this in the end: