# algorithm question

Discussion in 'Software Development' started by calculus87, Oct 9, 2003.

Not open for further replies.

Joined:
Sep 4, 2003
Messages:
34
I am having a hard time trying to figure out something and was wondering if anyone could point me in the right direction,

ok....say I have x number of attributes
ex. ABCD

How could I write code to find every combination of the 4 letters but no two combinations have the same letters in them.

and if it has BCD then it won't have CBD.

Also it has to work no matter how many attributes are entered.

Any help would be greatful.

Thanks so much.

2. ### moebius

Joined:
Oct 7, 2003
Messages:
1,741
got a language preference?

3. ### Snake~eyes

Joined:
Apr 18, 2002
Messages:
640
I have just solved the problem!

Code:
```	void main()
{
int total = 4; //total number of attributes
string letterCombo = "ABCD"; //the string name
string newResult;
for(int counter = 0; counter < total; counter++)
{
newResult = "";
for(int secCounter = counter; secCounter < total; secCounter++)
{
newResult = newResult +  letterCombo[secCounter];
cout << newResult << ", ";
}
}
}
```
Using your example, the output is:

A, AB, ABC, ABCD, B, BC, BCD, C, CD, D

Basically the first loop picks the first character. And the second loop then adds the subsquent characters on.

One time through first loop outputs: A, AB, ABC, ABCD
(each one of those combinations is outputtted by the second loop)
Second time through first loop outputs: B, BC, BD
Third time: C, CD
Fourth time: D

I hope that makes sense.

As Seen On