# algorithm question

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

#### calculus87

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.

#### moebius

got a language preference?

#### Snake~eyes

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.

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

As Seen On