recursion? combinations of numbers

This is a discussion on recursion? combinations of numbers within the C Programming forums, part of the General Programming Boards category; I've been working on this program for almost 3 weeks, but I can not figure out how to do it. ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    recursion? combinations of numbers

    I've been working on this program for almost 3 weeks, but I can not figure out how to do it. Let me know if you can help.

    i have to write a program using recursion to print a list of number possibilities from a given set of numbers of a given number of digits.

    Sample input:
    3 2
    3 5 6
    0

    3 is the amount of numbers in the set.
    2 is the amount of digits in the output numbers.
    3 5 6 is the set of numbers
    0 tells the program to quit

    sample out put would look like:
    33 35 36 53 55 56 63 65 66


    so everything comes down to 1 function that i cant write recursively for the life of me.
    iteratively, it is simple but that's not acceptable as a solution.

    my program takes in the number set, sorts them by ascending, stores them in an array, and then ... blank.

    i just cant get my mind to process this recursively. this program is an assignment so i can not post code.

    let me know if you can help. thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ominub View Post
    this program is an assignment so i can not post code.
    Pretty sure the answer then is DITTO. If you are having trouble with your code and want to post what you have tried, someone may critique it or make suggestions.

    Everyone else is comfortable posting code when they are working on an assignment, just look around.

    Otherwise, you are just asking someone to do your homework for you, which is against cboard policy.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    iteratively, it is simple but that's not acceptable as a solution.
    How do you do it iteratively? If you have some sort of data structure that you're storing information in in a stack-like manner, you may be able to turn the stack into the function call-stack.

    A hint for a recursive solution: you're basically calculating permutations with repetitions. Recursion is all about breaking problems down into smaller ones. Let's say your numbers are 0 and 1, and you want permutations of length 2. What possibilities are there? There's 0N and 1N, where 'N' is all of the permutations of length 2-1. What are the permutations of length 1? 0N and 1N, where 'N' is all of the permutations of length 1-1. What are the permutations of length 0? Just the empty string.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  2. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 09:56 AM
  3. Reversing a set of numbers using recursion
    By Shikimo in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2002, 02:47 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21