Click here to Skip to main content
15,743,541 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
We have to write a code to get a reverse of an array using recursive function to which only we pass length of array as argument.
And length of array, reading of elements, printing of reverse will be done through MAIN function.

Following is the iterative algorithm. how to convert it into a recursive function??

What I have tried:

int main(void){
    int a[100], n, i, temp;
    printf("Enter the number of elements you want to enter.\n=>");
    scanf("%d", &n);
    printf("\n\nEnter the numbers:- \n");
    for(i=0; i<n; i++ ){
        scanf("%d", &a[i]);
    printf("\n\nThe reverse of the entered array is:- \n\n");

    for(i=0; i<n/2; i++){
        temp = a[i];
        a[i] = a[n-i-1];
        a[n-i-1] = temp;

    for(i=0; i<n; i++){
     printf("%d  ", a[i]);

CPallini 14-Feb-16 9:08am    
Your iterative algorithm uses both the array and the array length. A recursive function would need thoese two arguments too.
Dishank Bansal 20-Feb-16 7:52am    
yeah. sorry, you can take array as argument also..

Think about it. How would you do this manually?
Assume you have a row of pennies on the desk in front of you:

If you take the number of elements in the line (8) and swap the last element with the first:
Then reduce the count by one, and swap that element with the first non-swapped element:
And continue until you have less than two elements to swap, you have finished, and it's reversed.
So write your method to take the length of the array and use that to generate the two indexes to swap.
Then check if you have any elements to swap, and if not, return.
Otherwise, swap them, and call yourself with one less value.
Share this answer
Dishank Bansal 14-Feb-16 9:01am    
but for that count, i have to make a static variable which i want to avoid.
OriginalGriff 14-Feb-16 9:48am    
All you have to do is move the array and "n" outside the main method. That's not static, it's global. You should never consider static variables inside recursive methods - they are very hard to use well.
Dishank Bansal 14-Feb-16 18:10pm    
yeah. i mean i want to avoid static as well as global variable. suppose it as a imposed condition on the question.
OriginalGriff 15-Feb-16 2:18am    
The only other way to do it is to pass two parameters to the function, one a pointer to the first element that needs swapping, and the second the length left unswapped. When you recurse, you increment the pointer, and decrement the length by two.
Dishank Bansal 16-Feb-16 6:14am    
is there a way such that Like you may use global or static variable but imposed condition is that you can only pass array and its length to the function not indexes as arguements.
First of all don't mince about using an array name and a size - if you have to do it recursively use a couple of pointers to the first and last characters to be swapped. Something like:

void reverse_range( int *first_to_swap, int *last_to_swap )
    if( first_to_swap < last_to_swap )
        *first_to_swap ^= *last_to_swap ^= *first_to_swap ^= *last_to_swap;
        reverse_range( first_to_swap++, last_to_swap-- );

(This is untested BTW, so don't be surprised if it's buggy. Likewise if you have to only have one parameter or some other balls ache of an artificial restriction then you converting the code is easy enough - it's just work.)

This is instructive 'cause If you compare this to the iterative version...

void reverse_range( int *first_to_swap, int *last_to_swap )
    while( first_to_swap < last_to_swap )
        *first_to_swap ^= *last_to_swap ^= *first_to_swap ^= *last_to_swap;
        first_to_swap++, last_to_swap--;

you'll get a general pattern as to how to make a recursive version out of an iterative implementation. Replace the while with a conditional and instead of updating the iteration state call the function again with what would be the updated variables.

More importantly you get the opposite - how to convert from a recursive implementation to an iterative one which is often more important. Loads of problems can be expressed easier as a recursive solution but usually an iterative solution is faster. Having the mental tools to switch between the two is a big help.
Share this answer
CPallini 20-Feb-16 9:22am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900