0
#include <stdio.h>

void ScanArray (int* , int*);
int Pair (int* , int);

int main () {

        int a [15] , n;

        ScanArray (a , &n);

//      printf("\nHello World !!!\n");

        if(Pair(a , n) == 0)
                printf("The array fulfills the requirements");
        else
                printf("The array does not fulfills the requirements");

        return 0;
}

void ScanArray (int *a , int *n) {

        int i , f = 0;


        do {
        printf("Enter the number of integers : ");
        scanf("%d",n);

        if (*n > 15)
                printf("Enter number bellow 15 \n");
        else
                f=1;
        } while(f == 0);

        printf("Enter the %d integers : ",*n);
        for (i = 0 ; i < *n ; i++)
                scanf("%d",a+i);

}

int Pair (int *a , int n) {

        if (n <= 1)
                return 0;
        else
                if (*a-*(a+1) != 1 && *a-*(a+1) != -1)
                        return 1;
                else
                        return Pair(a++ , n--);

}

don't know why it's not working.

Segmentation fault (core dump).

pat
  • 12,587
  • 1
  • 23
  • 52
  • 3
    Where does it crash? Have you tried a debugger like gdb? – John Kugelman Mar 03 '14 at 17:48
  • 1
    YOu are getting a stack overflow in Pair(), which never exits, and is recursive. – OldProgrammer Mar 03 '14 at 17:50
  • This is really a "give a man a fish / teach a man to fish" moments. I'm sure this will not be the last core dump you will ever get. Try compiling your program with debug enabled (-g under gcc), run it, and load the core into gdb. There should be plenty of gdb tutorials on the interwebs. – pat Mar 03 '14 at 18:01
  • you should at least accept the correct answer – ppaulojr Apr 27 '16 at 12:49

1 Answers1

4
else
    return Pair(a++ , n--);

Using postfix increment and decrement operators will result in the recursive call processing the same values. You should either use prefix operators, or even better, just add and subtract 1.

else
    return Pair(a + 1 , n - 1);

I say that's better because it's misleading to think modifying the values matters; the key to recursion is that the recursive calls will have their own copies of a and n, so modifying them in a parent has no effect on the children.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578