[go: up one dir, main page]

0% found this document useful (0 votes)
9 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 d. It includes the main function for input and a recursive function to explore possible subsets. Sample input and output demonstrate the program's functionality, showing subsets that sum to the given value d.

Uploaded by

abhinehalove71
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)
9 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 d. It includes the main function for input and a recursive function to explore possible subsets. Sample input and output demonstrate the program's functionality, showing subsets that sum to the given value d.

Uploaded by

abhinehalove71
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

Program 8

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

#include<stdio.h>

int s[10], x[10], d;


void sumofsub(int,int,int);

void main()
{
int n, sum=0, i;
printf("\n Enter the size of the set:");
scanf("%d",&n);
printf("Enter the set in increasing order:\n");
for(i=1; i<=n; i++)
scanf("%d",&s[i]);
printf("Enter the value of d:\n");
scanf("%d",&d);

for(i=1; i<=n; i++)


sum=sum+s[i];

if(sum<d || s[1]>d)
printf("No subset possible");
else
sumofsub(0,1,sum);
}

void sumofsub(int m, int k, int r)


{
int i=1; x[k]=1;

if((m+s[k])==d)
{
printf("Subset:");
for(i=1; i<=k; i++)
if(x[i]==1)
printf("\t%d",s[i]);
printf("\n");
}
else
if(m+ s[k]+ s[k+1]<=d)
sumofsub(m+s[k], k+1, r-s[k]);

if((m+ r- s[k] >= d)&&(m+ s[k+1] <= d))


{
x[k]=0;
sumofsub(m, k+1, r-s[k]);
}
}
Sample Input and Output:
Enter the size of the set:5
Enter the set in increasing order:
12345
Enter the value of d:
6
Subset:1 2 3
Subset:1 5
Subset:2 4

You might also like