
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Generate All Possible Combinations Out of a,b,c,d,e
This is a C++ program to generate all possible combinations out of a, b, c, d, e.
Algorithms
Begin Take the number of elements and the elements as input. function Combi(char a[], int reqLen, int s, int currLen, bool check[], int l) to print the all possible combination of given array set: // Here, char a[] = character array reqLen = required length s = start variable currLen = current length check[] = a boolean variable l = length of array // Body of the Function: If currLen>reqLen Return Else if currLen=reqLen Then print the new generated sequence. If s = l Then return no further element is left. For every index there are two option: either proceed with a start as ‘true’ and recursively call Combi() with incremented value of ‘currLen’ and ‘s’. Or proceed with a start as ‘false’ and recursively call Combi() with only incremented value of ‘s’. End
Example
#include<iostream> using namespace std; void Combi(char a[], int reqLen, int s, int currLen, bool check[], int l) { if(currLen > reqLen) return; else if (currLen == reqLen) { cout<<"\t"; for (int i = 0; i < l; i++) { if (check[i] == true) { cout<<a[i]<<" "; } } cout<<"\n"; return; } if (s == l) { return; } check[s] = true; Combi(a, reqLen, s + 1, currLen + 1, check, l); check[s] = false; Combi(a, reqLen, s + 1, currLen, check, l); } int main() { int i,n; bool check[n]; cout<<"Enter the number of element array have: "; cin>>n; char a[n]; cout<<"\n"; for(i = 0; i < n; i++) { cout<<"Enter "<<i+1<<" element: "; cin>>a[i]; check[i] = false; } for(i = 1; i <= n; i++) { cout<<"\nThe all possible combination of length "<<i<<" for the given array set:\n"; Combi(a, i, 0, 0, check, n); } return 0; }
Output
Enter the number of element array have: 5 Enter 1 element: a Enter 2 element: b Enter 3 element: c Enter 4 element: d Enter 5 element: e The all possible combination of length 1 for the given array set: a b c d e The all possible combination of length 2 for the given array set: a b a c a d a e b c b d b e c d c e d e The all possible combination of length 3 for the given array set: a b c a b d a b e a c d a c e a d e b c d b c e b d e c d e The all possible combination of length 4 for the given array set: a b c d a b c e a b d e a c d e b c d e The all possible combination of length 5 for the given array set: a b c d e
- Related Articles
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- How to generate a list of all possible 4 digits combinations in Excel?
- Generate all combinations of supplied words in JavaScript
- Program to find list of all possible combinations of letters of a given string s in Python
- JavaScript function that generates all possible combinations of a string
- C++ Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Golang Program to Read Three Digits and Print all Possible Combinations from the Digits
- Generate all combinations of a specific size from a single set in PHP
- Print all possible combinations of r elements in a given array of size n in C++
- Python program to generate all possible valid ID address from given string
- Program to generate all possible strings by taking the choices in python
- Python - Generate all possible permutations of words in a Sentence
- Finding all possible combinations from an array in JavaScript
- C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters

Advertisements