My recursion of getting subset doesn't print out the right answer

cooker

I am trying to implement a method to get all subset of a set. I understand the logic of doing that. i.e. Subset(n) = n + Subset(n-1), but the code I wrote keep printing out the wrong answers. Here is my code:

void subset(vector<int> &input, vector<int> output, int current) {
    if (current == input.size()-1) {
        output.push_back(input[current]);
        print(output);
    }
    else {
        for (int i = current; i < input.size();i++) {
            output.push_back(input[i]);
            print(output);
            subset(input, output, i+1);
        }
    }
}

Here is the output I got (input is {1,2,3}):

1 
1 2 
1 2 3 
1 2 3 
1 2 
1 2 3 
1 2 3

Any idea where I went wrong? Any help will be appreciated!

Emadpres

I think this can help you:

void subset(vector<int> &input, vector<int> output, int current) {
    static int n=0;
    if (current == input.size()) {
        cout<<n++<<":\t {";
        for(int i=0;i<output.size();i++) cout<<output[i]<<", ";
        cout<<"\b\b}\n";
    }
    else {
        subset(input, output, current+1); //exclude current'th item
        output.push_back(input[current]);
        subset(input, output, current+1); //include current'th item 
    }
}

and first time you may call it like this:

int main()
{
    std::vector<int> inp, out;
    for(int i=0; i<5; i++) // for example {1,2,3,4,5}
        inp.push_back(i);
    subset(inp, out, 0);
    return 0;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Splitting by delimeter doesn't give the right answer

Instanceof doesn't return right answer

my implementation of linked list doesn't print out anything

My program doesn't print out Strings converted to ints

My Java project doesn't print out anything

Why doesn't my ArrayList print out the output desired in Java?

Why won't my indexes be parallel with each other when I try to print out the answer?

The right answer is not out of range of 'Integer', but why is my output negative?

I dont know my calculator doesnt print out answer

Angular project doesn't answer to my request

My returned answer doesn't apppear in label

Trying to calculate sunrise...ain't getting the right answer

Rxjava, Retrofit response body doesn't get the right answer?

The minimize function of scipy.optimize doesn't give the right answer

The for loop doesn't loop even when the answer is not right

Union doesn't print right values in C

Can't figure out why im getting null values in my array print statement

Javascript: challenge - not getting the right answer

Problem with while and if statement which doesn't print answer

Why doesn't my showAllUsers() method print out the list of users with the correct formatted header for each object?

My webpage doesn't display the table I'm trying to print out

How to print out the indices of the answer?

I can't get an answer out of my C++ program

UIDatePicker doesn't print out a date

Connect to database doesn't print out error

Jekyll plugin doesn't print variable out

My query doesn't return the right count

why my arraylist doesn't keep the last answer?

what is the problem with my code linreg.predict() not giving out right answer?