Pds Pb Function 2013

download Pds Pb Function 2013

of 49

Transcript of Pds Pb Function 2013

  • 7/29/2019 Pds Pb Function 2013

    1/49

    Functions

    Programming & Data StructureCS 11002

    Partha Bhowmickhttp://cse.iitkgp.ac.in/pb

    CSE Department

    IIT Kharagpur

    Spring 2012-2013

    PB | CSE IITKGP | Spring 2012-2013 PDS

  • 7/29/2019 Pds Pb Function 2013

    2/49

    Functions

    Callee : Caller = Bowler : Captain

  • 7/29/2019 Pds Pb Function 2013

    3/49

    Functions Iter Rec Fibo

    Functions (1)int f(){

    ...}

    main(){

    ...f();

    ...

    }

    Here f is the callee function.main is the caller function, which calls f().

    PB | CSE IITKGP | Spring 2012-2013 PDS

  • 7/29/2019 Pds Pb Function 2013

    4/49

    Functions Iter Rec Fibo

    Computation of sinx (1)

    sin x = x x3

    3!+ x

    5

    5! x

    7

    7!+ x

    9

    9!

    = i0

    (1)ix2i+1

    (2i + 1)!

    =i0

    ti, where ti = (1)i

    x2i+1

    (2i + 1)!

    A finite number of terms of the above powerseries can be used to compute an approximatevalue of sin x, where x is in radian.

    PB | CSE IITKGP | Spring 2012-2013 PDS

  • 7/29/2019 Pds Pb Function 2013

    5/49

    Functions Iter Rec Fibo

    Computation of sinx (2)

    Inductive Definitions

    ti =

    x if i = 0,

    ti1x2

    2i(2i + 1)if i > 0.

    Si =

    t0 if i = 0,Si1 + ti if i > 0.

    They are also called recurrence relations orrecursive definitions.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    F ti It R Fib

  • 7/29/2019 Pds Pb Function 2013

    6/49

    Functions Iter Rec Fibo

    Computation of sinx (3)

    Inductive Definition Iterative Process

    An iterative process of computation can beobtained from the inductive definition.Thus, an approximation of sin x is computed as

    Si, the sum from t0 to ti, as follows.1 Start with initial values: t0 and S0.2 Repeat the following two steps for i > 0:

    1 Compute ti from ti1.2 Compute the next approximate value of sin x by

    computing Si from Si1 and ti.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    F ti It R Fib

  • 7/29/2019 Pds Pb Function 2013

    7/49

    Functions Iter Rec Fibo

    Computation of sinx (4)

    Termination of Iteration

    The process is to be terminated after a finitenumber of iterations. The termination may be

    1 after a fixed number of iterations, or2 after achieving a pre-specified accuracy.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    8/49

    Functions Iter Rec Fibo

    Computation of sinx (5)

    #include

    #include int main() {

    float x, preErr, appErr, rad, term, sinx;int i = 1;

    printf("Enter the angle in degree: ");scanf("%f", &x);printf("Enter the Percentage Error: ");scanf("%f", &preErr);

    // Initializationrad = atan(1.0)*x/45.0; term = rad; sinx = term;

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    9/49

    Functions Iter Rec Fibo

    Computation of sinx (6)

    do { // Iteration

    float factor;factor = 2.0 * i++;factor *= (factor + 1.0);factor = - rad * rad / factor;

    sinx += (term = factor * term);appErr = 100.0*fabs(term/sinx);} while (appErr >= preErr);

    printf("\nsin(%f) = %55.50f\n#Iterations = %d\n",

    x, sinx, i - 1);return 0;

    } // sin.c

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    10/49

    Functions Iter Rec Fibo

    Computation of sinx (7)

    $ cc -lm -Wall sin.c

    $ ./a.outEnter the angle in degree: 90Enter the Percentage Error: .1sin(90.000000) =

    1.00000345706939697265625000000000000000000000000000#Iterations = 4

    $ ./a.outEnter the angle in degree: 90

    Enter the Percentage Error: .01sin(90.000000) =0.99999988079071044921875000000000000000000000000000#Iterations = 5

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    11/49

    Functions Iter Rec Fibo

    Computation of sinx (8)

    $ ./a.out

    Enter the angle in degree: 90Enter the Percentage Error: .00001sin(90.000000) =0.99999994039535522460937500000000000000000000000000

    #Iterations = 6

    $ ./a.outEnter the angle in degree: 90Enter the Percentage Error: .000000000001

    sin(90.000000) =0.99999994039535522460937500000000000000000000000000

    #Iterations = 10

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    12/49

    Computation of sinx (9)

    Caution!Notice the last two runs.The accuracy of the program decreases for largevalues of angle due to error propagation.

    PB | CSE IITKGP | Spring 2012-2013 PDS

  • 7/29/2019 Pds Pb Function 2013

    13/49

    Recursion

    To understand recursion,

    you must understand

    recursion.

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    14/49

    Recursion (1)

    Example: Factorial Function

    n! =

    1 if n = 0,n (n 1)! ifn > 0.

    or,

    fact(n) = 1 if n = 0,n fact(n 1) if n > 0.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    15/49

    Recursion (2)

    Example: Computation of 4!

    4! = 4 3!

    = 4 (3 2!)

    = 4 (3 (2 1!))= 4 (3 (2 (1 0!)))

    = 4 (3 (2 (1 1)))

    = 4 (3 (2 1))

    = 4 (3 2)

    = 4 6 = 2 4

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    16/49

    Recursion (3)

    Features

    There is no value computation in the firstfour steps, as the function is being unfolded.

    The value computation starts only after the

    basis of the definition is reached.Last four steps computes the values.

    Definition: Recursive Function

    A function that calls itself directly or indirectly iscalled a recursive function.Unfolding and delayed computation can besimulated by such a function.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    17/49

    Recursion (4)

    Recursive Call

    A()call A A()

    call A A()

    call A A()

    As a recursive function calls itself, the obviousquestion is about its termination.The basis of the inductive (recursive) definitionprovides the condition for termination.

    The function calls itself to gradually reach thetermination condition or the basis!

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    18/49

    Recursion (5)

    int fact(int n){

    if (n == 0)

    return 1;

    else

    return n * fact(n-1);}

    fact(4) = 4 fact(3) = 4 (3 fact(2))= 4 (3 (2 fact(1))) = 4 (3 (2 (1 fact(0))))

    = 4 (3 (2 (1 1))) = 4 (3 (2 1)) = = 24.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    19/49

    Recursion (6)

    Functions during Recursive Calls

    For every call, there is new incarnation of allthe formal parameters and the local variables(that are not static). The variable names

    get bound to different memory locations.Variables of one invocation are not visiblefrom another invocation.

    Once a return statement is executed, all thevariables of the corresponding invocation die.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    20/49

    Recursion (7)

    The last incarnation of a variable name dies

    first and the last call is returned firstaprinciple called last in first out (LIFO).

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    21/49

    Stack Frame or Activation Record (1)

    main() xstack frame

    nreturn address

    factorial(4)stack frame

    Stack Region

    4

    main() xstack frame

    nreturn addres

    factorial(4)

    StackRegion

    4

    return addres3

    factorial(3) n

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    22/49

    Stack Frame or Activation Record (2)

    main() x

    nreturn address

    factorial(4)

    Stack Region

    4

    return address3 n

    stack frame

    2

    1

    0

    return address

    return address

    n

    n

    n

    factorial(3)

    factorial(0)

    factorial(1)

    factorial(2)

    main() x

    nreturn addres

    factorial(4)

    StackRegion

    4

    return addres3 n

    stack frame

    2

    1

    return addres

    return addres

    n

    n

    factorial(3)

    factorial(1)

    factorial(2)

    return addrReturn 1

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

  • 7/29/2019 Pds Pb Function 2013

    23/49

    Stack Frame or Activation Record (3)

    main() x

    nreturn address

    factorial(4)

    Stack Region

    4

    return address3 n

    stack frame

    2

    1

    return addressn

    n

    factorial(3)

    factorial(1)

    factorial(2)

    main() x

    nreturn addres

    factorial(4)

    StackRegion

    4

    return addres3 n

    stack frame

    2 return addresn

    factorial(3)

    factorial(2)

    return addrReturn 1

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    S k A d

  • 7/29/2019 Pds Pb Function 2013

    24/49

    Stack Frame or Activation Record (4)

    main() x

    nfactorial(4)

    Stack Region

    4

    stack framemain() x

    StackRegion

    stack frame

    return addrReturn 24

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    E i id f i f i

  • 7/29/2019 Pds Pb Function 2013

    25/49

    Expensive side of recursive function (1)

    The recursive factorial function uses morememory than its non-recursive counterpart.

    The non-recursive function uses fixed amountof memory for an int data, whereas the

    memory usage by the recursive function isproportional to the value of data.

    A function call and return takes someamount of extra time.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    R i F i b I i !

  • 7/29/2019 Pds Pb Function 2013

    26/49

    Recursive Function by Iteration! (1)

    Value computation in recursively defined factorialfunction starts after unfolding the recursion. Butwe can redesign the function so that it can startthe computation from the beginning.

    int factIter(int n) {

    int acc = 1, i;

    for(i = n; i > 0; --i) acc *= i;

    return acc;}

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    d( )

  • 7/29/2019 Pds Pb Function 2013

    27/49

    gcd(m, n) (1)

    Inductive Definition & Recursive Function

    gcd(m, n)|mn = n if m = 0,gcd(n mod m, m) if m > 0.

    int gcd(int m, int n){ // m

  • 7/29/2019 Pds Pb Function 2013

    28/49

    gcd(m, n) (2)

    Different Calls to gcd()

    gcd(0, 0) return 0

    gcd(0, 5) return 5

    gcd(5, 0) gcd(0, 5) return 5

    gcd(18, 12) gcd(12, 18)

    gcd(6, 12)

    gcd(0, 6)

    return 6

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fib i S

  • 7/29/2019 Pds Pb Function 2013

    29/49

    Fibonacci Sequence (1)

    Fibonaccis question in the year 1202:

    How many pairs of rabbits will be there in a year from now?

    Conditions:

    Begin with 1 male and 1 female rabbitjust

    bornwhich reach sexual maturity after 1 month.

    The gestation period of a rabbit is 1 month.

    On reaching sexual maturity, a female rabbit gives

    birth to 1 male and 1 female rabbit in every month.

    Rabbits never die.

    Leonardo Pisano Fibonacci (11701250, Pisa).

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fib i S

  • 7/29/2019 Pds Pb Function 2013

    30/49

    Fibonacci Sequence (2)

    n 0 1 2 3 4 5 6 7 8 9 10 fib(n) 0 1 1 2 3 5 8 13 21 34 55

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fib i S

  • 7/29/2019 Pds Pb Function 2013

    31/49

    Fibonacci Sequence (3)

    0 1 1 2 3 5 8 13 21 34 ...

    A tiling with squares whose side lengths are successiveFibonacci numbers.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence

  • 7/29/2019 Pds Pb Function 2013

    32/49

    Fibonacci Sequence (4)

    Fibonacci spiral from golden rectangle of size 13 8.(It starts at the left-bottom corner of the first square (length 8) and

    passes through the farthest corners of succeeding squares.)

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence

  • 7/29/2019 Pds Pb Function 2013

    33/49

    Fibonacci Sequence (5)

    Romanesque brocolli (Roman cauliflower) is a strikingexample of the Fibonacci.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence ( )

  • 7/29/2019 Pds Pb Function 2013

    34/49

    Fibonacci Sequence (6)

    Spiral aloe. Numerous such cactus display the Fibonaccispiral.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (7)

  • 7/29/2019 Pds Pb Function 2013

    35/49

    Fibonacci Sequence (7)

    Nautilus shell displays Fibonacci spiral.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (8)

  • 7/29/2019 Pds Pb Function 2013

    36/49

    Fibonacci Sequence (8)

    Cancer cell division (mitosis) [Imaging by time-lapse microscopy.

    The DNA is shown in red, and the cell membrane is shown in cyan.]

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (9)

  • 7/29/2019 Pds Pb Function 2013

    37/49

    Fibonacci Sequence (9)

    Inductive Definition

    fibn

    =

    n if 0 n < 2

    fibn1 + fibn2 if n 2.

    And its direct coding results to:

    int fibr(int n){ // efficient?

    if(n < 2) return n;

    return fibr(n-1) + fibr(n-2);}

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (10)

  • 7/29/2019 Pds Pb Function 2013

    38/49

    Fibonacci Sequence (10)

    Recursion

    Tree(n = 5)

    +

    +

    +

    +

    fibr(1)

    fibr(2)

    fibr(3)

    fibr(4)

    fibr(5

    1 1

    1

    2

    3

    4

    5

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (11)

  • 7/29/2019 Pds Pb Function 2013

    39/49

    Fibonacci Sequence (11)

    +

    +

    +

    +

    fibr(1)

    fibr(2)

    fibr(3)

    fibr(4)

    fibr(5

    1

    fibr(0)

    0

    1

    2

    3

    4

    5

    21

    3

    6

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (12)

  • 7/29/2019 Pds Pb Function 2013

    40/49

    Fibonacci Sequence (12)

    +

    +

    +

    +

    fibr(1)

    fibr(2)

    fibr(3)

    fibr(4)

    fibr(5

    1

    fibr(0)

    0

    1

    2

    3

    4

    5

    21

    3

    6

    fibr(1)

    7

    1

    4

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (13)

  • 7/29/2019 Pds Pb Function 2013

    41/49

    Fibonacci Sequence (13)

    +

    +

    +

    +

    fibr(1)

    fibr(2)

    fibr(3)

    fibr(4)

    fibr(5

    1

    fibr(0)

    0

    1

    2

    3

    4

    5

    21

    3

    6

    fibr(1)

    7

    1

    4

    fibr(2)

    +

    fibr(1)

    1

    fibr(0)

    0

    8

    9 10

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (14)

  • 7/29/2019 Pds Pb Function 2013

    42/49

    Fibonacci Sequence (14)

    +

    + +

    1+ + +

    +

    1

    1 11

    0

    00

    fibr(0)fibr(1)

    fibr(1) fibr(1)fibr(0) fibr(1)

    fibr(0)

    fibr(1

    1

    2

    3

    4

    5 6

    7

    8

    9 10

    11

    12

    1314

    15

    fibr(2)

    fibr(2) fibr(2)fibr(3)

    fibr(3)

    fibr(5)

    fibr(4)

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (15)

  • 7/29/2019 Pds Pb Function 2013

    43/49

    Fibonacci Sequence (15)

    Why bad?

    For n = 5, #calls = 15, #additions = 17.

    Better option

    Can be done by only n 1 = 4 additions in aniterative program.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (16)

  • 7/29/2019 Pds Pb Function 2013

    44/49

    Fibonacci Sequence (16)

    Number of additions to compute fibn by the

    previous recursive function:

    n 0 1 2 3 4 5 6 fibn 0 1 1 2 3 5 8

    addn 0 0 1 2 4 7 12

    addn =

    0 if n = 0 or 1,

    addn1 + addn2 + 1= fibn+1 1 if n > 1

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (17)

  • 7/29/2019 Pds Pb Function 2013

    45/49

    S q ( )

    Why bad?

    When the recursive function is called with n asparameter, there will be n + 1 activation records(stack frames) present on the stack.

    On the contrary, for the iterative program, thereis only a constant number of variables.

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (18)

  • 7/29/2019 Pds Pb Function 2013

    46/49

    q ( )

    Non-Recursive

    int fib(int n){ // non-recursive

    int i=1, f0=0, f1=1;

    if(n < 2) return n;

    for(i=2; i

  • 7/29/2019 Pds Pb Function 2013

    47/49

    q ( )

    An Efficient Recursive Function

    We can write a recursive C function that willcompute like the iterative program. This functionhas three parameters, and let it be called as

    fib(n,0,1), where 0 and 1 are base valuescorresponding to fib(0) and fib(1).

    int fib(int n, int f0, int f1) {

    if(n == 0) return f0;

    if(n == 1) return f1;

    return fib(n-1, f1, f1+f0);}

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    Fibonacci Sequence (20)

  • 7/29/2019 Pds Pb Function 2013

    48/49

    q ( )

    #include

    int fib(int, int, int);int main() {

    int n;

    printf("Enter a non-ve integer: ");

    scanf("%d", &n);

    printf("fib(%d)=%d\n",n,fib(n,0,1));

    return 0;}

    PB | CSE IITKGP | Spring 2012-2013 PDS

    Functions Iter Rec Fibo

    5 0 11st call

    f0 f1

  • 7/29/2019 Pds Pb Function 2013

    49/49

    n

    1 12nd call

    3rd call

    4th call

    5th call

    4

    3

    2

    1

    +

    1 2

    2 3

    3 5

    Return

    5

    5

    5

    5

    PB | CSE IITKGP | Spring 2012-2013 PDS