Subsets

Write an algorithm that prints out all the subsets of 3
elements of a set of n elements. The elements of the set are
stored in a list that is the input to the algorithm. (Since
it is a set, you may assume all elements in the list are
distinct.)

Answer Posted / majid - iran

#include<iostream>
#include<conio.h>
#include<time.h>

using namespace std;

int **m;
long q=0;
long num=0;
long number=0;

long fact( long n )
{
return ( n > 0 ? n*fact(n-1) : 1 );
}

long C( long n , long r )
{
return ( fact(n)/(fact(r)*fact(n-r)) );
}

bool isPrinted(int *a, long n)
{
bool is=true;
for(long i=0;i<num;i++)
{
is=true;
for(long j=0;j<n;j++)
{
if(a[j]!=m[i][j])
{
is=false;
}
}
if(is)return true;
}
return false;
}

void print(int *a,long n)
{
int swap=0;
for(long i=0;i<n;i++)
{
for(long j=i;j<n;j++)
{
if(a[j]<a[i])
{
swap=a[i];
a[i]=a[j];
a[j]=swap;
}
}
}
if(!isPrinted(a,n))
{
number++;
cout<<number<<".{";
for(long i=0;i<n;i++)
{
cout<<a[i];
m[q][i]=a[i];
if(i!=n-1)
{
cout<<",";
}
}
q++;
cout<<"}"<<endl;
}
}

void printAll(int *a,int *b,long n,long k)
{
if(q<num)
{
if(k==1)
{
print(a,n);
}
if(k==0)
{
print(a,n);
}
else if(k>0)
{
int *c, *d;
long p=0;
for(long j=0;j<k;j++)
{
p=0;
c=new int[k-1];
for(long t=0;t<k;t++)
{
if(t!=j)
{
c[p]=b[t];
p++;
}
}
d=new int[n];
for(long i=-1;i<n;i++)
{
for(long t=0;t<n;t++)
{
d[t]=a[t];
}
if(i!=-1)
{
d[i]=b[j];
}
printAll(d,c,n,k-1);
}
}
delete c , d;
}
}
}

int main()
{
time_t start , end;
long n = 0 , k = 0;
cout<<"Enter n:";
cin>>n;
cout<<"Enter k:";
cin>>k;
int *a=new int[n];
for(long i=0;i<n;i++)
{
cin>>a[i];
}
int *b=new int[k];
int *c=new int[n-k];
for(long i=0;i<n;i++)
{
if(i<k)
{
b[i]=a[i];
}
else
{
c[n-i-1]=a[i];
}
}
system("cls");
cout<<"S={";
for( long i = 0 ; i < n ; i++ )
{
cout<<a[i];
if( i != n-1 )
cout<<",";
}
cout<<"}"<<endl;
num=C(n,k);
m=new int*[C(n,k)];
for(long i=0;i<C(n,k);i++)
{
m[i]=new int[k];
}
start=clock();
printAll(b,c,k,n-k);
delete a , b , c;
end=clock();
cout<<"All of subsets printed in "<<float(end-
start)/float(CLK_TCK)<<" seconds."<<endl;
getch();
return 0;
}

Is This Answer Correct ?    9 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Ask the user to input three positive integers M, N and q. Make the 2 dimensional array of integers with size MxN, where all the elements of I (I = 1,…,M) line will be members of geometrical progression with first element equal to the number of line (I) and denominator q.

3389


output for printf("printf");

1977


3. Program to find the Sum of give series. a. (1)+(1+2)+(1+2+3)+(1+2+3+4)+……………………………….. b. 1/1+1/9+1/25+1/49+……………...

4349


How can I Draw an ellipse in 3d space and color it by using graph3d?

2130


Given a table of the form: Product Sold on A 1/1/1980 B 1/1/1980 C 1/1/1980 A 1/1/1980 B 1/1/1980 C 2/1/1980 A 2/1/1980 There are 30 products and 10,000 records of such type. Also the month period during which sales happened is given to u. Write the program to display the result as: Product Month No. of copies A January 12 A February 15 A March 27 B January 54 B February 15 B March 10 C January 37

2042






find level of following tree (state, parent) " J,D I,D H,C E,B F,B G,C B,A D,A C,A A,& K,E L,E L,F M,F N,G O,H P,I P,H Q,I R,J S,K U,P T,L

2002


how to diplay a external image of output on winxp by using c & c++,

2930


write a program that creates a sequenced array of numbers starting with 1 and alternately add 1 and then 2 to create the text number in the series , as shown below. 1,33,4,6,7,9,............147,148,150 Then , using a binary search , searches the array 100 times using randomly generated targets in the range of 1 to 150

3262


what mean void creat_object?in public class in this code class A{ public: int x; A(){ cout << endl<< "Constructor A";} ~A(){ cout << endl<< "Destructor A, x is\t"<< x;} }; void create_object(); void main() { A a; a.x=10; { A c; c.x=20; } create_object(); } void create_object() { A b; b.x=30; }

2061


We need to write the function to check the password entered is correct or not based on the following conditions.. a) It must have atleast one lower case character and one digit. b)It must not have any Upper case characters and any special characters c) length should be b/w 5-12. d) It should not have any same immediate patterns like abcanan1 : not acceptable coz of an an pattern abc11se: not acceptable, coz of pattern 11 123sd123 : acceptable, as not immediate pattern adfasdsdf : not acceptable, as no digits Aasdfasd12: not acceptable, as have uppercase character

3949


How to Split Strings with Regex in Managed C++ Applications?

3115


write a program to perform generic sort in arrays?

2619


create a stucture student containing field for roll no,class,year and marks.create 10 student annd store them in a file

2212


write a program that prompt the user to enter his height and weight,then calculate the body mass index and show the algorithm used

4300


Write a program that print in screen a tree with its height taken from user by entering number of 4 digits and find the odd numbers then calculate the sum of odd numbers so he get the height of tree?

2907