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.)



Subsets Write an algorithm that prints out all the subsets of 3 elements of a set of n element..

Answer / 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

More C++ Code Interview Questions

write a program using virtual function to find the transposing of a square matrix?

0 Answers  


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

0 Answers  


Display Pattern: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 &#8230;

2 Answers   Mind Tree,


a program using one dimensional array that searches a number if it is found on the list of given input numbers given by the user and locate its exact location in the list.. ""EXAMPLE"" enter how many numbers to be inputted: 5 12 14 11 09 30 what number to search: 11 11 IS FOUND IN LOCATION 3 PLZZZ.. ELP ME...

3 Answers  


what is the diffrence between ++x , x++ pleaaaaase ???

7 Answers  






What output does the following code generate? Why? What output does it generate if you make A::Foo() a pure virtual function? class A { A() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; class B : public A { B() { this->Foo(); } virtual void Foo() { cout << "A::Foo()" << endl; } }; int main(int, char**) { A objectA; B objectB; return 0; }

0 Answers  


write a program to perform generic sort in arrays?

0 Answers  


Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?

0 Answers   Wipro,


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.

0 Answers  


Write a program using two-dimensional arrays that determines the highest and lowest of the 12 input values. Example: Enter 12 numbers: 13 15 20 13 35 40 16 18 20 18 20 14 highest: 40 lowest: 13

1 Answers  


A suduco given & u hv 2 check if it is incomplete(blanks left),or correct or incorrect

0 Answers  


program to find the magic square using array

1 Answers  


Categories