-2

PS: it's similar to subset sum problem.

thanks

Not Yet Answered # c/c++ code

Narue 5,707 Discussion Starter anaychowdhary Narue 5,707 Discussion Starter anaychowdhary Discussion Starter anaychowdhary NathanOliver 429 Discussion Starter anaychowdhary Discussion Starter anaychowdhary

-2

PS: it's similar to subset sum problem.

thanks

0

We don't give away code so that students can cheat on their homework. If you want to put forth some effort in writing this program you'll find help here, otherwise the thread will be in violation of Daniweb's rules and be either closed or deleted.

0

the code i have written calculates for the sum to be equal to the given number. I have tried to test run and see, what modifications i could make to satisfy the problem statement, could not come across a solution. Also i am an electrical engineering student, i do not have much idea of c/c++.

0

the code i have written

Doesn't exist in our perspective until you post it. Show us what you have, tell us what you've tried, and someone will help you move forward.

0

```
#include<stdio.h>
int count,w[10],d,x[10];
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==d)
{
printf("\n Subset solution = %d\n",++count);
for(i=0; i<=k; i++)
{
if(x[i]==1)
printf("%d\n",w[i]);
}
}
else if(cs+w[k]+w[k+1] <=d)
subset(cs+w[k],k+1,r-w[k]);
if((cs+r-w[k]>=d)&&(cs+w[k+1])<=d)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
int main()
{
int sum=0,i,n;
printf("enter no of elements\n");
scanf("%d",&n);
printf("Enter the elements in ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&w[i]);
printf("Enter the required sum\n");
scanf("%d",&d);
for(i=0; i<n; i++)
sum +=w[i];
if(sum < d)
{
printf("no solution exits\n");
}
printf("The solution is\n");
count =0;
subset(0,0,sum);
return 0;
}
```

I cannot figure out the changes required that will satisfy the problem statement.

My friend helped me out in writing the above code. I am using it for calculating the required load to be shed for power transmission.

-Anay

*Edited 5 Years Ago by Nick Evan*: Added CODE tags

0

```
#include<stdio.h>
int count,w[10],d,x[10];
void subset(int cs,int k,int r)
{
int i;
x[k]=1;
if(cs+w[k]==d)
{
printf("\n Subset solution = %d\n",++count);
for(i=0; i<=k; i++)
{
if(x[i]==1)
printf("%d\n",w[i]);
}
}
else if(cs+w[k]+w[k+1] <=d)
subset(cs+w[k],k+1,r-w[k]);
if((cs+r-w[k]>=d)&&(cs+w[k+1])<=d)
{
x[k]=0;
subset(cs,k+1,r-w[k]);
}
}
int main()
{
int sum=0,i,n;
printf("enter no of elements\n");
scanf("%d",&n);
printf("Enter the elements in ascending order\n");
for(i=0; i<n; i++)
scanf("%d",&w[i]);
printf("Enter the required sum\n");
scanf("%d",&d);
for(i=0; i<n; i++)
sum +=w[i];
if(sum < d)
{
printf("no solution exits\n");
}
printf("The solution is\n");
count =0;
subset(0,0,sum);
return 0;
}
```

I cannot figure out the changes required that will satisfy the problem statement.

My friend helped me out in writing the above code. I am using it for calculating the required load to be shed for power transmission.

-Anay

*Edited 5 Years Ago by Narue*: Added code tags

0

there is a simple approach to this problem. start at the begining of the array and sum all of the elements untill you reach your goal. then you need to store the begining and end posistion of that subset. then you will start at the 2nd element of the array and do the same process. if your result is better than overwrite the posistions and the total. keep doing this untill you run to the end of the array and your total is still not greater than the number you are looking for

0

the approach you mentioned will not get the best subset possible.

consider there are five number 8,11,14,17,80 and the given sum is 89

the output by the approach you mentioned will be 97[17,80] whereas is should be 91[11,80]. So i need to look for the best subset possible.

0

@ NathanOliver yes, the subset does not need to be continuous.

@ arkoenig it is very similar to subset problem, just the condition that the sum can be greater than the given number as well.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...