Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

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

pseudo code pseudocode

  • Please log in to reply
3 replies to this topic

#1 x2187

x2187

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 04 December 2008 - 02:09 PM

Dear Members,

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

I want to write a program 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,
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 04 December 2008 - 02:29 PM

Since this is clearly homework: what do you have so far for an algorithm?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 x2187

x2187

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 05 December 2008 - 05:57 AM

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.

The algorithm should be recursive (i.e. a subroutine calling itself). You 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 !
  • 0

#4 x2187

x2187

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 05 December 2008 - 06:36 AM

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


Thanks for your help !
  • 0





Also tagged with one or more of these keywords: pseudo code, pseudocode

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download