You are here:

C++/a question


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)


Dear Bita:

I do not think RAND is going to help you much since you want all possible combinations.

You can easily develop an algorithm to give you all the combinations, but you will have to use as many nestled do-loops as characters.  For example to get all the combinations for two characters (a,b), you must use two nestled do-loops.  To get all the combinations for five characters, you must use five nestled do-loops.

Using RAND would be harder since you would have to keep using RAND until you have found all the possible combinations, which might take quite a long time.  It will take quite a long time using the nestled do-loops to if you have a lot of characters:

for two characters:
for ( char i = 'a' ; i <= 'b' ; ++ i )
 for ( char j = 'a' ; j <= 'b' ; ++ j )
   char result [ 2 ] ;
   result [ 0 ] = i ;
   result [ 1 ] = j ;


All Answers

Answers by Expert:

Ask Experts


Titus B. Ledbetter, Jr.


C , MFC, Object Oriented, Artificial Intelligence


I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Master's Degree Computer Science from Johns Hopkins

©2017 All rights reserved.