Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,197,896 members, 7,966,338 topics. Date: Friday, 04 October 2024 at 12:51 PM

HELP! If You Are Into C/C++ Programming, Come In! - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / HELP! If You Are Into C/C++ Programming, Come In! (485 Views)

The Future Of C++ Programming Language– Is It Still Among The Trends? / Do You Want To Learn Programming, Come In Here / C Programming Assignment Help ? (2) (3) (4)

(1) (Reply) (Go Down)

HELP! If You Are Into C/C++ Programming, Come In! by Ikechukwu183: 6:57pm On Mar 31, 2022
Please, I need a code to print all possible arrangement (permutation) of the combination of an array (of 'k' elements), i.e. the permutation of the result of combination.



For clarity of purpose, let's say the array has the following elements (k=4):


"alpha", "beta", "gamma", "delta".


Then, for the combination, we choose r=3

After which permutation is carried out.


[For better understanding, the result of the combination is put in UPPERCASE, while the unique result of permutation is in lowercase]

The expected result is as follows:


ALPHA BETA GAMMA
alpha gamma beta
beta alpha gamma
beta gamma alpha
gamma alpha beta
gamma beta alpha
ALPHA BETA DELTA
alpha delta beta
beta alpha delta
beta delta alpha
delta alpha beta
delta beta alpha
ALPHA DELTA GAMMA
alpha gamma delta
delta alpha gamma
delta gamma alpha
gamma alpha delta
gamma delta alpha
BETA DELTA GAMMA
beta gamma delta
delta beta gamma
delta gamma beta
gamma beta delta
gamma delta beta


From the result above, all unique PERMUTATON of the COMBINATION for r=3 from the array of k=4 elements is completed.


This is confirmed since 4C3 x 3! = 24 lines of results

On my own I have been able to get separate codes for permutation and combination, but I can only utilize them MANUALLY as I have to keep substituting the elements of the array of the permutation code with results from the combination code.

Please, I need someone to combine the two codes below into one code and let it work to produce results like the example above.

COMBINATION CODE:

/*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[] = {"alpha", "beta", "gamma", "delta"};
int r = 3;
int n = sizeof(arr) / sizeof(arr[0]);
printCombination(arr, n, r);
}


PERMUTATION CODE:
#include <bits/stdc++.h>
#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[] = { "alpha", "beta", "gamma"};
int n = sizeof(a) / sizeof(a[0]);
findPermutations(a, n);
return 0;
}

PLEASE, I need help!
Re: HELP! If You Are Into C/C++ Programming, Come In! by Ikechukwu183: 6:59pm On Mar 31, 2022
I know it's a long read, but I appreciate your patience.
Re: HELP! If You Are Into C/C++ Programming, Come In! by Ikechukwu183: 7:05pm On Mar 31, 2022
Also, I will be gifting anyone that helps me arrange the code with N1,000 in recharge card or cash transfer, first. Just to show my appreciation
Re: HELP! If You Are Into C/C++ Programming, Come In! by Kvngfrosh(m): 7:44pm On Mar 31, 2022
Take it to stack overflow
Re: HELP! If You Are Into C/C++ Programming, Come In! by Ikechukwu183: 8:07pm On Mar 31, 2022
Kvngfrosh:
Take it to stack overflow

I have taken it there but they are not willing to help. I believe their programmers are a bit arrogant. Well, I don't really blame them, they just feel it's too simple so they won't answer it.

But they don't realize that everyone is not at the same level.

1 Like

Re: HELP! If You Are Into C/C++ Programming, Come In! by Deicide: 8:57pm On Apr 02, 2022
C++ has a function next_premutation() use it.

(1) (Reply)

How Do I Convert US CST To Nigerian Time / Username Change / Professional Python Data Analyst and Backend Developers

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.