summing numbers recursive C++ - c++

I'm trying to make a recursive program that sums an array or a list of numbers.
Using visual studio 2013, C++ console application.
My 1st question is:
Now I know how many numbers I have and I know the size of my array. How can I program it the way that don't know the numbers in advance, like while it's calculating the numbers there are still new numbers adding up, with the least space usage?
My 2nd question is that:
How can i improve the program that still works recursively and its time and space usage be optimal?
Here is my code:
// summing a list of number.cpp
#include "stdafx.h"
#include "iostream"
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0, i = 0;
int sumarray(int i){
if (i < 9){
sum += array[i];
i++;
sumarray(i);
}
else
return sum;
}
int main(){
std::cout << "sum is ::: " << sumarray(i);
getchar();
}

I hope you'll stop writing functions that depend on global variables to work when they can be easily made to work only with the input they have been provided.
Here's a version that works for me.
#include <iostream>
int sumarray(int array[], int i)
{
if ( i <= 0 )
{
return 0;
}
return sumarray(array, i-1) + array[i-1];
}
int main()
{
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::cout << "sum is : " << sumarray(array, 0) << std::endl;
std::cout << "sum is : " << sumarray(array, 5) << std::endl;
std::cout << "sum is : " << sumarray(array, 10) << std::endl;
}
Output:
sum is : 0
sum is : 15
sum is : 55

If i >= 9, your function does a return sum;.
(that is fine and good)
Where does your function return if i < 9???
if (i < 9){
sum += array[i];
i++;
sumarray(i); // I see no return statement here!!
}
Basically, if you call sumarray(3), there is no return statement that gets hit.
In your program, there is a global variable called i.
There is also a local parameter to the function also called i.
The local variable shadows the global variable, so there is no clear purpose to the global i.
I'd do it like this:
int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Pass in the current index, and the size of the array
int sumarray(int i, int sz)
{
if (sz == 0)
{
return 0;
}
return array[i] + sumarray(i+1, sz-1);
}
int main(){
std::cout << "sum is ::: " << sumarray(0, 10);
// Start at the beginning (index 0)
// Run for 10 elements.
getchar();
}
The first recursive call will be to sumarray(1,9); then to sumarray(2,8);... when finally sumarray(10,0) is called, it will return 0.

