1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

algorithm question

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

Thread Status:
Not open for further replies.
  1. calculus87

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

    moebius

    Joined:
    Oct 7, 2003
    Messages:
    1,741
    got a language preference?
     
  3. Snake~eyes

    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
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/170810

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice