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.

Ceria
  • 97
  • 2
  • 9

4 Answers4

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.

CiaPan
  • 9,381
  • 2
  • 21
  • 35
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.

jarmod
  • 71,565
  • 16
  • 115
  • 122
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

Eric Tsui
  • 1,924
  • 12
  • 21
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];
  }
}
eroween
  • 163
  • 11
  • 1
    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 Jul 03 '15 at 12:18
  • @Ceria if you're reversing the array in-place, there is no need to return anything. Just declare the function as returning `void`. – Quentin Jul 03 '15 at 12:19
  • 2
    `array[arraySize - i]` accesses `array[arraySize]` (invalid memory location) in the first iteration. – Spikatrix Jul 03 '15 at 12:20