CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    3

    URGENT: I need a pseudo code for performing a certain task

    Dear Members,

    If you can help me, I will be greatly thankful,

    I want to write an algorithm to do the following:
    Input: two positive integers N,K
    The output should be an array whose number of columns is N,
    and its rows lists all the possible combinations of natural integers less than or equal K (possibly zero) such that the sum of every row is K.

    To clerify more, Let N=3, K=2
    the output array is
    2 0 0
    1 1 0
    1 0 1
    0 2 0
    0 0 2
    0 1 1

    Notice that the sum of every row = K=2

    Another example, N=4,K=3

    3 0 0 0
    2 1 0 0
    2 0 1 0
    2 0 0 1
    1 2 0 0
    1 1 1 0
    1 1 0 1
    1 0 2 0
    1 0 1 1
    1 0 0 2
    0 3 0 0
    0 2 1 0
    0 2 0 1
    0 1 2 0
    0 1 1 1
    0 1 0 2
    0 0 3 0
    0 0 2 1
    0 0 1 2
    0 0 0 3




    Thanks,

  2. #2

    Re: URGENT: I need a pseudo code for performing a certain task

    Some hints, as this sounds like homework:

    The first column can contain the numbers 0 to K.

    Given the value in first column, how do the remaining columns look? Are they the same problem with different N and K?

    Given the values in all but the last column, what must the value in the last column be?

  3. #3
    Join Date
    Dec 2008
    Posts
    3

    Re: URGENT: I need a pseudo code for performing a certain task

    No, this isn't a homework, although it looks like it!

    I writing some program in MATLAB, and I needed to generate this array. It represents a very small block in my program.

    I have the algorithm in my mind, but I wasn't successful in formulating it as a pseudo-code.

    so, I know that the algorithm should be recursive (i.e. the subroutine call itself). You can make a decreasing loop of the first column value, and it terminates at the last column. Also, the number of rows is (n+k-1)choose(n-1)

    If you could help me by writing the pseudo-code, I will be thankful.
    It was long ago when I was exercising these classical programming problems !

  4. #4
    Join Date
    Dec 2008
    Posts
    3

    Talking Re: URGENT: I need a pseudo code for performing a certain task

    Actually, I have finished the program now and it is working !!


    Thanks for your help !
    Last edited by x2187; December 5th, 2008 at 09:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured