arrays - My code Catalan number in C is not working ( using recurrence formula ) -


i trying insert catalan number in array, code not working.

  • description: insert elements in catalan sequence in array given initialized c[0].
  • inputs: address of array
    • n: next position filled;
    • top: maximum number of entries computed.
  • output:
    • int: number of elements in array.
    • side effects: update elements of array.

code:

#include <stdio.h>   #define max 6 int catseq (int catarray[], int n, int top){     int c;     if (top == 1) catarray[n]= 1;      else{          ( c = 0; c <= max; c++){             catarray[n] = 2 * (2*top - 1) * catseq(catarray, n, top-1) / (top+1);             n++;         }     }     return n; } void printseq(int seq[], int top){     int i;      ( = 1; < max; i++)           printf("%d \n", seq[i]); } int main(){     int c = 0, n = 0 ;     int catarray[max];     c = catseq(catarray, n, max);     printseq(catarray, c);     return 0; } 

array out of index error:

for ( c = 0 ; c <= max;c++){                 ^                 check loop 

correct is:

for ( n = 0 ; n < max; n++){ 

it should n < max, since n passed in non-zero function.

catarray[n] = 2 * (2*top - 1) * catseq(catarray, n, top-1) / (top+1);                                                  ^                                                 here n non-zero  

no need of c variable.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -