Home > Net >  C recursive function to check between two arrays if they are reversed or not
C recursive function to check between two arrays if they are reversed or not

Time:12-24

I need to check between two arrays if they are reversed, e.g. A[3] = {1, 2, 3} and B[3] = {3, 2, 1}

Returning zero if A is not the reverse of B of a given length, and 1 otherwise.

Here is what I managed to do for now

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

int* input_array(int);
int areReversed(int*, int*, int);

int main()
{
    int n = 0;
    int* A, * B;
    printf("please enter the size of arrays: \n");
    scanf("%d", &n);
    printf("please enter elements of the first array: \n");
    A = input_array(n);
    printf("please enter elements of the second array: \n");
    B = input_array(n);
    areReversed(A, B, n) ? printf("Arrays are the opposite to one another.\n") : printf("Arrays are not the opposite to one another.\n");
    free(A); free(B);
}

int areReversed(int* A, int* B, int n) {
    int i = 0, j = n-1;
    int reverse = 0;
    if (n > 1)
    {
        for (i = 0, j = 0; i < n && j >= 0; i  , j--)
        {
            if (A[i] == B[j])
            {
                return areReversed(A   1, B   1, n);
            }
            if (A[i] != B[j])
                return 0;

        }
        return 1;
    }
}

int* input_array(int n) {
    int i;
    int* A = (int*)malloc(n * sizeof(int));
    assert(A);
    printf("Enter %d integers\n", n);
    for (i = 0; i < n; i  ) {
        scanf("%d", A   i);
    }
    return A;
}

`

but sadly its not working and I have tried so many things... even if you can give hints its will be awesome

CodePudding user response:

areReversed can be simply:

int areReversed(int *A, int *B, int n) 
{
    return n == 0 || A[0] == B[n-1] && areReversed(A 1, B, n-1);
}

This works:

  • If n is zero, the two arrays are trivially reverses of each other, since both are empty. (We expect n is not negative.)
  • Otherwise, we compare the first element of A to the last element of B. If they are unequal, this == and the && fail (produce zero for “false”), and “false” is returned.
  • If they are equal, we also require the rest of the arrays to be reversed, which is handled by a recursive case: The remaining n-1 elements of A must be the reverse of the first n-1 elements of B.

CodePudding user response:

With recursion the key is:

  1. To have a robust terminating condition that will be met, and which has a definitive answer.
  2. The problem is a function of a smaller but identical problem.

In this case, the terminating condition is when the length of the arrays n is zero or less (return true), or when A[0] != B[n-1] (return false)

For an array length n where the two opposite ends are equal (A[0] == B[n-1]), A may be the reverse of B, so you turn the problem into a smaller one and test that. The smaller problem is "one-in" from each end of each array - i.e.:

areReversed( &A[1], &B[1], n - 2 ) ;

Note the n-2 - that is why the terminating condition is n <= 0 since an array with an odd number of elements will recurse to n = -1. You could trap that rather then recurse, but it serves no purpose to do so and ad complexity.

So:

#include <stdio.h>
#include <stdbool.h>

bool areReversed( int* A, int* B, int n) 
{
    int is_reverse = false ;
    if( n <= 0 )
    {
        is_reverse = true ;
    }
    else if( A[0] == B[n-1] )
    {
        is_reverse = areReversed( &A[1], &B[1], n - 2 ) ;
    }

    return is_reverse ;
}
int main()
{
    int A1[] = {1, 2, 5, 14, 9, 3} ;
    int B1[] = {3, 9, 14, 5, 2, 1} ;
    int A2[] = {1, 2, 5, 14, 9, 3} ;
    int B2[] = {3, 9, 14, 5, 2, 7} ;
    
    bool R1 = areReversed( A1, B1, sizeof(A1) / sizeof(*A1) ) ;
    bool R2 = areReversed( A2, B2, sizeof(A2) / sizeof(*A2) ) ;

    printf( "A1 %s the reverse and B1\n", R1 ? "is" : "is not" ) ;
    printf( "A2 %s the reverse and B2\n", R2 ? "is" : "is not" ) ;
}

Outputs:

A1 is the reverse and B1
A2 is not the reverse and B2

And to demonstrate its function with an odd number of elements:

int A1[] = {1, 2, 5,   99, 14, 9, 3} ;
int B1[] = {3, 9, 14, 101, 5, 2, 1} ;
int A2[] = {1, 2, 5,  100, 14, 9, 3} ;
int B2[] = {3, 9, 14, 100, 5, 2, 1} ;

Output then is:

A1 is not the reverse and B1
A2 is the reverse and B2

I recommend to understand recursion, you use your debugger to step through the code, stepping in to each recursive call to observe the "smaller problem" and the meeting the terminating condition, and stepping-out to observe the "unwinding" of the algorithm and final return. In any event you should learn effective use of a debugger - it is a great learning tool to observe the precise behaviour of code and state of variables as well as a debugging aid.

  • Related