SlideShare a Scribd company logo
1 of 3
Download to read offline
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

What's hot

Plot3D Package and Example in R.-Data visualizat,on
Plot3D Package and Example in R.-Data visualizat,onPlot3D Package and Example in R.-Data visualizat,on
Plot3D Package and Example in R.-Data visualizat,onDr. Volkan OBAN
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicalsManoj Chauhan
 
Surface3d in R and rgl package.
Surface3d in R and rgl package.Surface3d in R and rgl package.
Surface3d in R and rgl package.Dr. Volkan OBAN
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..Dr. Volkan OBAN
 
Simulador carrera de caballos desarrollado en C++
Simulador carrera de caballos desarrollado en C++Simulador carrera de caballos desarrollado en C++
Simulador carrera de caballos desarrollado en C++Santiago Sarmiento
 
C2 mate factorización por binomio - 5º
C2 mate   factorización por binomio - 5ºC2 mate   factorización por binomio - 5º
C2 mate factorización por binomio - 5ºbrisagaela29
 
Mate factorización por binomio - 2º
Mate   factorización por binomio - 2ºMate   factorización por binomio - 2º
Mate factorización por binomio - 2ºbrisagaela29
 
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...
Rdio's Alex Gaynor at Heroku's Waza 2013: Why Python, Ruby and Javascript are...Heroku
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsKandarp Tiwari
 
4.5 tan and cot.ppt worked
4.5   tan and cot.ppt worked4.5   tan and cot.ppt worked
4.5 tan and cot.ppt workedJonna Ramsey
 
Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 

What's hot (14)

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

Similar to Coding

Similar to Coding (20)

LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Struct examples
Struct examplesStruct examples
Struct examples
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
array.ppt
array.pptarray.ppt
array.ppt
 
Progr2
Progr2Progr2
Progr2
 
C programms
C programmsC programms
C programms
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programming
C programmingC programming
C programming
 
Pnno
PnnoPnno
Pnno
 
C programs
C programsC programs
C programs
 
programs
programsprograms
programs
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.10 book - Part 33 of 212The Ring programming language version 1.10 book - Part 33 of 212
The Ring programming language version 1.10 book - Part 33 of 212
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 

More from Prasanta Paul

ICard-Prasanta Kumar Paul
ICard-Prasanta Kumar PaulICard-Prasanta Kumar Paul
ICard-Prasanta Kumar PaulPrasanta Paul
 
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...Prasanta Paul
 
Techno India Ramgarh
Techno India RamgarhTechno India Ramgarh
Techno India RamgarhPrasanta Paul
 

More from Prasanta Paul (8)

Aicte-List
Aicte-List Aicte-List
Aicte-List
 
ICard-Prasanta Kumar Paul
ICard-Prasanta Kumar PaulICard-Prasanta Kumar Paul
ICard-Prasanta Kumar Paul
 
Prasanta paul
Prasanta paulPrasanta paul
Prasanta paul
 
Prasanta Kumar Paul
Prasanta Kumar PaulPrasanta Kumar Paul
Prasanta Kumar Paul
 
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
Ppt for paper id 696 a review of hybrid data mining algorithm for big data mi...
 
Techno India Ramgarh
Techno India RamgarhTechno India Ramgarh
Techno India Ramgarh
 
ICardPrasanta
ICardPrasantaICardPrasanta
ICardPrasanta
 
Sl02 2x2 (1)
Sl02 2x2 (1)Sl02 2x2 (1)
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 :