Thread: Subset problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    2

    Subset problem

    Hi. I need a c/c++ code, which generates the subsets of a given set. i could not find anywhere on the internet. If you help me, i will be glad.

    i have an array which is wArray[] that has length 5 for example.

    wArray = {1,2,3,4,5}

    this array is the weight array (it contains the weights of some items), so the subsets of it will be:

    Code:
    sArray = {1,2,3,4,5,(1+2),(1+3),(1+4)....,(1+2+4),....(1+2+3+4+5)}
    how can i generate subsets of this set and assign it to a different array?

    Thank you...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A for loop?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    2
    i thought that it will be by recursion and also i could not do it by recursion.
    how can i do it by a for loop?

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    http://www2.toki.or.id/book/AlgDesig...eratingsubsets

    From that source it should be simple to devise an appropriate algorithm.

    I have the code.

    But the if this is homework, then I won't be posting.



    He he...

    Code:
    Enter n: 5
    Generating subsets...
      1
      1  2
      1  2  3
      1  2  3  4
      1  2  3  4  5
      1  2  3  5
      1  2  4
      1  2  4  5
      1  2  5
      1  3
      1  3  4
      1  3  4  5
      1  3  5
      1  4
      1  4  5
      1  5
      2
      2  3
      2  3  4
      2  3  4  5
      2  3  5
      2  4
      2  4  5
      2  5
      3
      3  4
      3  4  5
      3  5
      4
      4  5
      5
    Last edited by treenef; 12-06-2005 at 03:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM