3

I was having some problem when trying to do a reverse array using recursion. Here is the function prototype:

void rReverseAr(int ar[ ], int size); 

And here is my code:

int main()
{
    int ar[10], size, i;

    printf("Enter array size: ");
    scanf("%d", &size);
    printf("Enter %d numbers: ", size);
    for (i = 0; i<size; i++)
        scanf("%d", &ar[i]);
    rReverseAr(ar, size);
    printf("rReverseAr(): ");
    for (i = 0; i<size; i++)
        printf("%d ", ar[i]);
    return 0;
}

void rReverseAr(int ar[], int size) {
    int start = 0, end = size - 1, temp;
    if (start < end) {
        temp = ar[start];
        ar[start] = ar[end];
        ar[end] = temp;
        start++;
        end--;

        rReverseAr(ar, size - 1);
    }       
}

The expected output should be when user entered 1 2 3 and it supposed to return 3 2 1. However, with these code, the output that I am getting is 2 3 1.

Any ideas?

3 Answers 3

7

Your code is almost right. The only problem is that instead of "shrinking" the array from both sides, you shrink it only from the back.

The recursive invocation should look like this:

rReverseAr(ar + 1, size - 2);

You do not need to increment start or decrement end, because their values are not used after modification.

Sign up to request clarification or add additional context in comments.

3 Comments

Alright thanks a lot! It works! However, can I know why for each recursion, you are increasing the array element position and at the same time, you are minusing the array size by 2? I mean like how +1 and -2 restrict the array shrinking from the back only?
@Denise Each recursive call passes a "sub-array" to the next level. This sub-array starts one element after the initial element (i.e. at ar+1 in pointer arithmetic syntax, or &ar[1] in array index syntax). The size of the sub-array is less than the size of the original array by 2, because the initial and the last elements have already been processed.
I see I see. Thanks a lot for the help!
0

A Simple way :

#include<stdio.h>
using namespace std;

void revs(int i, int n, int arr[])
{
  if(i==n)
  {
    return ;
  }
  else
  {
    revs(i+1, n, arr);
    printf("%d ", arr[i]);
  }
}


int main()
{
  int i, n, arr[10];
  scanf("%d", &n);
  for(i=0; i<n; i++)
  {
      scanf("%d", &arr[i]);
  }
  revs(0, n, arr);

  return 0;
}

Iterate array with recursion in C : link

Comments

0

What you are doing is to exchange values of the 1st and last elements and do the recursion.

Every time you should move your address to the next element as the starter for the next array exchange.

A possible way:

void rReverseAr(int ar[], int size){
      int buffer=ar[0]; 
      ar[0] = ar[size-1];
      ar[size-1] = buffer;
      if ((size!=2)&&(size!=1)) rReverseAr(ar+1,size-2);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.