#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, )
i <= index;is immediatelytruesince-1was passed toindex. So empty parentheses are output. What is the expected output?int *curr = (int*)malloc(sizeof(int) * 50)instead ofint curr[50]?falsebut I am sure you realise I mean the loop does not iterate.