0

I modified a code I've found on the internet to fit my needs. It calculates and prints all possible combinations of r elements in an array given size of N. Here's the code:

#include <iostream>
#include <vector>

void combinationUtil(std::vector<int> arr, std::vector<int> data, int start, int end, int index, int r);

void printCombination(std::vector<int> arr, int n, int r)
{
    std::vector<int> data;
    data.assign(r, 0);
    combinationUtil(arr, data, 0, n-1, 0, r);
}

void combinationUtil(std::vector<int> arr, std::vector<int> data, int start, int end, int index, int r)
{
    if (index == r)
    {
        for (int j = 0; j < r; j++)
            std::cout << data.at(j) << " ";
        std::cout << std::endl;
        return;
    }

    for (int i = start; i <= end && end - i + 1 >= r - index; i++)
    {
        data.at(index) = arr.at(i);
        combinationUtil(arr, data, i+1, end, index+1, r);
    }
}

int main()
{
    std::vector<int> arr = {1, 2, 3, 4, 5};
    int r = 3;
    int n = arr.size();
    printCombination(arr, n, r);
}

The output of it is:

1 2 3 
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4 
2 3 5
2 4 5
3 4 5 

I can modify the start value to 1 so the output can start from value 2 like so:

2 3 4 
2 3 5
2 4 5
3 4 5

How can I achieve a similar effect for the end. For example if I wanted it to end before calculating combinations starting with value 2. I want to see a result like:

1 2 3 
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5

I want to do this so I can utilize parallelizations for a larger scale function. I hope I could relay the idea clear enough. Thanks in advance. (Code compiles with some casting warnings. I just left it like this for an easier read for the reader.)

  • the code is terrible. Where did you find it? Its making a whole lot of unnecessary copies – 463035818_is_not_an_ai Aug 15 '22 at 07:29
  • geeksforgeeks. I use pointers in my actual code but didn't want to paste a 200 line code here. What would you recommend to improve this? – Taylan Ünver Aug 15 '22 at 07:43
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 15 '22 at 07:49
  • 1
    i recommend to not use code from geeksforgeeks. Its unfortunate, but most code I have seen from this site is either not even proper C++, or rather poor, or both – 463035818_is_not_an_ai Aug 15 '22 at 07:49
  • @Community my problem is I can't limit the code to run for certain amount without the ability of having all combinations up until that point. I thought my question was clear with all the examples but I can provide further information if the ambiguity is pointed out. – Taylan Ünver Aug 15 '22 at 08:05

1 Answers1

-1

I solved the issue by modifying another combination method I found on this site.

Here's the code for it:

#include <iostream>
#include <vector>

using namespace std;

vector<int> people;
vector<int> combination;

void pretty_print(const vector<int>& v) {
  static int count = 0;
  cout << "combination no " << (++count) << ": [ ";
  for (int i = 0; i < v.size(); ++i) { cout << v[i] << " "; }
  cout << "] " << endl;
}

void go(int offset, int k, int end, bool outermost = true) {
  if (k == 0) {
    pretty_print(combination);
    return;
  }
  for (int i = offset; i <= people.size() - k; ++i) {
    combination.push_back(people[i]);
    go(i+1, k-1, end, false);
    combination.pop_back();
    if(outermost && i == end) return;
  }
}

int main() {
  int k = 3, end = 1;

  people = {1, 2, 3, 4, 5};
  go(0, k, end);

  return 0;
}

offset controls start and end controls the end. For loop going on at the outside most layer controls the first element selected for the combination and it progresses as the function recurses. If statement inside the for loop checks if the wanted line is reached and returns the function prematuraly as needed.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 18 '22 at 11:53