0

Good day everyone, I am quite new to C++ coding but I am taking courses on it.

I want to write a code that will find the combination of r elements of an array and then permutate the result.

I have been able to research from various sources and have separate codes that will print combination and permutation of the array.

The challenge I am facing is how to combine the two codes and make it work as one.

The first code is for the combination of the array of 5 elements in which 4 elements are to be selected at a time.

/*C++ program to print all combination  of size r in an array of size n*/ 
#include <iostream> 
using namespace std; 
 
void combinationUtil(string arr[], string data[], 
                    int start, int end, 
                    int index, int r); 
 
/*The main function that prints all combinations of size r in arr[] of
  size n. This function mainly uses combinationUtil()*/
void printCombination(string arr[], int n, int r) 
{ 
    /*A temporary array to store all combination one by one*/
    string data[r]; 

    /*Print all combination using temporary array 'data[]'*/
    combinationUtil(arr, data, 0, n-1, 0, r); 
} 
 
/*arr[] ---> Input Array 
  data[] ---> Temporary array to 
  store current combination 
  start & end ---> Starting and
  Ending indexes in arr[] 
  index ---> Current index in data[] 
  r ---> Size of a combination to be printed */
void combinationUtil(string arr[], string data[], 
                    int start, int end, 
                    int index, int r) 
{ 
   
    if (index == r) 
    { 
        for (int j = 0; j < r; j++) 
            cout << data[j] << " "; 
        cout << endl; 
        return; 
    } 
 
    /*replace index with all possible elements. The condition "end-i+1 >= r-index"
      makes sure that including one element at index will make a combination
      with remaining elements at remaining positions*/
    for (int i = start; i <= end && 
        end - i + 1 >= r - index; i++) 
    { 
        data[index] = arr[i]; 
        combinationUtil(arr, data, i+1, 
                        end, index+1, r); 
    } 
} 
 
// Driver code 
int main() 
{ 
    string arr[] = {"Red", "Orange", "Green", "Blue", "Indigo"}; 
    int r = 4; 
    int n = sizeof(arr) / sizeof(arr[0]); 
    printCombination(arr, n, r); 
} 

The second code is to permutate each of the results of the combination (previous code) of the array, which means permutation of 4 elements. In this code below, the permutation of the first-line of results from the first code was used.

#include <iostream>
#include <string>
using namespace std;

// Function to display the array
void display(string a[], int n)
{
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

// Function to find the permutations
void findPermutations(string a[], int n)
{
    // Sort the given array
    sort(a, a + n);

    // Find all possible permutations
    cout << "Possible permutations are:\n";
    do {
        display(a, n);
    } while (next_permutation(a, a + n));
}

// Driver code
int main()
{
    string a[] = { "Red", "Orange", "Green", "Blue"};
    int n = sizeof(a) / sizeof(a[0]);
    findPermutations(a, n);
    return 0;
}

In summary, a code that will find the combination of an array of words and the permutate each line of result of the combination is required.

  • 1
    "I am looking for a code" is a phrase that's very likely to trigger this question being closed as off-topic (see item 3 in the FAQ: https://stackoverflow.com/help/on-topic ) -- you might want to rephrase that. – Jeremy Friesner Mar 21 '22 at 01:34
  • Might I suggest TAoCP, or as I like to call it, taco-p. Many many good algorithms in there explained in autistic detail, included generating all permutations in all sorts of different orderings. – Taekahn Mar 21 '22 at 02:17
  • Replace `/*Current combination is ready to be printed, print it*/` by (a variant of) `findPermutations`? – Jarod42 Mar 21 '22 at 08:49
  • " but I am taking courses on it."... sorry to say, but it feels you took the wrong courses! "using namespace std; ", variable names without any meaningful name, if/else and loop "blocks" without braces and all the C-style array stuff and size calculations. Only as an example: If you have a compile time known sized array, use std::array insead. If size is unknown, give std::vector a chance. Your code is more or less bad style C. It is not because I want to blame you, but to give you a hint that the code has bad style. You should watch out for better learning material! – Klaus Mar 21 '22 at 11:57

1 Answers1

0

You have 2 pieces of code, each of which prints some data. You want to connect them. One way to do that is to change one piece of code to store the data in some data structure, instead of printing. Then change the other piece of code to get this data structure, and work on its contents.

This brings up the idea of data structures. C++ has std::vector, which is an array with variable size. This is a very simple and most useful data structure in C++. The elements of a vector can have any specific type, including vector, which allows using nested lists (see below).

Change your printCombination function to return std::vector instead of void (also change its name appropriately):

std::vector<std::vector<std::string>>
findCombinations(std::vector<std::string> strings, int r)
{
    ...
}

I omitted the implementation, because I want to concentrate on interfaces in my answer.

Note that this function's return type is std::vector<std::vector<std::string>>. In general, std::vector<T> means "a list of items, each of which has type T". So the return type of this function is a list of combinations, where each combination is a list of strings. Also, I replaced an array of strings by std::vector<std::string>, which is a list of strings. It's functionally the same as an array, but slightly better, because it stores the length inside, and doesn't require a dedicated additional argument to your function.

Then change your findPermutations to receive its data using std::vector instead of array.

void findPermutations(std::vector<std::string> a)
{
   ...
}

(I omitted pass-by-reference for brevity)

Then make your "driver" function call the first function and pass its output to the second function:

// Driver code
int main()
{
    std::vector<std::string> arr{"Red", "Orange", "Green", "Blue", "Indigo"};
    int r = 4;
    auto combinations = findCombinations(arr, r);
    for (auto combination: combinations)
        findPermutations(combination);
}

(I omitted pass-by-reference in the for-loop for brevity)

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • 1
    Not a good idea to remove const& for brevity for my point of view. It is absolutely important to define clear interfaces. Passing data by copy must have an intention and a reader of an interface expects that the function will modify the data but not give them back, for whatever reason. Especially if you advice a beginner, make clear what and why you do something. "bravity" is not an argument... my two cents! – Klaus Mar 21 '22 at 13:15
  • @anatoylg, I really don't understand how vector works. I would really appreciate if you can fix the code together. Also, paste your USDT address, I have a gift you. – Kanu Micheal Mar 25 '22 at 03:41
  • Please, I am still waiting for your help to put the codes together as one code. I will really appreciate – Kanu Micheal Mar 25 '22 at 18:27