A function to sum the elements of an array would normally accept the array as an argument. In that case, as a practical matter it must also accept the size of the array. Something like this:
int sumarray(int a[], size_t size) {
A signature like that furthermore gives you access to better recursive approaches. In particular, you could recursively compute the sum of the first and second halves of the array, and return their sum:
size_t midpoint = size / 2;
return sumarray(a, midpoint) + summaray(a + midpoint, size - midpoint);
That's not a complete solution: you need a termination condition (when size is less than 2). Putting that in and finishing off the function are left as an exercise for you, since you'll learn better if you have to put some work into it yourself.
That approach limits the recursion depth and thus stack size (memory overhead) to be proportional to the logarithm of the array size, though it still involves total numbers of function calls and integer additions proportional to the array size. I don't think you can achieve better asymptotic space or time complexity with a recursive algorithm. (A non-recursive algorithm for this task requires only a fixed number of function calls and and a fixed amount of memory overhead, however.)

here is a working C++ code in Qt, which i wrote - Good Luck
I added some debug points outputs to make its understanding clearer
#include <QCoreApplication>
#include <QDebug>
int sum=0;
int sumrec(int *array,int n)
{
if (n>=0)
{
int element=*(array+n); // note *(array+n) -> moving the pointer
// *array+n -> this is adding n to the pointer data (wrong)
// what is array ?
qDebug() << " element value " << *(array+n) << " at n=" << n << " array address = " << array;
n--;
sum=sum+element;
qDebug() << "sum = " << sum;
sumrec(array,n);
return sum;
}
else
{
return 0;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int A[10]={12,13,14,15,16,17,18,19,20,11};
int b=sumrec(&A[0],9);
qDebug() << "answer = " << b;
//return a.exec();
}
here is the output of the terminal
element value 11 at n= 9 array address = 0x7fff5fbffb78
sum = 11
element value 20 at n= 8 array address = 0x7fff5fbffb78
sum = 31
element value 19 at n= 7 array address = 0x7fff5fbffb78
sum = 50
element value 18 at n= 6 array address = 0x7fff5fbffb78
sum = 68
element value 17 at n= 5 array address = 0x7fff5fbffb78
sum = 85
element value 16 at n= 4 array address = 0x7fff5fbffb78
sum = 101
element value 15 at n= 3 array address = 0x7fff5fbffb78
sum = 116
element value 14 at n= 2 array address = 0x7fff5fbffb78
sum = 130
element value 13 at n= 1 array address = 0x7fff5fbffb78
sum = 143
element value 12 at n= 0 array address = 0x7fff5fbffb78
sum = 155
answer = 155

In C++ you have all the tools to do that in a very simple, readable and safe way. Check out the valarray container:
#include <iostream>
#include <valarray>
int main () {
std::valarray<int> array{1,2,3,4,5,6,7,8,9,10};
std::cout << array.sum() << '\n';
return 0;
}

Related

Math - function to return index adding previously skipped indexes

I was trying to find a way to skip some indexes and add the number of skipped indexes to the index.
for example,
I want to skip all the index divisible by 4, and add those skipped indexes as well.
i.e, if index = 4, 4 mod 4 == 0, so index becomes 5.
if index = 8, 8 mod 4 == 0, but index becomes 10, because we already had to skip 4.
likewise, if index = 16, 16 mod 4 == 0, but index becomes 21. Because 16 + 4 = 20, which again is a multiple of 4, so we skip that and goes to next index, 21.
okay, let me try to explain it through a code snippet i was trying it on.
int fn(int i) {
if (i <= 0) {
return 0;
}
if (i % 4 == 0) {
return fn(i / 4) + (i / 4);
} else {
return i / 4;
}
}
for (int i = 0; i < 100; i++) {
int bl = fn(i);
cout << "block:" << i << endl << "Translated:" << bl << endl;
}
But as you can see its not working properly. Any ideas?
Every block of four consecutive numbers—like 1, 2, 3, 4 or 5, 6, 7, 8—contains three usable indices and one that should be skipped. So all we need to do is:
Divide by three to get the number of blocks needed.
Multiply that number by four to switch from a number of usable indices to a number of original indices.
Add the remainder modulo three to account for position within the last block.
Adjust because we apparently want to start with index 1. Thus adjustment is to subtract one (to convert to zero-based indices), do the calculation described above, and then add one (to convert back to one-based indices).
This program:
#include <stdio.h>
int fn(int i)
{
return (i-1)/3*4 + (i-1)%3 + 1;
}
int main(void)
{
for (int i = 1; i < 17; ++i)
printf("%d -> %d.\n", i, fn(i));
}
produces this output:
1 -> 1.
2 -> 2.
3 -> 3.
4 -> 5.
5 -> 6.
6 -> 7.
7 -> 9.
8 -> 10.
9 -> 11.
10 -> 13.
11 -> 14.
12 -> 15.
13 -> 17.
14 -> 18.
15 -> 19.
16 -> 21.
I'm assuming fn is supposed to return the offset to the normal index. If this is true, your code is actually correct - you're just printing the "translated" value wrong. bl will contain the offset; this has to be added to i in order to get the actual index.
std::cout << "block:" << i << std::endl << "Translated:" << i + bl << std::endl;
Demo
This function is better solved through logic than recursion. We're skipping every third index; ergo, we need only add 1 to the index after every 3 steps.
#include <iostream>
int fn(int i) {
if (i <= 0) {
return 0;
}
int offset = (i-1)/3;
return offset;
}
int main() {
for (int i = 1; i <= 13; i++) {
int bl = fn(i);
std::cout << "block:" << i << std::endl << "Translated:" << i + bl << std::endl;
}
}
Demo

Divide elements of a sorted array into least number of groups such that difference between the elements of the new array is less than or equal to 1

How to divide elements in an array into a minimum number of arrays such that the difference between the values of elements of each of the formed arrays does not differ by more than 1?
Let's say that we have an array: [4, 6, 8, 9, 10, 11, 14, 16, 17].
The array elements are sorted.
I want to divide the elements of the array into a minimum number of array(s) such that each of the elements in the resulting arrays do not differ by more than 1.
In this case, the groupings would be: [4], [6], [8, 9, 10, 11], [14], [16, 17]. So there would be a total of 5 groups.
How can I write a program for the same? Or you can suggest algorithms as well.
I tried the naive approach:
Obtain the difference between consecutive elements of the array and if the difference is less than (or equal to) 1, I add those elements to a new vector. However this method is very unoptimized and straight up fails to show any results for a large number of inputs.
Actual code implementation:
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int main() {
int num = 0, buff = 0, min_groups = 1; // min_groups should start from 1 to take into account the grouping of the starting array element(s)
cout << "Enter the number of elements in the array: " << endl;
cin >> num;
vector<int> ungrouped;
cout << "Please enter the elements of the array: " << endl;
for (int i = 0; i < num; i++)
{
cin >> buff;
ungrouped.push_back(buff);
}
for (int i = 1; i < ungrouped.size(); i++)
{
if ((ungrouped[i] - ungrouped[i - 1]) > 1)
{
min_groups++;
}
}
cout << "The elements of entered vector can be split into " << min_groups << " groups." << endl;
return 0;
}
Inspired by Faruk's answer, if the values are constrained to be distinct integers, there is a possibly sublinear method.
Indeed, if the difference between two values equals the difference between their indexes, they are guaranteed to belong to the same group and there is no need to look at the intermediate values.
You have to organize a recursive traversal of the array, in preorder. Before subdividing a subarray, you compare the difference of indexes of the first and last element to the difference of values, and only subdivide in case of a mismatch. As you work in preorder, this will allow you to emit pieces of the groups in consecutive order, as well as detect to the gaps. Some care has to be taken to merge the pieces of the groups.
The worst case will remain linear, because the recursive traversal can degenerate to a linear traversal (but not worse than that). The best case can be better. In particular, if the array holds a single group, it will be found in time O(1). If I am right, for every group of length between 2^n and 2^(n+1), you will spare at least 2^(n-1) tests. (In fact, it should be possible to estimate an output-sensitive complexity, equal to the array length minus a fraction of the lengths of all groups, or similar.)
Alternatively, you can work in a non-recursive way, by means of exponential search: from the beginning of a group, you start with a unit step and double the step every time, until you detect a gap (difference in values too large); then you restart with a unit step. Here again, for large groups you will skip a significant number of elements. Anyway, the best case can only be O(Log(N)).
I would suggest encoding subsets into an offset array defined as follows:
Elements for set #i are defined for indices j such that offset[i] <= j < offset[i+1]
The number of subsets is offset.size() - 1
This only requires one memory allocation.
Here is a complete implementation:
#include <cassert>
#include <iostream>
#include <vector>
std::vector<std::size_t> split(const std::vector<int>& to_split, const int max_dist = 1)
{
const std::size_t to_split_size = to_split.size();
std::vector<std::size_t> offset(to_split_size + 1);
offset[0] = 0;
size_t offset_idx = 1;
for (std::size_t i = 1; i < to_split_size; i++)
{
const int dist = to_split[i] - to_split[i - 1];
assert(dist >= 0); // we assumed sorted input
if (dist > max_dist)
{
offset[offset_idx] = i;
++offset_idx;
}
}
offset[offset_idx] = to_split_size;
offset.resize(offset_idx + 1);
return offset;
}
void print_partition(const std::vector<int>& to_split, const std::vector<std::size_t>& offset)
{
const std::size_t offset_size = offset.size();
std::cout << "\nwe found " << offset_size-1 << " sets";
for (std::size_t i = 0; i + 1 < offset_size; i++)
{
std::cout << "\n";
for (std::size_t j = offset[i]; j < offset[i + 1]; j++)
{
std::cout << to_split[j] << " ";
}
}
}
int main()
{
std::vector<int> to_split{4, 6, 8, 9, 10, 11, 14, 16, 17};
std::vector<std::size_t> offset = split(to_split);
print_partition(to_split, offset);
}
which prints:
we found 5 sets
4
6
8 9 10 11
14
16 17
Iterate through the array. Whenever the difference between 2 consecutive element is greater than 1, add 1 to your answer variable.
`
int getPartitionNumber(int arr[]) {
//let n be the size of the array;
int result = 1;
for(int i=1; i<n; i++) {
if(arr[i]-arr[i-1] > 1) result++;
}
return result;
}
`
And because it is always nice to see more ideas and select the one that suites you best, here the straight forward 6 line solution. Yes, it is also O(n). But I am not sure, if the overhead for other methods makes it faster.
Please see:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
using Data = std::vector<int>;
using Partition = std::vector<Data>;
Data testData{ 4, 6, 8, 9, 10, 11, 14, 16, 17 };
int main(void)
{
// This is the resulting vector of vectors with the partitions
std::vector<std::vector<int>> partition{};
// Iterating over source values
for (Data::iterator i = testData.begin(); i != testData.end(); ++i) {
// Check,if we need to add a new partition
// Either, at the beginning or if diff > 1
// No underflow, becuase of boolean shortcut evaluation
if ((i == testData.begin()) || ((*i) - (*(i-1)) > 1)) {
// Create a new partition
partition.emplace_back(Data());
}
// And, store the value in the current partition
partition.back().push_back(*i);
}
// Debug output: Copy all data to std::cout
std::for_each(partition.begin(), partition.end(), [](const Data& d) {std::copy(d.begin(), d.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; });
return 0;
}
Maybe this could be a solution . . .
How do you say your approach is not optimized? If your is correct, then according to your approach, it takes O(n) time complexity.
But you can use binary-search here which can optimize in average case. But in worst case this binary search can take more than O(n) time complexity.
Here's a tips,
As the array sorted so you will pick such a position whose difference is at most 1.
Binary search can do this in simple way.
int arr[] = [4, 6, 8, 9, 10, 11, 14, 16, 17];
int st = 0, ed = n-1; // n = size of the array.
int partitions = 0;
while(st <= ed) {
int low = st, high = n-1;
int pos = low;
while(low <= high) {
int mid = (low + high)/2;
if((arr[mid] - arr[st]) <= 1) {
pos = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
partitions++;
st = pos + 1;
}
cout<< partitions <<endl;
In average case, it is better than O(n). But in worst case (where the answer would be equal to n) it takes O(nlog(n)) time.

Trying to copy elements from one array to another using only pointers

My professor wants me to only use pointers, no subscripts are permitted in any function
I'm suppose to let the user input two size and elements into two separate arrays and then make a union function which finds all elements that are the same (no duplicate values in either array)
All I want to do is move my array elements from the first array to the union array but I just get random numbers when I execute the program
void get_union(short *set1,short size1,short *set2,short size2,short *union_array,short size_union) // look at call statement to assist incompleting this statement
{
short *end1=(set1+size1), //*end2=(set1+size1+size2);
for( ;set1<end1;set1++)
{
union_array=set1;
cout<<"Union array value number "<<count++<<" "<<*union_array++<<endl;
}
}
and also I tried memcpy but I have never used it before especially not with pointers
memcpy(union_array,set1,sizeof(union_array));
Here's my first two functions that lets the user input data, and then displays it.
void inputData(short *data, short size) // function to enter data into the array
{
short count=1;
short *end=(data+size);
for( ;data<end;data++)
{
cout<<"Enter Number "<<count++<<" : ";
cin>>*data;
cout<<endl;
}
}
and
void displayData(short *data, short size) // function to display data in an array
{
short count;
short *end=(data+size);
for( ;data<end;data++)
{
cout<<"Number "<<count++<<" : "<<*data<<endl;
}
}
This is what I get when I run the program. The whole thing runs through but the union array gets random numbers
enter the number of values to store in the data set 1
or zero to terminate the program
3
Enter Number 1 : 2
Enter Number 2 : 4
Enter Number 3 : 6
there are 3 values in the array set1
Number 0 : 2
Number 1 : 4
Number 2 : 6
enter the number of values to store in the data set 2
3
Enter Number 1 : 10
Enter Number 2 : 11
Enter Number 3 : 12
there are 3 values in the array set2
Number 0 : 10
Number 1 : 11
Number 2 : 12
Union array value number 1 -5245
the union array contains 0 values
the intersection array contains -1 values
I'm not sure about your question, but let me try to help you:
All I want to do is move my array elements from the first array to the union array but I just get random numbers when I execute the program
using namespace std;
void get_union(short *set1,short size1,short *union_array,short size_union) {
for (; size1; --size1) {
*union_array++ = *set1++;
}
}
int main () {
short set1[] = {1, 0, 15, 35, 200, 12};
size_t size = sizeof(set1) / sizeof(*set1);
short union_arr[size];
get_union(set1, size, union_arr, size);
for (size_t i = 0; i < size; i++) {
cout << union_arr[i] << " ";
}
cout << endl;
return 0;
}
make a union function which finds all elements that are the same (no duplicate values in either array)
size_t get_union(short *set1, unsigned size1, short *set2, unsigned size2, short *union_array, unsigned size_union) {
size_t count = 0;
// TODO:: Make sure that the arrays are already sort
// todo:: or use the following two commands:
qsort(set1, size1, sizeof(*set1), [](const void * a, const void * b) -> int{
return ( *(short*)a - *(short*)b );
});
qsort(set2, size2, sizeof(*set2), [](const void * a, const void * b) -> int{
return ( *(short*)a - *(short*)b );
});
while (size1 && size2) {
if (*set1 > *set2) {
++set2;
--size2;
} else if (*set1 < *set2) {
++set1;
--size1;
} else {
*union_array++ = *set1++;
--size1;
++set2;
--size2;
++count;
}
}
return count;
}
int main () {
short set1[] = {1, 0, 15, 35, 200, 12};
short set2[] = {50, 0, 15, 0, 200, 12};
size_t size = sizeof(set1) / sizeof(*set1);
short union_arr[size];
size_t count;
count = get_union(set1, size, set2, size, union_arr, size);
cout << count << endl;
for (size_t i = 0; i < count; i++) {
cout << union_arr[i] << " ";
}
cout << endl;
return 0;
}
Btw, it is C question more then C++ one.. In C++ you can simply use vectors, and make the code as simple as you can (and the union array will be axactly with the minimum size). Pointers in C++ are more relevant in different situations, when you dealing with speed situations and APIs..
What he wants from us it to write a function that compares two arrays (set1 and set2 that I had) and puts all numbers that occur in either. so if there are 0 elements in the first array and 5 in the other, then the union array should have 5 elements
size_t get_unique_union(short *arr, size_t size, short *target) {
size_t target_bigger = 0;
short *curr, *curr_test;
//size_t dup = 0; (1)
if (!size) {
return target_bigger;
}
curr = arr + 1; // Current place in the array.
curr_test = curr; // Current place with the offset of the duplicate elements.
while (curr_test < arr + size) {
while (curr_test < arr + size && *arr == *curr_test) {
curr_test++;
//dup++; // | (1) Update global size. see comment.
}
*curr++ = *curr_test++;
}
size -= curr_test - curr; // Update global size | (1) size -= dup;
if (curr == curr_test) { // If it is a unique appearance (If there were no founded duplicates).
*target = *arr; // Set target in the current place the appearance.
target_bigger = 1; // Mention that in the next recursive calling,
// it will be called from the next place in the unique array.
}
for (size_t i = 0; i < size; i++) { // Display the current checked array (Without the doubles of the current appearance).
cout << arr[i] << " ";
}
cout << endl;
return target_bigger + get_unique_union(arr + 1, size - 1, target + target_bigger); // Recursive call with the next element in the array.
}
size_t get_union(short *set1, unsigned size1, short *set2, unsigned size2, short *uniq_arr) {
size_t global_size = size1 + size2;
size_t uniq_size = 0;
short union_array[global_size];
for (size_t i = 0; i < size1; i++) {
union_array[i] = set1[i];
}
for (size_t i = 0; i < size2; i++) {
union_array[i + size1] = set2[i];
}
for (size_t i = 0; i < global_size; i++) {
cout << union_array[i] << " ";
}
cout << endl;
return get_unique_union(union_array, global_size, uniq_arr);
}
int main () {
short set1[] = {12, 0, 2, 1, 12, 12, 6, 8};
short set2[] = {3, 0, 300, 12, 12, 12};
size_t size1 = sizeof(set1) / sizeof(*set1);
size_t size2 = sizeof(set2) / sizeof(*set2);
short union_arr[size1 + size2];
size_t count;
count = get_union(set1, size1, set2, size2, union_arr);
cout << "Results:" << endl;
cout << "Count: " << count << endl;
cout << "Arr: [";
for (size_t i = 0; i < count; i++) {
cout << union_arr[i] << ((i < count - 1) ? ", " : "");
}
cout << "]" << endl;
return 0;
}
This code even take care of cases that there are duplicates in the same array. It combines the arrays to the same big array, remove duplicates, and insert only the elements that have no duplicates at all.
Pay attention that the function "get_unique_union" is recursive, and returns the count of the unique elements, and not the unique union array's real size.
The code format is C one, and not C++ (it works on C++, has been written in cpp file and compiled with g++ compiler. But in C++ it would be write with vectors, and it would save more useless place in the unique union array).

Strange bug in a while loop (C++)

I'm trying to make a program that works with a simple algorithm.
But for some reason, I get a strange bug (below is the simplified version of the program).
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::string;
void find(int arr[], string name)
{
int t = 8;
int i = 0;
int v = 0;
// t should become equal to the smallest int of the array after this.
while (arr[i])
{
if (arr[i] < t)
{
t = arr[i];
}
++i;
}
/* When this statement below gets executed t gets what looks like a
random value for some reason */
cout << arr[t] << '\n';
for (int b = 0; b < 2; ++b)
{
if (t == arr[b])
{
v = b;
}
}
/* Again, arr[v] gets what looks like a random number */
cout << "The cheapest " << name << " is number " << arr[v] << ".";
}
int main()
{
/* [0] = "Cost for Steve"
[1] = "Cost for Mark"
[2] = "Cost for Andrew" */
int cleaning[] = { 5, 4, 7 };
int cooking[] = { 3, 6, 4 };
int babysitting[] = { 7, 6, 3 };
cout << "Number 1: Steve, Number 2: Mark, Number 3: Andrew.\n";
find(cleaning, "cleaner");
find(cooking, "cook");
find(babysitting, "babysitter");
/* This is to prevent the console application from quitting */
while (true)
{
}
}
I'm sure there is something wrong in the for and the while loop, but what?
If you're reading my code and some text or variable name seems foreign to you, chances are that I forgot to translate it (this is originally written in Italian).
Thanks for taking your time to read this.
EDIT: Thanks to #Tar I fixed the first part of the program, but the part which says The (name) that costs less is n. (arr[v]).still gives me a random number, I compiled and ran the program, the output is:
Number 1: Steve, Number 2: Mark, Number 3: Andrew.
4
The cheapest cleaner is number 4.
3
The cheapest cook is number 3.
3
The cheapest babysitter is number 7.
That is obviously wrong as it should say that the cheapest cleaner is number 2, the cheapest cook is number 1 and the cheapest babysitter is number 3.
PS: As soon as everything is fixed I will take the part which prints the cheapest price out.
The problem is within your first while loop in find:
while (arr[i]) // while the element at arr[i] is NOT 0
{
if (arr[i] < t)
{
t = arr[i];
}
i++;
}
Here you continuously evaluate elements in arr for whether they are not 0. This is not correct. You've declared your arrays as:
int cleaning[3] = { 5, 4, 7 };
int cooking[3] = { 3, 6, 4 };
int babysitting[3] = { 7, 6, 3 };
None of these contain 0, so your while loop will run indefinitely and you'll be reading past the memory for each array which is not good news.
Consider using std::vector instead, and see how much clearer and safer your code becomes:
#include <vector>
#include <iostream>
void find(const std::vector<int>& numbers)
{
auto t = 8;
// Look through each element in the container:
for(auto number : numbers)
{
if (number < t)
{
t = number;
}
}
std::cout << t << std::endl;
}
int main()
{
std::vector<int> cleaning = {5, 4, 7};
find(cleaning);
}
Above all, I want to make a statement: I am not an English-speaker, so if I said wrong words, please excuse me.
I think this question is not very difficult. I fixed your algorithm and output format. Actually, I almost rewrote it.
In my view, your code seems kind of naive. If you only learnt C++ syntax, there is a long way to study algorithm.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int find(const vector<int>& numbers) {
int minVul = numbers[0];
int rank = 0;
for (int i = 1; i < numbers.size(); i++)
{
if (minVul > numbers[i])
{
minVul = numbers[i];
rank = i;
}
}
return rank;
}
int main() {
vector<string> name = { "steve","mark","andrew" };
/* [0] = "Cost for steve"
[1] = "Cost for mark"
[2] = "Cost for andrew" */
vector<int> cleaning = { 5, 4, 7 };
vector<int> cooking = { 3, 6, 4 };
vector<int> babysitting = { 7, 6, 3 };
int cleaner = find(cleaning);
cout << "Cleaning:" << name[cleaner] << " costs least in " << cleaning[cleaner] << endl;
int cooker = find(cooking);
cout << "Cooking:" << name[cooker] << " costs least in " << cooking[cooker] << endl;
int babysitter = find(babysitting);
cout << "Babysitter:" << name[babysitter] << " costs least in " << babysitting[babysitter] << endl;
system("pause"); //This is a common skill to prevent the console application from quitting.
return 0;
}
Outputs:
Cleaning:mark costs least in 4
Cooking:steve costs least in 3
Babysitter:andrew costs least in 3

Power set of large set

I have to calculate power set of set which may have more elements upto 10^5. I tried an algo and the code below but it failed (I think cause large value of pow(2, size)).
void printPowerSet(int *set, int set_size)
{
unsigned int pow_set_size = pow(2, set_size);
int counter, j,sum=0;
for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
if(counter & (1<<j))
std::cout<<set[i]<<" ";
}
std::cout<<sum;
sum=0;
printf("\n");
}
}
Is there any other algorithm or how can I fix this one (if it is possible)??
OR
Can you suggest me how to do it i.e. finding subset of large set.
As pointed out in an answer it seems I'm stuck in
X-Y problem. Basically, I need sum of all subsets of any set. Now if you can suggest me any other approach to solve the problem.
Thank you.
Here is an algorithm which will print out the power set of any set that will fit in your computer's memory.
Given enough time, it will print the power set of a set of length 10^5.
However, "enough time" will be something like several trillion billion gazillion years.
c++14
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#include <iostream>
void printPowerset (const vector<int>& original_set)
{
auto print_set = [&original_set](auto first, auto last) -> ostream&
{
cout << '(';
auto sep = "";
for ( ; first != last ; ++first, sep = ",")
{
cout << sep << original_set[(*first) - 1];
}
return cout << ')';
};
const int n = original_set.size();
std::vector<int> index_stack(n + 1, 0);
int k = 0;
while(1){
if (index_stack[k]<n){
index_stack[k+1] = index_stack[k] + 1;
k++;
}
else{
index_stack[k-1]++;
k--;
}
if (k==0)
break;
print_set(begin(index_stack) + 1, begin(index_stack) + 1 + k);
}
print_set(begin(index_stack), begin(index_stack)) << endl;
}
int main(){
auto nums = vector<int> { 2, 4, 6, 8 };
printPowerset(nums);
nums = vector<int> { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
printPowerset(nums);
return 0;
}
expected results:
first power set (4 items):
(2)(2,4)(2,4,6)(2,4,6,8)(2,4,8)(2,6)(2,6,8)(2,8)(4)(4,6)(4,6,8)(4,8)(6)(6,8)(8)()
second power set (10 items)
(2)(2,4)(2,4,6)(2,4,6,8)(2,4,6,8,10)(2,4,6,8,10,12)(2,4,6,8,10,12,14)(2,4,6,8,10,12,14,16)(2,4,6,8,10,12,14,16,18)(2,4,6,8,10,12,14,16,18,20)(2,4,6,8,10,12,14,16,20)(2,4,6,8,10,12,14,18)(2,4,6,8,10,12,14,18,20)(2,4,6,8,10,12,14,20)(2,4,6,8,10,12,16)(2,4,6,8,10,12,16,18)(2,4,6,8,10,12,16,18,20)(2,4,6,8,10,12,16,20)(2,4,6,8,10,12,18)(2,4,6,8,10,12,18,20)(2,4,6,8,10,12,20)(2,4,6,8,10,14)(2,4,6,8,10,14,16)(2,4,6,8,10,14,16,18)(2,4,6,8,10,14,16,18,20)(2,4,6,8,10,14,16,20)(2,4,6,8,10,14,18)(2,4,6,8,10,14,18,20)(2,4,6,8,10,14,20)(2,4,6,8,10,16)(2,4,6,8,10,16,18)(2,4,6,8,10,16,18,20)(2,4,6,8,10,16,20)(2,4,6,8,10,18)(2,4,6,8,10,18,20)(2,4,6,8,10,20)(2,4,6,8,12)(2,4,6,8,12,14)(2,4,6,8,12,14,16)(2,4,6,8,12,14,16,18)(2,4,6,8,12,14,16,18,20)(2,4,6,8,12,14,16,20)(2,4,6,8,12,14,18)(2,4,6,8,12,14,18,20)(2,4,6,8,12,14,20)(2,4,6,8,12,16)(2,4,6,8,12,16,18)(2,4,6,8,12,16,18,20)(2,4,6,8,12,16,20)(2,4,6,8,12,18)(2,4,6,8,12,18,20)(2,4,6,8,12,20)(2,4,6,8,14)(2,4,6,8,14,16)(2,4,6,8,14,16,18)(2,4,6,8,14,16,18,20)(2,4,6,8,14,16,20)(2,4,6,8,14,18)(2,4,6,8,14,18,20)(2,4,6,8,14,20)(2,4,6,8,16)(2,4,6,8,16,18)(2,4,6,8,16,18,20)(2,4,6,8,16,20)(2,4,6,8,18)(2,4,6,8,18,20)(2,4,6,8,20)(2,4,6,10)(2,4,6,10,12)(2,4,6,10,12,14)(2,4,6,10,12,14,16)(2,4,6,10,12,14,16,18)(2,4,6,10,12,14,16,18,20)(2,4,6,10,12,14,16,20)(2,4,6,10,12,14,18)(2,4,6,10,12,14,18,20)(2,4,6,10,12,14,20)(2,4,6,10,12,16)(2,4,6,10,12,16,18)(2,4,6,10,12,16,18,20)(2,4,6,10,12,16,20)(2,4,6,10,12,18)(2,4,6,10,12,18,20)(2,4,6,10,12,20)(2,4,6,10,14)(2,4,6,10,14,16)(2,4,6,10,14,16,18)(2,4,6,10,14,16,18,20)(2,4,6,10,14,16,20)(2,4,6,10,14,18)(2,4,6,10,14,18,20)(2,4,6,10,14,20)(2,4,6,10,16)(2,4,6,10,16,18)(2,4,6,10,16,18,20)(2,4,6,10,16,20)(2,4,6,10,18)(2,4,6,10,18,20)(2,4,6,10,20)(2,4,6,12)(2,4,6,12,14)(2,4,6,12,14,16)(2,4,6,12,14,16,18)(2,4,6,12,14,16,18,20)(2,4,6,12,14,16,20)(2,4,6,12,14,18)(2,4,6,12,14,18,20)(2,4,6,12,14,20)(2,4,6,12,16)(2,4,6,12,16,18)(2,4,6,12,16,18,20)(2,4,6,12,16,20)(2,4,6,12,18)(2,4,6,12,18,20)(2,4,6,12,20)(2,4,6,14)(2,4,6,14,16)(2,4,6,14,16,18)(2,4,6,14,16,18,20)(2,4,6,14,16,20)(2,4,6,14,18)(2,4,6,14,18,20)(2,4,6,14,20)(2,4,6,16)(2,4,6,16,18)(2,4,6,16,18,20)(2,4,6,16,20)(2,4,6,18)(2,4,6,18,20)(2,4,6,20)(2,4,8)(2,4,8,10)(2,4,8,10,12)(2,4,8,10,12,14)(2,4,8,10,12,14,16)(2,4,8,10,12,14,16,18)(2,4,8,10,12,14,16,18,20)(2,4,8,10,12,14,16,20)(2,4,8,10,12,14,18)(2,4,8,10,12,14,18,20)(2,4,8,10,12,14,20)(2,4,8,10,12,16)(2,4,8,10,12,16,18)(2,4,8,10,12,16,18,20)(2,4,8,10,12,16,20)(2,4,8,10,12,18)(2,4,8,10,12,18,20)(2,4,8,10,12,20)(2,4,8,10,14)(2,4,8,10,14,16)(2,4,8,10,14,16,18)(2,4,8,10,14,16,18,20)(2,4,8,10,14,16,20)(2,4,8,10,14,18)(2,4,8,10,14,18,20)(2,4,8,10,14,20)(2,4,8,10,16)(2,4,8,10,16,18)(2,4,8,10,16,18,20)(2,4,8,10,16,20)(2,4,8,10,18)(2,4,8,10,18,20)(2,4,8,10,20)(2,4,8,12)(2,4,8,12,14)(2,4,8,12,14,16)(2,4,8,12,14,16,18)(2,4,8,12,14,16,18,20)(2,4,8,12,14,16,20)(2,4,8,12,14,18)(2,4,8,12,14,18,20)(2,4,8,12,14,20)(2,4,8,12,16)(2,4,8,12,16,18)(2,4,8,12,16,18,20)(2,4,8,12,16,20)(2,4,8,12,18)(2,4,8,12,18,20)(2,4,8,12,20)(2,4,8,14)(2,4,8,14,16)(2,4,8,14,16,18)(2,4,8,14,16,18,20)(2,4,8,14,16,20)(2,4,8,14,18)(2,4,8,14,18,20)(2,4,8,14,20)(2,4,8,16)(2,4,8,16,18)(2,4,8,16,18,20)(2,4,8,16,20)(2,4,8,18)(2,4,8,18,20)(2,4,8,20)(2,4,10)(2,4,10,12)(2,4,10,12,14)(2,4,10,12,14,16)(2,4,10,12,14,16,18)(2,4,10,12,14,16,18,20)(2,4,10,12,14,16,20)(2,4,10,12,14,18)(2,4,10,12,14,18,20)(2,4,10,12,14,20)(2,4,10,12,16)(2,4,10,12,16,18)(2,4,10,12,16,18,20)(2,4,10,12,16,20)(2,4,10,12,18)(2,4,10,12,18,20)(2,4,10,12,20)(2,4,10,14)(2,4,10,14,16)(2,4,10,14,16,18)(2,4,10,14,16,18,20)(2,4,10,14,16,20)(2,4,10,14,18)(2,4,10,14,18,20)(2,4,10,14,20)(2,4,10,16)(2,4,10,16,18)(2,4,10,16,18,20)(2,4,10,16,20)(2,4,10,18)(2,4,10,18,20)(2,4,10,20)(2,4,12)(2,4,12,14)(2,4,12,14,16)(2,4,12,14,16,18)(2,4,12,14,16,18,20)(2,4,12,14,16,20)(2,4,12,14,18)(2,4,12,14,18,20)(2,4,12,14,20)(2,4,12,16)(2,4,12,16,18)(2,4,12,16,18,20)(2,4,12,16,20)(2,4,12,18)(2,4,12,18,20)(2,4,12,20)(2,4,14)(2,4,14,16)(2,4,14,16,18)(2,4,14,16,18,20)(2,4,14,16,20)(2,4,14,18)(2,4,14,18,20)(2,4,14,20)(2,4,16)(2,4,16,18)(2,4,16,18,20)(2,4,16,20)(2,4,18)(2,4,18,20)(2,4,20)(2,6)(2,6,8)(2,6,8,10)(2,6,8,10,12)(2,6,8,10,12,14)(2,6,8,10,12,14,16)(2,6,8,10,12,14,16,18)(2,6,8,10,12,14,16,18,20)(2,6,8,10,12,14,16,20)(2,6,8,10,12,14,18)(2,6,8,10,12,14,18,20)(2,6,8,10,12,14,20)(2,6,8,10,12,16)(2,6,8,10,12,16,18)(2,6,8,10,12,16,18,20)(2,6,8,10,12,16,20)(2,6,8,10,12,18)(2,6,8,10,12,18,20)(2,6,8,10,12,20)(2,6,8,10,14)(2,6,8,10,14,16)(2,6,8,10,14,16,18)(2,6,8,10,14,16,18,20)(2,6,8,10,14,16,20)(2,6,8,10,14,18)(2,6,8,10,14,18,20)(2,6,8,10,14,20)(2,6,8,10,16)(2,6,8,10,16,18)(2,6,8,10,16,18,20)(2,6,8,10,16,20)(2,6,8,10,18)(2,6,8,10,18,20)(2,6,8,10,20)(2,6,8,12)(2,6,8,12,14)(2,6,8,12,14,16)(2,6,8,12,14,16,18)(2,6,8,12,14,16,18,20)(2,6,8,12,14,16,20)(2,6,8,12,14,18)(2,6,8,12,14,18,20)(2,6,8,12,14,20)(2,6,8,12,16)(2,6,8,12,16,18)(2,6,8,12,16,18,20)(2,6,8,12,16,20)(2,6,8,12,18)(2,6,8,12,18,20)(2,6,8,12,20)(2,6,8,14)(2,6,8,14,16)(2,6,8,14,16,18)(2,6,8,14,16,18,20)(2,6,8,14,16,20)(2,6,8,14,18)(2,6,8,14,18,20)(2,6,8,14,20)(2,6,8,16)(2,6,8,16,18)(2,6,8,16,18,20)(2,6,8,16,20)(2,6,8,18)(2,6,8,18,20)(2,6,8,20)(2,6,10)(2,6,10,12)(2,6,10,12,14)(2,6,10,12,14,16)(2,6,10,12,14,16,18)(2,6,10,12,14,16,18,20)(2,6,10,12,14,16,20)(2,6,10,12,14,18)(2,6,10,12,14,18,20)(2,6,10,12,14,20)(2,6,10,12,16)(2,6,10,12,16,18)(2,6,10,12,16,18,20)(2,6,10,12,16,20)(2,6,10,12,18)(2,6,10,12,18,20)(2,6,10,12,20)(2,6,10,14)(2,6,10,14,16)(2,6,10,14,16,18)(2,6,10,14,16,18,20)(2,6,10,14,16,20)(2,6,10,14,18)(2,6,10,14,18,20)(2,6,10,14,20)(2,6,10,16)(2,6,10,16,18)(2,6,10,16,18,20)(2,6,10,16,20)(2,6,10,18)(2,6,10,18,20)(2,6,10,20)(2,6,12)(2,6,12,14)(2,6,12,14,16)(2,6,12,14,16,18)(2,6,12,14,16,18,20)(2,6,12,14,16,20)(2,6,12,14,18)(2,6,12,14,18,20)(2,6,12,14,20)(2,6,12,16)(2,6,12,16,18)(2,6,12,16,18,20)(2,6,12,16,20)(2,6,12,18)(2,6,12,18,20)(2,6,12,20)(2,6,14)(2,6,14,16)(2,6,14,16,18)(2,6,14,16,18,20)(2,6,14,16,20)(2,6,14,18)(2,6,14,18,20)(2,6,14,20)(2,6,16)(2,6,16,18)(2,6,16,18,20)(2,6,16,20)(2,6,18)(2,6,18,20)(2,6,20)(2,8)(2,8,10)(2,8,10,12)(2,8,10,12,14)(2,8,10,12,14,16)(2,8,10,12,14,16,18)(2,8,10,12,14,16,18,20)(2,8,10,12,14,16,20)(2,8,10,12,14,18)(2,8,10,12,14,18,20)(2,8,10,12,14,20)(2,8,10,12,16)(2,8,10,12,16,18)(2,8,10,12,16,18,20)(2,8,10,12,16,20)(2,8,10,12,18)(2,8,10,12,18,20)(2,8,10,12,20)(2,8,10,14)(2,8,10,14,16)(2,8,10,14,16,18)(2,8,10,14,16,18,20)(2,8,10,14,16,20)(2,8,10,14,18)(2,8,10,14,18,20)(2,8,10,14,20)(2,8,10,16)(2,8,10,16,18)(2,8,10,16,18,20)(2,8,10,16,20)(2,8,10,18)(2,8,10,18,20)(2,8,10,20)(2,8,12)(2,8,12,14)(2,8,12,14,16)(2,8,12,14,16,18)(2,8,12,14,16,18,20)(2,8,12,14,16,20)(2,8,12,14,18)(2,8,12,14,18,20)(2,8,12,14,20)(2,8,12,16)(2,8,12,16,18)(2,8,12,16,18,20)(2,8,12,16,20)(2,8,12,18)(2,8,12,18,20)(2,8,12,20)(2,8,14)(2,8,14,16)(2,8,14,16,18)(2,8,14,16,18,20)(2,8,14,16,20)(2,8,14,18)(2,8,14,18,20)(2,8,14,20)(2,8,16)(2,8,16,18)(2,8,16,18,20)(2,8,16,20)(2,8,18)(2,8,18,20)(2,8,20)(2,10)(2,10,12)(2,10,12,14)(2,10,12,14,16)(2,10,12,14,16,18)(2,10,12,14,16,18,20)(2,10,12,14,16,20)(2,10,12,14,18)(2,10,12,14,18,20)(2,10,12,14,20)(2,10,12,16)(2,10,12,16,18)(2,10,12,16,18,20)(2,10,12,16,20)(2,10,12,18)(2,10,12,18,20)(2,10,12,20)(2,10,14)(2,10,14,16)(2,10,14,16,18)(2,10,14,16,18,20)(2,10,14,16,20)(2,10,14,18)(2,10,14,18,20)(2,10,14,20)(2,10,16)(2,10,16,18)(2,10,16,18,20)(2,10,16,20)(2,10,18)(2,10,18,20)(2,10,20)(2,12)(2,12,14)(2,12,14,16)(2,12,14,16,18)(2,12,14,16,18,20)(2,12,14,16,20)(2,12,14,18)(2,12,14,18,20)(2,12,14,20)(2,12,16)(2,12,16,18)(2,12,16,18,20)(2,12,16,20)(2,12,18)(2,12,18,20)(2,12,20)(2,14)(2,14,16)(2,14,16,18)(2,14,16,18,20)(2,14,16,20)(2,14,18)(2,14,18,20)(2,14,20)(2,16)(2,16,18)(2,16,18,20)(2,16,20)(2,18)(2,18,20)(2,20)(4)(4,6)(4,6,8)(4,6,8,10)(4,6,8,10,12)(4,6,8,10,12,14)(4,6,8,10,12,14,16)(4,6,8,10,12,14,16,18)(4,6,8,10,12,14,16,18,20)(4,6,8,10,12,14,16,20)(4,6,8,10,12,14,18)(4,6,8,10,12,14,18,20)(4,6,8,10,12,14,20)(4,6,8,10,12,16)(4,6,8,10,12,16,18)(4,6,8,10,12,16,18,20)(4,6,8,10,12,16,20)(4,6,8,10,12,18)(4,6,8,10,12,18,20)(4,6,8,10,12,20)(4,6,8,10,14)(4,6,8,10,14,16)(4,6,8,10,14,16,18)(4,6,8,10,14,16,18,20)(4,6,8,10,14,16,20)(4,6,8,10,14,18)(4,6,8,10,14,18,20)(4,6,8,10,14,20)(4,6,8,10,16)(4,6,8,10,16,18)(4,6,8,10,16,18,20)(4,6,8,10,16,20)(4,6,8,10,18)(4,6,8,10,18,20)(4,6,8,10,20)(4,6,8,12)(4,6,8,12,14)(4,6,8,12,14,16)(4,6,8,12,14,16,18)(4,6,8,12,14,16,18,20)(4,6,8,12,14,16,20)(4,6,8,12,14,18)(4,6,8,12,14,18,20)(4,6,8,12,14,20)(4,6,8,12,16)(4,6,8,12,16,18)(4,6,8,12,16,18,20)(4,6,8,12,16,20)(4,6,8,12,18)(4,6,8,12,18,20)(4,6,8,12,20)(4,6,8,14)(4,6,8,14,16)(4,6,8,14,16,18)(4,6,8,14,16,18,20)(4,6,8,14,16,20)(4,6,8,14,18)(4,6,8,14,18,20)(4,6,8,14,20)(4,6,8,16)(4,6,8,16,18)(4,6,8,16,18,20)(4,6,8,16,20)(4,6,8,18)(4,6,8,18,20)(4,6,8,20)(4,6,10)(4,6,10,12)(4,6,10,12,14)(4,6,10,12,14,16)(4,6,10,12,14,16,18)(4,6,10,12,14,16,18,20)(4,6,10,12,14,16,20)(4,6,10,12,14,18)(4,6,10,12,14,18,20)(4,6,10,12,14,20)(4,6,10,12,16)(4,6,10,12,16,18)(4,6,10,12,16,18,20)(4,6,10,12,16,20)(4,6,10,12,18)(4,6,10,12,18,20)(4,6,10,12,20)(4,6,10,14)(4,6,10,14,16)(4,6,10,14,16,18)(4,6,10,14,16,18,20)(4,6,10,14,16,20)(4,6,10,14,18)(4,6,10,14,18,20)(4,6,10,14,20)(4,6,10,16)(4,6,10,16,18)(4,6,10,16,18,20)(4,6,10,16,20)(4,6,10,18)(4,6,10,18,20)(4,6,10,20)(4,6,12)(4,6,12,14)(4,6,12,14,16)(4,6,12,14,16,18)(4,6,12,14,16,18,20)(4,6,12,14,16,20)(4,6,12,14,18)(4,6,12,14,18,20)(4,6,12,14,20)(4,6,12,16)(4,6,12,16,18)(4,6,12,16,18,20)(4,6,12,16,20)(4,6,12,18)(4,6,12,18,20)(4,6,12,20)(4,6,14)(4,6,14,16)(4,6,14,16,18)(4,6,14,16,18,20)(4,6,14,16,20)(4,6,14,18)(4,6,14,18,20)(4,6,14,20)(4,6,16)(4,6,16,18)(4,6,16,18,20)(4,6,16,20)(4,6,18)(4,6,18,20)(4,6,20)(4,8)(4,8,10)(4,8,10,12)(4,8,10,12,14)(4,8,10,12,14,16)(4,8,10,12,14,16,18)(4,8,10,12,14,16,18,20)(4,8,10,12,14,16,20)(4,8,10,12,14,18)(4,8,10,12,14,18,20)(4,8,10,12,14,20)(4,8,10,12,16)(4,8,10,12,16,18)(4,8,10,12,16,18,20)(4,8,10,12,16,20)(4,8,10,12,18)(4,8,10,12,18,20)(4,8,10,12,20)(4,8,10,14)(4,8,10,14,16)(4,8,10,14,16,18)(4,8,10,14,16,18,20)(4,8,10,14,16,20)(4,8,10,14,18)(4,8,10,14,18,20)(4,8,10,14,20)(4,8,10,16)(4,8,10,16,18)(4,8,10,16,18,20)(4,8,10,16,20)(4,8,10,18)(4,8,10,18,20)(4,8,10,20)(4,8,12)(4,8,12,14)(4,8,12,14,16)(4,8,12,14,16,18)(4,8,12,14,16,18,20)(4,8,12,14,16,20)(4,8,12,14,18)(4,8,12,14,18,20)(4,8,12,14,20)(4,8,12,16)(4,8,12,16,18)(4,8,12,16,18,20)(4,8,12,16,20)(4,8,12,18)(4,8,12,18,20)(4,8,12,20)(4,8,14)(4,8,14,16)(4,8,14,16,18)(4,8,14,16,18,20)(4,8,14,16,20)(4,8,14,18)(4,8,14,18,20)(4,8,14,20)(4,8,16)(4,8,16,18)(4,8,16,18,20)(4,8,16,20)(4,8,18)(4,8,18,20)(4,8,20)(4,10)(4,10,12)(4,10,12,14)(4,10,12,14,16)(4,10,12,14,16,18)(4,10,12,14,16,18,20)(4,10,12,14,16,20)(4,10,12,14,18)(4,10,12,14,18,20)(4,10,12,14,20)(4,10,12,16)(4,10,12,16,18)(4,10,12,16,18,20)(4,10,12,16,20)(4,10,12,18)(4,10,12,18,20)(4,10,12,20)(4,10,14)(4,10,14,16)(4,10,14,16,18)(4,10,14,16,18,20)(4,10,14,16,20)(4,10,14,18)(4,10,14,18,20)(4,10,14,20)(4,10,16)(4,10,16,18)(4,10,16,18,20)(4,10,16,20)(4,10,18)(4,10,18,20)(4,10,20)(4,12)(4,12,14)(4,12,14,16)(4,12,14,16,18)(4,12,14,16,18,20)(4,12,14,16,20)(4,12,14,18)(4,12,14,18,20)(4,12,14,20)(4,12,16)(4,12,16,18)(4,12,16,18,20)(4,12,16,20)(4,12,18)(4,12,18,20)(4,12,20)(4,14)(4,14,16)(4,14,16,18)(4,14,16,18,20)(4,14,16,20)(4,14,18)(4,14,18,20)(4,14,20)(4,16)(4,16,18)(4,16,18,20)(4,16,20)(4,18)(4,18,20)(4,20)(6)(6,8)(6,8,10)(6,8,10,12)(6,8,10,12,14)(6,8,10,12,14,16)(6,8,10,12,14,16,18)(6,8,10,12,14,16,18,20)(6,8,10,12,14,16,20)(6,8,10,12,14,18)(6,8,10,12,14,18,20)(6,8,10,12,14,20)(6,8,10,12,16)(6,8,10,12,16,18)(6,8,10,12,16,18,20)(6,8,10,12,16,20)(6,8,10,12,18)(6,8,10,12,18,20)(6,8,10,12,20)(6,8,10,14)(6,8,10,14,16)(6,8,10,14,16,18)(6,8,10,14,16,18,20)(6,8,10,14,16,20)(6,8,10,14,18)(6,8,10,14,18,20)(6,8,10,14,20)(6,8,10,16)(6,8,10,16,18)(6,8,10,16,18,20)(6,8,10,16,20)(6,8,10,18)(6,8,10,18,20)(6,8,10,20)(6,8,12)(6,8,12,14)(6,8,12,14,16)(6,8,12,14,16,18)(6,8,12,14,16,18,20)(6,8,12,14,16,20)(6,8,12,14,18)(6,8,12,14,18,20)(6,8,12,14,20)(6,8,12,16)(6,8,12,16,18)(6,8,12,16,18,20)(6,8,12,16,20)(6,8,12,18)(6,8,12,18,20)(6,8,12,20)(6,8,14)(6,8,14,16)(6,8,14,16,18)(6,8,14,16,18,20)(6,8,14,16,20)(6,8,14,18)(6,8,14,18,20)(6,8,14,20)(6,8,16)(6,8,16,18)(6,8,16,18,20)(6,8,16,20)(6,8,18)(6,8,18,20)(6,8,20)(6,10)(6,10,12)(6,10,12,14)(6,10,12,14,16)(6,10,12,14,16,18)(6,10,12,14,16,18,20)(6,10,12,14,16,20)(6,10,12,14,18)(6,10,12,14,18,20)(6,10,12,14,20)(6,10,12,16)(6,10,12,16,18)(6,10,12,16,18,20)(6,10,12,16,20)(6,10,12,18)(6,10,12,18,20)(6,10,12,20)(6,10,14)(6,10,14,16)(6,10,14,16,18)(6,10,14,16,18,20)(6,10,14,16,20)(6,10,14,18)(6,10,14,18,20)(6,10,14,20)(6,10,16)(6,10,16,18)(6,10,16,18,20)(6,10,16,20)(6,10,18)(6,10,18,20)(6,10,20)(6,12)(6,12,14)(6,12,14,16)(6,12,14,16,18)(6,12,14,16,18,20)(6,12,14,16,20)(6,12,14,18)(6,12,14,18,20)(6,12,14,20)(6,12,16)(6,12,16,18)(6,12,16,18,20)(6,12,16,20)(6,12,18)(6,12,18,20)(6,12,20)(6,14)(6,14,16)(6,14,16,18)(6,14,16,18,20)(6,14,16,20)(6,14,18)(6,14,18,20)(6,14,20)(6,16)(6,16,18)(6,16,18,20)(6,16,20)(6,18)(6,18,20)(6,20)(8)(8,10)(8,10,12)(8,10,12,14)(8,10,12,14,16)(8,10,12,14,16,18)(8,10,12,14,16,18,20)(8,10,12,14,16,20)(8,10,12,14,18)(8,10,12,14,18,20)(8,10,12,14,20)(8,10,12,16)(8,10,12,16,18)(8,10,12,16,18,20)(8,10,12,16,20)(8,10,12,18)(8,10,12,18,20)(8,10,12,20)(8,10,14)(8,10,14,16)(8,10,14,16,18)(8,10,14,16,18,20)(8,10,14,16,20)(8,10,14,18)(8,10,14,18,20)(8,10,14,20)(8,10,16)(8,10,16,18)(8,10,16,18,20)(8,10,16,20)(8,10,18)(8,10,18,20)(8,10,20)(8,12)(8,12,14)(8,12,14,16)(8,12,14,16,18)(8,12,14,16,18,20)(8,12,14,16,20)(8,12,14,18)(8,12,14,18,20)(8,12,14,20)(8,12,16)(8,12,16,18)(8,12,16,18,20)(8,12,16,20)(8,12,18)(8,12,18,20)(8,12,20)(8,14)(8,14,16)(8,14,16,18)(8,14,16,18,20)(8,14,16,20)(8,14,18)(8,14,18,20)(8,14,20)(8,16)(8,16,18)(8,16,18,20)(8,16,20)(8,18)(8,18,20)(8,20)(10)(10,12)(10,12,14)(10,12,14,16)(10,12,14,16,18)(10,12,14,16,18,20)(10,12,14,16,20)(10,12,14,18)(10,12,14,18,20)(10,12,14,20)(10,12,16)(10,12,16,18)(10,12,16,18,20)(10,12,16,20)(10,12,18)(10,12,18,20)(10,12,20)(10,14)(10,14,16)(10,14,16,18)(10,14,16,18,20)(10,14,16,20)(10,14,18)(10,14,18,20)(10,14,20)(10,16)(10,16,18)(10,16,18,20)(10,16,20)(10,18)(10,18,20)(10,20)(12)(12,14)(12,14,16)(12,14,16,18)(12,14,16,18,20)(12,14,16,20)(12,14,18)(12,14,18,20)(12,14,20)(12,16)(12,16,18)(12,16,18,20)(12,16,20)(12,18)(12,18,20)(12,20)(14)(14,16)(14,16,18)(14,16,18,20)(14,16,20)(14,18)(14,18,20)(14,20)(16)(16,18)(16,18,20)(16,20)(18)(18,20)(20)()
Since number of elements in the set can go upto 10^5 and hence the size of the set will go upto 2^(10^5) which is huge. You can just either print it or if you want to store it , store it in a file.
It is not possible, as you pointed out yourself power set contains pow(2, size) no of elements. You need to print the entire set that can only be done by generating the set using backtracking, there is nothing better.
To find sum of all subsets of a given set is however a simpler problem.
If the no of elements is n, then each element in the array occurs 2^(n-1) times in the power set.
Pseudo code :
int sum = 0;
for(int i = 0;i < n;i++){
sum += ( set[i] * ( 1 << (n-1) ) ); //pow(2, n-1) == 1 << (n-1)
}
cout << sum << endl;
You would need a Big Integer Library for larger values of n.