Thread: recursion function / Pascal

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    recursion function / Pascal

    I need to write a RECURSIVE function to calculate binomial coefficients.

    It has been 9 years since last I took a mathematics course.
    I have forgotten the purpose of this formula:

    C(n,0) = 1 and C(n,n)=1 fo n >=0
    C(n,k)=C(n-1,k)+C(n-1, k-1) for n >k>0

    Does anyone know of what this is to yield??
    What is C here in the formula(s)??
    Does this have to do with coordinates that cross a point on an
    (x,y) graph???

    Help get me started on this program.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    This looks like a variation of Ackermann
    http://hissa.nist.gov/dads/HTML/ackermann.html

    > C(n,0) = 1 and C(n,n)=1 fo n >=0
    > C(n,k)=C(n-1,k)+C(n-1, k-1) for n >k>0

    > Does anyone know of what this is to yield?
    Big numbers very quickly i think

    > What is C here in the formula(s)??
    A name for the function - since C is written in terms of C, its a recursive function.

    It looks a bit like this
    Code:
    int C ( int n, int k ) {
      if ( k == 0 || n == k ) return 1;
      else return C(n-1,k) + C(n-1,k-1);
    }
    > Does this have to do with coordinates that cross a point on an
    (x,y) graph???
    Perhaps - if you treat the two parameters as x,y coordinates
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Post

    C(n,k) is the number of subsets of size k of the set {1,2,3,...n}
    C(n,0)=1 as only one set has 0 elements the empty set
    C(n,n)=1 as subset of n elements of a set of n elements must be the whole set
    one way to look at this formula is as follows
    consider the set S={1,2,...,n}
    How many subsets of size k of S are there:C(n,k)
    How many subsets of size k of S are there which contain 1:
    C(n-1,k-1) (1 is already in , choose the reamining k-1 , from the remaining n-1)
    How many subsets of size k of S are there which do not contain 1: C(n-1,k) (choose all k elements from {2,3,4,...n})
    Adding up
    C(n,k)=C(n-1,k-1)+C(n-1,k)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Question

    I have three questions:

    1) what is the best method of asking for user input of n and k values ? such as in the form C(1,2)

    2) shouldn't the below code have an additional set of parentheses around C(n-1,k) + C(n-1,k-1)
    on the line with 'else return' ? so it is returning the value of the addition of the two recursive calls of C ??

    3) is this final return line returning a single value???

    > What is C here in the formula(s)??
    A name for the function - since C is written in terms of C, its a recursive function.

    It looks a bit like this

    code:--------------------------------------------------------------------------------
    int C ( int n, int k ) {
    if ( k == 0 || n == k ) return 1;
    else return C(n-1,k) + C(n-1,k-1);
    }
    --------------------------------------------------------------------------------

  5. #5
    Unregistered
    Guest
    I have three questions:

    1) what is the best method of asking for user input of n and k values ? such as in the form C(1,2)

    2) shouldn't the below code have an additional set of parentheses around C(n-1,k) + C(n-1,k-1)
    on the line with 'else return' ? so it is returning the value of the addition of the two recursive calls of C ??

    3) is this final return line returning a single value???

    1) think about what it is you want function to do
    2) I think so.
    3) put values in for the variables n and k and check and see

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    Unhappy

    Code compiles okay.
    But when I run program after I enter my values as asked,
    get segFault. Why ???

    I think my scanf and printf lines in main() might be the problem.
    Please help.

    I want the user to input as
    (x,y)

    and the result to say

    The Coefficients of (x,y) is: (return value)

    Any ideas??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int c(int row, int index);
    
    main()
    {
      int n, k;
      printf("input values of C as (n,k)");
      scanf("%d" "%d",n,k);
    
      printf("The coefficient of (%d,%d) is : %d",n,k,c(n,k));
    
      return 0;
    
    }
    
    int c(int row, int index)
    {
      if (row == 0 || index == 0)
         return 1;
    
      else
         return c(row-1,index) + c(row-1,index-1);
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your problem is with 'scanf'. Pass it the address of the variables:

    scanf( "%d %d", &x, &y );

    Quzah.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    I fixed the scanf call.

    I use input of (7,3)
    7th row of pascal's triangle, 3rd element
    result should be 35 (i thought this was the 4th element, myself)

    getting garbage output, why???

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>


    int c(int rowint index);

    main()
    {
      
    int nk;
      
    printf("input values of C as (n,k)\n");
      
    scanf("%d" "%d",&n,&k);

      
    printf("The coefficient of (%d,%d) is : %d\n",n,k,c(n,k));

      return 
    0;

    }

    int c(int rowint index)
    {
      if (
    row == || index == 0)
         return 
    1;

      else
         return 
    c(row-1,index) + c(row-1,index-1);


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM