Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 13 / subsets_iter.cpp
blob: dfad15e509af6ca63535d0fc0559359fb1924863 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
       
bool getIsOne(size_t number, size_t index)
{
	return ((1 << index) & number) != 0;
}

void printSubsetsIter(const int* set, size_t size, size_t bitString)
{
	std::cout << "{ ";

	for (unsigned i = 0; i < size; i++)
	{
		if (getIsOne(bitString, size - 1 - i))
			std::cout << set[i] << " ";
	}

	std::cout << " }" << std::endl;

}

void genAllSubsets(const int* set, size_t size)
{
	size_t count = 1 << size; /// 2^size

	for (size_t i = 0; i < count; i++)
	{
		printSubsetsIter(set, size, i);
	}
}


int main()
{
	const size_t SIZE = 4;
	int arr[SIZE] = { 1, 2, 3, 4 };

	genAllSubsets(arr, 4);
}