简体   繁体   中英

Generate All Possible Combinations of 6 of K numbers

I have an assignment in which I am not allowed to use " [] ", and to do it only with pointers.
So far my code works alright, but I am facing a problem while I print all possible combinations of 6 of K numbers.
Here is my code:

    # include <stdio.h>
    int main() {
    system("chcp 1253");

    int a, i, j, temp, *ar, k, I, J, K;
    printf("Numbers must be 6 or 49.\n"); /*User can enter 6-49 numbers*/
    scanf("%d",&a);
    while(a<6 || a>49) {
            printf("Wrong, choose again: \n");
            scanf("%d", &a);
        } 

    ar = (int*) malloc(a*sizeof(int)); /*Creating array*/

    system("cls");
    printf("Choosing numbers*/
    for (i=0; i<a; i++) {
        scanf("%d", ar+i);
        while (*(ar+i)<1 || *(ar+i)>49) { /*Numbers must be greater than 1 and less than 49*/
        printf("Wrong number, choose again: \n");
        scanf("%d", ar+i);
        }
    }

    for (i=0; i<a; i++) { /*Sorting array*/
        for (j=i+1; j<a; j++) {
            if (*(ar+i) > *(ar+j)) {
                temp = *(ar+i);
                *(ar+i) = *(ar+j);
                *(ar+j) = temp;
            }
        }
    }

    printf("\n\n"); /*Printing all possible 6 combinations of K numbers*/
    for(i=1; i<=a-5; i++) {
        for(j=i+1; j<=a-4; j++) {
            for(k=j+1; k<=a-3; k++) {
                for(I=k+1; I<=a-2; I++) {
                    for(J=I+1; J<=a-1; J++) {
                        for(K=J+1; K<=a; K++) {
                            printf("%d|%d|%d|%d|%d|%d|\n", *(ar), *(ar+i), *(ar+j), *(ar+k), *(ar+I), *(ar+J));
                            }
                        }
                    }
                }
            }
        }


    free(ar);
    return 0; 
}

And let's say that the user enters 6 numbers, the print of combinations is correct(1|2|3|4|5|6).
But if user chooses anything else, for example 7 numbers, the results are:

1|2|3|4|5|6
1|2|3|4|5|6
1|2|3|4|5|7
1|2|3|4|6|7
1|2|3|5|6|7
1|2|4|5|6|7
1|3|4|5|6|7

I am stuck and I can't figure out what's my wrong, any hint please?
I am 95% sure that the mistake is at printf, but I tried several changes and none worked.
Sorry for my english,
Cheers,
pronoobgr

I found my problem, when user was entering numbers, I had my code like this:

printf("Choosing numbers*/
    for (i=0; i<a; i++) {
        scanf("%d", ar+i);

and at printing the combinations the "i" was set to 1. So I changed the "i" at the "Choosing numbers", and set it i=0:

printf("Choosing numbers*/
        for (i=1; i<a; i++) {
            scanf("%d", ar+i);

and now everything works! Anyway, thanks for your time!

Your problem is that you are using as many loops as many numbers you have, but the number of numbers is dynamic if you mean to work with permutations. I will give you the idea only, as this is obviously homework:

  • use a stack to store your current state
  • the stack should have the size of K and should have values of -1
  • you should operate with the indexes, rather than the values, only use the values when you output the solution in the form of *(ar + index)
  • given depth < K, find the first possible index which is greater than the index at depth. If there is such an index, then assign it to the given position and increase depth. Otherwise decrease depth
  • if depth reaches K, then you have a solution, print it, or do whatever you need to do with it
  • if depth reaches -1, then the algorithm successfully finished

This is not a good design to print combinations. your programs complexity is O(k^n) whereas the same problem can be solved with O(2^n) complexity by using a two-way recursion function. Moreover, rather than using 6 nested loops you can simply use a multi recursion loop. Multi recursion loop will help you control the depth of nesting of loops. and that way your program will be able to solve combination problems like nCk directly (rather than nC6, wherein you are using a constant value 6 for k).

Anyway, you can read about above-mentioned stuff in free time. Now, let me come back to your original problem. The issue is in your nested loops. I correct your loops in the below code snippet.

1. you should not decrement 'a' at every nested loop (eg a-5, a-4 is wrong)
2. start 'i' from value '0'
3. don't use '<=' operater in the loop. rather use '<' operator.
4. printf printing wrong values, correct values are => *(ar+i), *(ar+j), *(ar+k), *(ar+I), *(ar+J), *(ar+K).

  for(i=0; i<a; i++) {
      for(j=i+1; j<a; j++) {
          for(k=j+1; k<a; k++) {
              for(I=k+1; I<a; I++) {
                  for(J=I+1; J<a; J++) {
                      for(K=J+1; K<a; K++) {
                          printf("%d|%d|%d|%d|%d|%d|\n", *(ar+i), *(ar+j), *(ar+k), *(ar+I), *(ar+J), *(ar+K));
                        }
                    }
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM