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

Thread Starter
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.

A,B,C,D,AB,AC,AD,... ABC,BCD...ABCD etc.

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

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top