[go: up one dir, main page]

0% found this document useful (0 votes)
20 views2 pages

Program 8

The document provides a C/C++ program that finds subsets of a given set of positive integers whose sum equals a specified positive integer. It includes a recursive function to explore possible subsets and prints those that meet the criteria. The program prompts the user for input values and displays the valid subsets found.

Uploaded by

vijethviju04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Program 8

The document provides a C/C++ program that finds subsets of a given set of positive integers whose sum equals a specified positive integer. It includes a recursive function to explore possible subsets and prints those that meet the criteria. The program prompts the user for input values and displays the valid subsets found.

Uploaded by

vijethviju04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

8.

Design and implement C/C++ Program to find a subset of a given set S = {s1 , s2,
…..,sn} of n positive integers whose sum is equal to a given positive integer d.

#include<stdio.h>
#define MAX 10
int s[MAX],x[MAX],d;
void sumofsub(int p,int k,int r)
{
int i;
x[k]=1;
if((p+s[k])==d)
{
for(i=1; i<=k; i++)
if(x[i]==1)
printf("%d ",s[i]);
printf("\n");
}
else if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r
-s[k]);
if((p+r
-s[k]>=d) && (p+s[k+1]<=d))
{
x[k]=0;
sumofsub(p,k+1,r
-s[k]);
}
}

int main()
{
int i,n,sum=0;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the set in increasing order:");
for(i=1; i<=n; i++)
scanf("%d",&s[i]);
printf("\nEnter the max subset value:");
scanf("%d",&d);
for(i=1; i<=n; i++)
sum=sum+s[i];
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);
return 0;
}
OUTPUT:

Enter the n value:9

Enter the set in increasing order:1 2 3 4 5 6 7 8 9

Enter the max subset value:9


126
135
18
234
27
36
45
9

You might also like