You are here:

C++/a question

Advertisement


Question
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


Answer
> 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).  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

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.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.