GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Mar-2010, 10:29
fourtytwo fourtytwo is offline
New Member
 
Join Date: Mar 2010
Location: Poland
Posts: 5
fourtytwo is on a distinguished road

Calculating Stirling Number Of The Second Kind


Hi there,
I have to make a program that calculates stirling number of the second kind (http://en.wikipedia.org/wiki/Stirling_numbers_of_the_second_kind), using dynamic programming.

I wrote something like this, and it works for smaller numbers but it fails when n=5, k=4 (stirling number is 10, and my program returns 6)

CPP / C++ / C Code:
// To be run with arguments n i k. "./stirling.x n k"


#include <iostream>
using namespace std;

unsigned long long int mnozenie = 0;

unsigned long long int stirling(unsigned long long int n, unsigned long long
int k)
{
       

       unsigned long long int a[n][k];
       for (int i=0; i<=n; i++){     //clearing the table with zeros
       	for (int j=0; j<=k; j++){
       		a[i][j]=0;	
       		};
       }; 
       
        for (int i=0; i<=n; i++){     
       	for (int j=0; j<=k; j++){
       		if (i<j) a[i][j]=0;
       		else if (i==j) a[i][j]=1;
       		else if (j==1) a[i][j]=1;
       		else if (j==0) a[i][j]=0;
       		else if ((j<i)&&((!(j==1))&&(!(j==0))))  a[i][j]= (j * a[i-1][j]) + a[i-1][j-1];
       		cout<<"i: "<<i<<", j: "<<j<<", a[i][j]: "<<a[i][j]<<endl;	
       		};
       }; 
       
               
        for (int i=0; i<=n; i++){     
       	for (int j=0; j<=i; j++){
       		 
       		 cout<<"i: "<<i<<", j: "<<j<<", a[i][j]: "<<a[i][j]<<endl;
       		};
       }; 

       

       return a[n][k];

}

int main(int argc, char *argv[])
{

       unsigned long long int a,b;
       int n,k;
       n = atoi(argv[1]);
       k = atoi(argv[2]);
       if ( n < k ) { cout << "N must be grater than k!" << endl; }
       cout<<"n: "<<n<<endl<<"k: "<<k<<endl<<"Stirling num: "<<stirling(n,k)<<endl;

return 0;
}
  #2  
Old 19-Mar-2010, 11:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Calculating Stirling Number Of The Second Kind


Quote:
Originally Posted by fourtytwo
...
I wrote something like this...

CPP / C++ / C Code:
...
       unsigned long long int a[n][k];
       for (int i=0; i<=n; i++){     //clearing the table with zeros
       	for (int j=0; j<=k; j++){
       		a[i][j]=0;	
       		};
       }; 
...


Since your index values go up to and including n and k, the array dimension must be a[n+1][k+1]. See Footnote.

Anyhow, with proper limits on the array and with slight modifications to your main() function, after commenting out print statements in your stirling() function I get
Code:
Stirling numbers of the second kind n\k | 0 1 2 3 4 5 6 7 8 9 --- +------------------------------------------------------------ 0 | 1 1 | 0 1 2 | 0 1 1 3 | 0 1 3 1 4 | 0 1 7 6 1 5 | 0 1 15 25 10 1 6 | 0 1 31 90 65 15 1 7 | 0 1 63 301 350 140 21 1 8 | 0 1 127 966 1701 1050 266 28 1 9 | 0 1 255 3025 7770 6951 2646 462 36 1

Regards,

Dave

Footnote:
Even if your compiler accepts variable length arrays, it's not part of the current C++ standard. Allocation using new would be generally recommended if portability is a consideration.
Last edited by davekw7x : 19-Mar-2010 at 11:48.
  #3  
Old 19-Mar-2010, 11:32
fourtytwo fourtytwo is offline
New Member
 
Join Date: Mar 2010
Location: Poland
Posts: 5
fourtytwo is on a distinguished road

Re: Calculating Stirling Number Of The Second Kind


Thank you so much! It now works quite well.

In this case it's ok that I'm using variable lenght arrays, since it's only an examplatory implementation of an algorythm I had to made for a homework, but I'll keep it in mind for the future.
Thanks again, your reply had solved all my problems.

- Nathalie
  #4  
Old 19-Mar-2010, 11:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Calculating Stirling Number Of The Second Kind


Quote:
Originally Posted by fourtytwo
...It now works...
Thank you for sharing the code, and thank you for the feedback.

Regards,

Dave
 
 

Recent GIDBlogR for statistics by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in Calculating week number ahbi82 C Programming Language 10 08-Dec-2009 05:49
Converting a number amount to text Godzilla C++ Forum 5 31-Mar-2006 11:38
calculating number of bytes Haru C++ Forum 3 11-Dec-2005 01:15
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:03.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.