-2

I am writing a program to find if a given sum is possible from a subset of an array. (Subset sum problem). Although many implementations are available on the internet, I have chosen to code it myself. please suggest appropriate corrections. I am doing this using dynamic programming.

#include<iostream>

using namespace std;

bool subsetsum(int a[],int n,int s){

    bool T[n][s];

    for(int j=0;j<n;j++){
        T[j][0]=true;
    }
    for(int k=1;k<n;k++){
        T[0][k]=false;
        for(int l=1;l<n;l++){
            if((a[k]<s) && ((subsetsum(a,n-1,s-a[k])) || (subsetsum(a,n-1,s)))){
                T[k][l]=true;
            }
            else{
                T[k][l]=false;
            }
        }
    }
    return T[n][s];
}   

int main(){
    int n,s;
    int a[n];

    cout<<"Enter number of elements:\n";
    cin>>n;
    cout<<"Enter sum required\n";
    cin>>s;

    cout<<"Enter elements:\n";
    for(int i=0;i<n;i++){
        cin>>a[i];
    }

    cout<<subsetsum(a,n,s);
}

The output I obtained is below:

Enter number of elements:
3
Enter sum required
5
Enter elements:
1
4
2
0

I do not understand why I obtained 0 as the result when boolean should have been returned.

harshrd
  • 79
  • 9
  • 1
    please explain what you mean with "I cannot". I can anticipate what error you get, but maybe I am wrong, so please include errors / whatever you think is the problem in the question – 463035818_is_not_an_ai Jun 04 '19 at 10:20
  • 1
    Please provide a [MCVE]. – Vittorio Romeo Jun 04 '19 at 10:21
  • 1
    Your code isn't valid C++ anyway. Variable-length arrays aren't standard C++. – melpomene Jun 04 '19 at 10:22
  • 2
    `bool T[][]` should not appear in function signature, `int a[]` is actually a pointer (and should not appear in function signature either), `bool T[n][s];` is not a valid in C++ because array size must be a compile-time constant. Moreover, it is still a 1d array. – user7860670 Jun 04 '19 at 10:22
  • 1
    out of curiosity: where did you get that `cin >> n; int a[n];` from? This pattern is popping up rather frequently here, while I dont see any good reason to teach it or use it in serious code – 463035818_is_not_an_ai Jun 04 '19 at 10:30
  • @formerlyknownas_463035818 It might be caused by the fact that entry level programmers are not aware that C and C++ are considered as separate languages with diverging features over time. She/he might've simply seen it somewhere - not realizing that it was C code, or C++ code for a specific compiler which supports it as C++ extension. – Scheff's Cat Jun 04 '19 at 10:34
  • 4
    Use vectors! it will be a bit lesser pain. :) – RC0993 Jun 04 '19 at 10:35
  • 3
    @RC0993 Though `std::vector` is not known for _lesser pain_. ;-) – Scheff's Cat Jun 04 '19 at 10:36
  • I was pointing towards the dynamic arrays. Atleast that would give the OP a start. I am a beginner as well, and getting hands dirty is better now than later. – RC0993 Jun 04 '19 at 10:39
  • `subsetsum(n-1,s-a[k])` cannot work, the function takes 4 parameters. – mch Jun 04 '19 at 10:42
  • Possible duplicate of [C++ printing boolean, what is displayed?](https://stackoverflow.com/questions/15960015/c-printing-boolean-what-is-displayed) – Yksisarvinen Jun 04 '19 at 13:43
  • 1
    In short, if you expected `false` as the output instead of `0`, you should use `std::cout << std::boolalpha << subsetsum(a,n,s);`. `std::boolalpha` is declared in ``. Of course, use this after you fix the error with out-of-bounds array access in `return T[n][s];` – Yksisarvinen Jun 04 '19 at 13:45

1 Answers1

1

array is declared as follows:

bool T[n][s];

and return is done as:

return T[n][s];

Probably, you need to do as follows?

return T[n-1][s-1];

Array indexing in C++ starts from 0.

Rushikesh
  • 163
  • 8