0

I have learnt C language at school but I'm not good at it... And when I was trying to implement this algorithm using C language:

ReverseArray(int A[], int i, int j) {
   Input: Array A, nonnegative integer indices i and j
   Output: The reversal of the elements in A starting at index i and ending at j
   if i < j then
      swap A[i] and A[j]
      ReverseArray(A, i+1, j-1)
}

I managed to code this:

int *reverseArray(int A[], int i, int j) {
   int *R = NULL;
   if(i < j) {
      int temp = A[j];
      A[j] = A[i];
      A[i] = temp;
      R = reverseArray(A, i+1, j-1);
      return R;
   } else {
      return R;
   }
}

But when I tried to print the original and reversed array in the main:

int main(void) {
   int A[] = {1, 3, 5, 6, 8, 3, 4, 2};

   int *r = reverseArray(A, 0, 7);

   //This prints out the reversed array, when I intended to print the original
   for (size_t i = 0; i < 8; i++) {
      printf("%d ", A[i]);
   }
   printf("\n");

   /* This was intended to print the reversed array but doesn't work
   for (size_t i = 0; i < 8; i++) {
      printf("%d ", r[i]);
   }
   */

   return 0;
}

Could anyone please explain why the commented out for loop doesn't work? And why the first for loop prints out the reversed array... Is there any other way to get the result of reverseArray() without using *r? I tried to malloc *r just in case that was the problem, but it still didn't work.

Thank you.

5
  • 2
    You are returning NULL from reverseArray. Commented Jul 3, 2015 at 12:10
  • Oh you're right! But then, what should I be returning instead? That's where you have to stop the recursion right? (when i == j....or larger) Commented Jul 3, 2015 at 12:12
  • 3
    Make it void reverseArray(). You don't need to return anything. The data is changed directly. Commented Jul 3, 2015 at 12:13
  • Thank you! Returning A fixed it :D And yes, changing it to void also helped! Commented Jul 3, 2015 at 12:25
  • There is no reason to use recursion for this. Commented Jul 3, 2015 at 13:17

4 Answers 4

2

Just don't return anything. You make a reversion in place, so the resulting array is the same as the array to be reversed, and the caller knows it already.

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

Comments

2

You need to print the contents of A before you call reverseArray, not after. The reason is that you are reversing the bytes in place so the array A itself is changed by calling reverseArray.

1 Comment

haha that's true! I can't believe I got confused about that, thank you.
1
  • A try from your code base and the problem description

If allowed to rewrite the Array in place, then it will work

#include<stdio.h>

void reverseArray(int A[], int i, int j) {
   //int *R = NULL;
   if(i < j) {
      int temp = A[j];
      A[j] = A[i];
      A[i] = temp;
      reverseArray(A, i+1, j-1);
   }
}

int main(void) {
   int A[] = {1, 3, 5, 6, 8, 3, 4, 2};

   //This prints out original array
   for (size_t i = 0; i < 8; i++) {
      printf("%d ", A[i]);
   }
   printf("\n");

   reverseArray(A, 0, 7);

   // print the reversed array
   for (size_t i = 0; i < 8; i++) {
      printf("%d ", A[i]);
   }

   return 0;
}
  • It will Output:

1 3 5 6 8 3 4 2
2 4 3 8 6 5 3 1

Comments

0

R is always assigned to NULL, and A is not a pointer, then you are editing the real data of the array.

if you want to reverse and create a new array, you must do something like that :

int *reverseArray(int array[], int arraySize) {
    int *reversedArray = malloc(sizeof(int) * arraySize);

    for ( int i = 0 ; i < arraySize ; ++i ) {
        reversedArray[i] = array[arraySize - i - 1];
    }
    return reversedArray;
}

You can also do it in recursive way :

int   *reverseArray(int inputArray[], int arrayLength ) {

    int   *_reverseArray (int inputArray[], int arrayLength, int *outputArray, int actual) {

            if (outputArray == NULL) {
                outputArray = malloc(sizeof(int) * arrayLength);
            }
            if (actual < arrayLength) {
                outputArray[actual] = inputArray[arrayLength - actual - 1];
                return _reverseArray(inputArray, arrayLength, outputArray, ++actual);
            }
            return outputArray;

    }
    return _reverseArray(inputArray, arrayLength, NULL, 0);
}

If you want to edit the original array :

void    reverseArray(int array[], int arraySize)
{
    for ( int i = 0 ; i < arraySize / 2 ; ++i ) {
        array[i] ^= array[arraySize - i - 1];
        array[arraySize - i - 1] ^= array[i];
        array[i] ^= array[arraySize - i - 1];
  }
}

3 Comments

Oh that makes sense, that I am using the original A as a parameter... But if that's the case, what should I do with the else loop? If I don't write anything, there's a compiler error... Also do I still need int *r = reverseArray() in the main then? Even if I'm not going to use r afterwards?
@Ceria if you're reversing the array in-place, there is no need to return anything. Just declare the function as returning void.
array[arraySize - i] accesses array[arraySize] (invalid memory location) in the first iteration.

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.