SlideShare a Scribd company logo
Question 5.

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. For example, if S={1, 2, 5, 6, 8} and d = 9 there are two solutions
{1,2,6} and {1,8}. A suitable message is to be displayed if the given
problem instance doesn't have a solution.

Source Code--

#include<stdio.h>

#include<conio.h>

#define MAX 10

int s[MAX],x[MAX];

int 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]);
}

        }

void main()

{

int i,n,sum=0;

clrscr();

printf("n Enter max. number : ");

scanf("%d",&n);printf("n Enter the set in increasing order :n");

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

scanf("%d",&s[i]);

printf("n Enter the max. subset value : ");

scanf("%d",&d);

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

sum=sum+s[i];

if (sum < d || s[1] > d)

printf("n No subset possible");

else

sumofsub(0,1,sum);

getch();

}
Output Of this Program :

More Related Content

DOCX
Computer graphics programs in c++
PDF
Frsa
PDF
Program to sort the n names in an alphabetical order
PDF
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
DOCX
Wap in c to draw a line using DDA algorithm
PDF
The simplest existential graph system
Computer graphics programs in c++
Frsa
Program to sort the n names in an alphabetical order
CLIM Undergraduate Workshop: Tutorial on R Software - Huang Huang, Oct 23, 2017
Wap in c to draw a line using DDA algorithm
The simplest existential graph system

What's hot (14)

DOCX
Plot3D Package and Example in R.-Data visualizat,on
PPT
computer graphics practicals
DOCX
Surface3d in R and rgl package.
DOCX
imager package in R and examples..
TXT
Simulador carrera de caballos desarrollado en C++
PPTX
C2 mate factorización por binomio - 5º
PPTX
Mate factorización por binomio - 2º
PDF
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
PPTX
PPT
Module 2 topic 2 notes
DOCX
Computer Graphics Lab File C Programs
DOCX
Graphics point clipping c program
PPT
4.5 tan and cot.ppt worked
DOCX
Cg my own programs
Plot3D Package and Example in R.-Data visualizat,on
computer graphics practicals
Surface3d in R and rgl package.
imager package in R and examples..
Simulador carrera de caballos desarrollado en C++
C2 mate factorización por binomio - 5º
Mate factorización por binomio - 2º
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Module 2 topic 2 notes
Computer Graphics Lab File C Programs
Graphics point clipping c program
4.5 tan and cot.ppt worked
Cg my own programs
Ad

Similar to Coding (20)

PPTX
LAB PROGRAMS SARASWATHI RAMALINGAM
DOCX
C lab manaual
DOCX
Solutionsfor co2 C Programs for data structures
PDF
VTU Data Structures Lab Manual
DOCX
ADA FILE
DOCX
PPT
Struct examples
PDF
LET US C (5th EDITION) CHAPTER 2 ANSWERS
PPT
array.ppt
PDF
Progr2
PDF
C programms
PPT
All important c programby makhan kumbhkar
PPTX
C programming
DOCX
PDF
programs
PDF
Assignment on Numerical Method C Code
DOC
Basic c programs updated on 31.8.2020
PDF
The Ring programming language version 1.10 book - Part 33 of 212
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
LAB PROGRAMS SARASWATHI RAMALINGAM
C lab manaual
Solutionsfor co2 C Programs for data structures
VTU Data Structures Lab Manual
ADA FILE
Struct examples
LET US C (5th EDITION) CHAPTER 2 ANSWERS
array.ppt
Progr2
C programms
All important c programby makhan kumbhkar
C programming
programs
Assignment on Numerical Method C Code
Basic c programs updated on 31.8.2020
The Ring programming language version 1.10 book - Part 33 of 212
Let us C (by yashvant Kanetkar) chapter 3 Solution
Ad

More from Prasanta Paul (8)

PDF
Aicte-List
PDF
ICard-Prasanta Kumar Paul
PDF
Prasanta paul
PDF
Prasanta Kumar Paul
PPT
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
PPTX
Techno India Ramgarh
PDF
ICardPrasanta
PDF
Sl02 2x2 (1)
Aicte-List
ICard-Prasanta Kumar Paul
Prasanta paul
Prasanta Kumar Paul
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
Techno India Ramgarh
ICardPrasanta
Sl02 2x2 (1)

Coding

  • 1. Question 5. 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. For example, if S={1, 2, 5, 6, 8} and d = 9 there are two solutions {1,2,6} and {1,8}. A suitable message is to be displayed if the given problem instance doesn't have a solution. Source Code-- #include<stdio.h> #include<conio.h> #define MAX 10 int s[MAX],x[MAX]; int 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]);
  • 2. } } void main() { int i,n,sum=0; clrscr(); printf("n Enter max. number : "); scanf("%d",&n);printf("n Enter the set in increasing order :n"); for(i=1;i<=n;i++) scanf("%d",&s[i]); printf("n Enter the max. subset value : "); scanf("%d",&d); for(i=1;i<=n;i++) sum=sum+s[i]; if (sum < d || s[1] > d) printf("n No subset possible"); else sumofsub(0,1,sum); getch(); }
  • 3. Output Of this Program :