-1

This is a programme that try to figure out all derangement of array {10, 11, 12, 13}

But it doesn’t work, because when it comes to depth==0, i==0 and Peano[]=={0, 1, 2, 3}, the for loop didn’t end then i decrements to be -1. What hell is going on?

      #include<stdio.h>
        #include<stdlib.h>
        unsigned long long derange(int *array, int LEN, _Bool display, int *Peano, unsigned depth);
        int main(void){
        int i ,LEN=4, *Peano=malloc(sizeof(int));
        int array[4]={10,11,12,13};
        for(i=0; i<LEN; i++)Peano[i]=i;
        derange(array, LEN, 1, Peano, 0);
        printf("this derangement of the array total of %llu.\n", derange(array, LEN, 0, Peano, 0));
        return 0;}

            unsigned long long derange(int *array, int LEN, _Bool display, int *Peano, unsigned depth){
                    int i, temp;
                    unsigned long long count=0;
                    if(depth==LEN){
                            if(display){
                                    for(i=0; i<LEN; i++)
                                            fprintf(stdout, "%d\040", array[i]);
                                    putchar('\n');}
                            return 1;}
                    for(i=LEN-1; i>=depth; i--){
                            if(i==Peano[depth])continue;
                            temp=Peano[i]; Peano[i]=Peano[depth]; Peano[depth]=temp;
                            temp=array[i]; array[i]=array[depth]; array[depth]=temp;
                            count += derange(array, LEN, display, Peano, depth+1);
                            temp=Peano[i]; Peano[i]=Peano[depth]; Peano[depth]=temp;
                            temp=array[i]; array[i]=array[depth]; array[depth]=temp;}
                    return count;}
sanriot
  • 804
  • 4
  • 13
brushmonk
  • 109
  • 7
  • 2
    Please clean up the presentation of your code. What's the point of allocating `Peano`? You want it to be an array of 4 (or `LEN`) ints, but you only allocate storage for one int. Why not use `int Peano[LEN]`? – M Oehm Mar 08 '20 at 15:28

1 Answers1

0

Here:

int i ,LEN=4, *Peano=malloc(sizeof(int));

you allocate space for a single integer to Peano. Peano is an index array that goes alongside array, of length 4. Just create it as automatic variable on the stack:

int Peano[4];

There's another problem with your loop in derange:

for (i = LEN - 1; i >= depth; i--) ...

That is a loop tat counts the signed int i downwards. Your variable depth is unsigned, however. That means that in the comparson i >= depth the signed i is "promoted" to unsigned in. If depth is zero, that comparison will always be true, because i >= 0 is true for all unsigned ints per definition.

You can fix this by either making depth an int instead of an unsigned or by using a downwards loop ariant that works for signed and unsigned types:

for (i = LEN; i-- > depth; ) ...

or, if you don't like the empty update clause

i = LEN;
while (i-- > depth) ...
M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • what conditions will int type convert to unsigned type? what conditions will unsigned type convert to int type? because c primer plus said "When appearing in an expression, char and short, both signed and unsigned, are automatically converted to int or, if necessary, to unsigned int. " – brushmonk Mar 09 '20 at 05:27
  • If two integers have the same "[rank](https://stackoverflow.com/questions/38854596/integer-conversion-rank-and-promotion)", but one is unsigned and the other is signed, the common type is the unsigned type of that rank, in your case `unsigned int`. – M Oehm Mar 09 '20 at 06:15
  • In general, your code suffers from a certain untidiness, not only in presentation but also in logic. It is okay to use an unsigned type for things that cannot be negative like array sizes and positions, but if you use unsigned, you should use unsigned throughout, that is for the `peano` array and its sizes. If you use unsigned, make sure that your counters don't get below zero, because if so, they will be regarded as very large numbers. Use the loops I've shown in my answer. – M Oehm Mar 09 '20 at 06:18