0
#include <stdio.h>

void powerSet(int* a, int index, int *curr, int N) {
    if (index == N)
        return;

    printf("(");
    for(int i = 0; i <= index; i++)
        printf("%d, ", curr[i]);

    printf(")\n");
    // processing here.


    int x = index + 1;
    for (int i = index + 1; i < N; i++) {

        curr[x]  = a[i];
        // curr += str[i];
        powerSet(a, i, curr, N);
    }
    return;
}

int main(){

    int a[] = {10,12,14,17};
    int *curr = (int*)malloc(sizeof(int) * 50);
    int n = 4;

    powerSet(&(*a),-1,curr,n);
}

There seems to be some logical error with the above code. Can someone point it out, please? The output you get when you run the code looks something like this:

() 
(10, )
(10, 12, )
(10, 12, 14, )
(10, 12, 14, 17, )
(10, 12, 17, 17, )
(10, 14, 17, )
(10, 14, 17, 17, )
(10, 17, 17, 17, )
(12, 17, )
(12, 17, 14, )
(12, 17, 14, 17, )
(12, 17, 17, 17, )
(14, 17, 17, )
(14, 17, 17, 17, )
(17, 17, 17, 17, )
7
  • 1
    In the first loop, the condition i <= index; is immediately true since -1 was passed to index. So empty parentheses are output. What is the expected output? Commented Feb 27, 2020 at 9:24
  • 1
    Unrelated, but why int *curr = (int*)malloc(sizeof(int) * 50) instead of int curr[50]? Commented Feb 27, 2020 at 9:30
  • ...I mean false but I am sure you realise I mean the loop does not iterate. Commented Feb 27, 2020 at 9:30
  • 2
    @Giridhar Please edit your question to add requested information. Instead of describing the output like "the powerset of the array", please show how exactly the output should look like for the example array in your code. Commented Feb 27, 2020 at 9:57
  • 1
    @Bodo: The question contains sufficient information. Commented Feb 27, 2020 at 10:23

1 Answer 1

0

powerSet(a, i, curr, N); is wrong. At this point, some elements of a have been added to curr or used previously and, either way, are no longer eligible for inclusion. The call could be changed to powerSet(a+i+1, x, curr, N-i-1);, so the first and fourth arguments describe the elements remaining to be considered (the first is a pointer to where they start, the fourth is the number of them), and the second and third arguments describe the elements currently in the working set (the third is a pointer to where they start, and the second is the index of the last of them, or −1 if there are none yet).

To adapt the rest of the code to this change, delete if (index == N) and return; (because we will know we are done when N is zero, indicating there are no elements left to consider, and the final loop will handle this naturally) and change for (int i = index + 1; i < N; i++) to for (int i = 0; i < N; i++) (so we loop over the elements remaining to be considered, which does not involve index).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.