#include <iostream>
bool isSubsetSum(int arr[], int n, int sum)
{
bool arr[n+1][sum+1];
for(int i=0;i<n+1;++i)
arr[i][0] = true;
for(int i=0;i<sum+1;++i)
arr[0][i] = false;
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
arr[i][j] = arr[i-1][j-arr[i-1]] || arr[i-1][j];
else
arr[i][j] = arr[i-1][j];
}
}
return arr[n][sum];
}
int main()
{
int arr[] = {3, 34, 4, 12, 5, 2};
int sum = 9;
int n = sizeof(arr)/sizeof(arr[0]);
if(isSubsetSum(arr, n,sum) == true)
std::cout << "Found a subset with given sum ";
else
std::cout << "No such subset present";
return 0;
}
i think the problem is in the function, it is taking a parameter named arr and you declared a matrix named arr inside the function body, compiler will give as both the variables are in same scope. try using different name, (suggestion: use more meaningful identifiers). hope this helps.
2 Likes
That made no difference …
Post your new code, please, and also be more specific about precisely what “the error” actually is!
2 Likes
In the function isSubsetSum, you certainly have redeclared array ‘arr’.