You are here:

- Home
- Computing/Technology
- C/C++
- C++
- a question

Advertisement

Hi

i want to write a programme that shows all combination of 'n' variable,for instance for two variable 'a' and 'b',it prints:"aa","ab","ba","bb"

my question is that:

i should found a method for printing these combination or i can use RAND function?

i mean is it logical to use RAND function or it's better to use a method for finding the combination?

(indeed i want to know your idea as an expert)

Thanx

Bita

> for instance for two variable 'a' and 'b',it prints:"aa","ab","ba","bb"

"ab" is a different ordering of "ba"; these are permutations, not combinations. http://en.wikipedia.org/wiki/Permutation#In_combinatorics

> i should found a method for printing these combination or i can use RAND function?

you should use an algorithm to generate all possible permutations.

std::rand() can be used to generate a random permutation; not to enumerate all permutations.

hint: i would be easier to program in terms of generating all permutations of the N elements of an array (instead of N distinct variables).

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | No Comment |

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

about 15 years or so**Education/Credentials**

post graduate engineer