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

Write a C/C++ program that connects to a MySQL server and displays the global TIMEZONE.

4577


i really need help about this.. write a program to display the set of odd and even numbers separately. find the highest and lowest value of the given numbers.

1968


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

3272


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

3125


write a program that reads a series of strings and prints only those strings begging with letter "b"

2674






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

2049


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

2008


Code for Two Classes for Doing Gzip in Memory?

2814


write a program to perform generic sort in arrays?

2625


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+……………...

4351


Code for Easily Using Hash Table?

2390


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

2135


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

2938


Write a simple encryption program using string function which apply the substitution method.

5538


write a program using 2 D that searches a number and display the number of items 12 inputs values input 15,20, 13, 30, 38, 40,16, 18, 20 ,18 ,20 enter no. to search : 20

3366