0

I'm struggling with some code I have to write for an assignment in C. I have to calculate a Catalan number recursively. The following formula is given: Formula IMG.

The awnsers should be:

0 > 1 (Filling in 0 should print 1)

5 > 42 (Filling in 5 should print 42)

7 > 429 (Filling in 7 should print 429)

9 > 4862 (Filling in 9 should print 4862)

10 > 16796 (Filling in 10 should print 16796)

Please have a look at it:

#pragma warning(disable : 4996)
#include <stdio.h>

int catalanRecursief(int n) {
    if (n == 0){
        return 1;
    } else { 
        return (2 * ((2 * n) - 1)) / (n + 1) * (catalanRecursief(n - 1));
}
}


int main(void){
    int n;
    printf("Catalan printer\n\n");
    printf("What catalan number do you want to calculate? ");
    scanf("%d", &n);

    /*catalanRecursief(n);*/

    printf("Catalan number: %d > %d", n, catalanRecursief(n));
    
    getchar();
    return(0);
    
}

Laurens
  • 33
  • 8
  • Division of integers truncates the decimal portion. Change one of the numbers in your formula to a float. Something like `return (2. * ((2 * n) - 1)) / (n + 1) * (catalanRecursief(n - 1));` (notice the decimal place on the first 2) – Jerry Jeremiah Sep 27 '21 at 21:15
  • Better still: do all of the multiplications before any of the divisions - `return (2 * ((2 * n) - 1)) * (catalanRecursief(n - 1)) / (n + 1)` – psmears Sep 27 '21 at 21:18

2 Answers2

2

By changing the unit from n to float it will be enough to solve it.

int catalanRecursief(float n) {
  if (n == 0) {
    return 1;
  } else {
    return ((2 * ((2 * n) - 1)) / (n + 1)) * (catalanRecursief(n - 1));
  }
}

or also

int catalanRecursief(int n) {
  if (n == 0) {
    return 1;
  } else {
    return ((2.0 * ((2 * n) - 1)) / (n + 1)) * (catalanRecursief(n - 1));
  }
}

this is because dividing int numbers c truncates them

fraco_oxza
  • 70
  • 6
  • I would never advise to use floating point here. According to [wikipedia](https://en.wikipedia.org/wiki/Catalan_number) the Catalan numbers are an **integer** sequence. – Serge Ballesta Sep 27 '21 at 21:37
  • I understand, but the final number is never a float, it is only temporarily. For example, in the case of 3, 2.5 * 2 * 1 * 1 is calculated and the result is 5. you can always multiply a float by a integer so that the result is integer – fraco_oxza Sep 27 '21 at 21:45
  • You may get *almost* correct values for large numbers, if int is 64 bits wide, because double have only 48 bits of mantissa. Using floating point numbers for integer operation is know to be the cause of many problems... – Serge Ballesta Sep 27 '21 at 21:54
  • that's for an school assignment. Eventually, it is interesting to warn about limits of floating point numbers for information, but this is more than enough to get the exact correct result in every case for OP. – Pac0 Sep 27 '21 at 22:00
0

As the theory says that Catalan numbers are integers, you should just ensure that the division happens last to avoid an integer division truncation:

...
else {
    return (2 * ((2 * n) - 1)) * (catalanRecursief(n - 1)) / (n + 1);
}
...

With 32 bits integers, overflow is to be expected starting at C17, so use long long if you want to go further...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252