Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

recursive code for bell number... help ¡¡¡

recursive

  • Please log in to reply
3 replies to this topic

#1 R¡CHARD

R¡CHARD

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 16 October 2010 - 08:42 PM

Hi !

The bell number is the number of partitions of a set with n members.. look at the recursion formula in this link:

Bell number - Wikipedia, the free encyclopedia

so i have to do the recursive code in c++ , i've did without any troubles other recursive programs like the tower of hanoi, catalan numbers, fibonacci numbers, ackermann function, but the bell number not yet.
well this is the code that i had made, so ,i just wanna know if is my implementation correct? I'm assuming it's not, but where am I going wrong?

//bell number
#include<iostream.h>
#include<conio.h>

long fact(int k)
{
     if(k == 0)
          return 1;
     else
         return k * fact(k - 1);
}

long bell(int k, int n, int j)
{
     if(k == 0)
          return 1;
     else
         return fact(n)/fact(j)* fact(n - j)) * bell * j;
}

int main ()
{
    int n, int j = 0, int sum = 0;
    cout << " BELL NUMBER " << endl;
    cout << " Enter the number of elements of the set /n";
    cin >> n;
    for(j = 0; j < n; j++)
    {
          sum+ = j
          }
    cout << " the number of partitions of this set is " << bell(n)"/n";
    getch();
    return 0;
}

Note: I don't know why, but it's not necessary put "using namespace std"

Edited by Alexander, 18 October 2010 - 04:08 PM.
Added [code] tags around code

  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 16 October 2010 - 10:56 PM

Please edit your post and wrap [noparse][/noparse] tags around your code.

Your syntax has errors which prevent it from running. For example you are telling the bell and fact functions to return type long, yet you accept type int, "long bell(int k, int n, int j)". You are also using not using function calls correctly in various places.

A sample output of correct Bell numbers should be:
1, 1, 2, 5, 15, 52, 203, 877, 4140, 21147, 115975, 678570, 4213597,  27644437, 190899322, 1382958545, 10480142147, 82864869804, 682076806159,  5832742205057, 51724158235372, 474869816156751, 4506715738447323
So you may wish to check your program, if it outputs similar numbers, then your implementation is correct.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 R¡CHARD

R¡CHARD

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 18 October 2010 - 04:05 PM

how can i edit my post ? so, i checked the syntax, i tried changin the variable types, but it don't running yet
  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 18 October 2010 - 04:16 PM

The edit button is directly below your post, it looks like this: Posted Image

I had cleaned up your code errors a bit:
//bell number
#include <iostream>
#include <conio.h>
using namespace std;

long fact(long k)
{
     if(k == 0)
          return 1;
     else
         return k * fact(k - 1);
}

long bell(long k, long n, long j)
{
     if(k == 0)
          return 1;
     else
         return fact(n)/fact(j)* fact(n - j)) * bell * j;
}

int main ()
{
    int n, j = 0, sum = 0;
    cout << " BELL NUMBER " << endl;
    cout << " Enter the number of elements of the set /n";
    cin >> n;
    for(j = 0; j < n; j++)
    {
          sum += j;
    }
    cout << " the number of partitions of this set is " << bell(n) << "/n";
    getch();
    return 0;
}
But notice on this line:
return fact(n)/fact(j)* fact(n - j)) * bell * j;
You call bell but without any arguments, bell should be bell(k, n, j) right?

Your other error is here:
cout << " the number of partitions of this set is " <<  bell(n) << "/n";
You are calling bell with one argument here this time, there should be three. Once those are corrected it may work.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






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