Variable types - PowerPoint PPT Presentation

1 / 74
About This Presentation
Title:

Variable types

Description:

return fact; Termination Condition. Recursive Calll. 10 ... Write a program that receives two non-negative integers and computes their product recursively. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 75
Provided by: csta3
Category:
Tags: fact | types | variable

less

Transcript and Presenter's Notes

Title: Variable types


1
Recursion
2
Functions reminder
return-type name(arg_type1 arg_name1, arg_type2
arg_name2, ) function body return value
  • a group of variables and statements that is
    assigned a name
  • a sub-program
  • A function can call other functions

3
Return Statement - reminder
  • Return causes the execution of the function to
    terminate and returns a value to the calling
    function
  • The type of the value returned must be the same
    as the return-type defined for the function

4
Scope of variables - reminder
  • A variable declared within a function is
    unrelated to variables declared elsewhere
  • A function cannot access variables that are
    declared in other functions

5
Func. declaration - reminder
return-type name(arg_type1 arg_name1, arg_type2
arg_name2, )
  • We can call a function starting from the point in
    the file in which it was declared, and until the
    end of the file

6
The functions Stack
g() -gth()
f() -gtg()
main -gt f()
7
Factorial
  • As we saw, n! 123 (n-1)n
  • Thus, we can also define factorial the following
    way
  • 0! 1
  • n! n(n-1)! for ngt0

n
8
A recursive definition
  • C functions can also call themselves!
  • However, usually not with the same parameters
  • Some functions can be defined using smaller
    occurrences of themselves.
  • Every recursive function has a termination
    condition. The function stops calling itself
    when it is satisfied.

9
Example - factorial
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)
  • int factorial(int n)
  • int fact 1
  • while (n gt 1)
  • fact n
  • n--
  • return fact

10
Example - factorial
factRec(1)
factRec(2) -gt2factRec (1)
factRec(3) -gt3factRec (2)
main -gt factRec (3)
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

11
Recursive factorial step by step
FactRec(4)
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

n
4
Returns
12
Recursive factorial step by step
FactRec(4)
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

n
4
Returns
4
13
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

14
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

15
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

16
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

17
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

18
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

19
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

20
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

21
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

22
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

23
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

24
Recursive factorial step by step
  • int factRec(int n)
  • if (n0 n1)
  • return 1
  • return nfactRec(n-1)

25
Example
  • int sum_digits(int n)
  • int sum 0
  • while (n gt 0)
  • sum n10 n n/10
  • return sum
  • Given the following iterative version of
    sum-of-digits calculation
  • Find the recursive definition of this function

26
Solution
  • int sum_digit_rec(int n)
  • if (n 0)
  • return 0
  • return n10 sum_digit_rec(n/10)

27
Exercise _at_ home
  • Write a program that receives two non-negative
    integers and computes their product recursively.
  • Hint Notice that the product ab is actually
    aaa (b times).
  • How does this help us define the problem
    recursively?

28
Solution
  • int mult_rec(int a,int b)
  • if (b1)
  • return a
  • return amult_rec(a,b-1)
  • int main()
  • int num1,num2
  • printf(Please enter two non-negative
    integers)
  • scanf(d d,num1,num2)
  • printf(Their product is d,mult_rec(num1,num2)
  • return 0

29
Exercise _at_home
  • int mod(int x,int y)
  • while (x gt y)
  • x x - y
  • return x
  • Given the following iterative version of modulo
    calculation
  • Find the recursive definition of modulo

30
Solution
  • int mod_rec(int x, int y)
  • if(xlty)
  • return x
  • return mod_rec(x-y,y)

31
Example recursive strchr
  • A recursive implementation of strchr
  • Input a string and a character
  • Output pointer to the first occurrence of the
    character in the string
  • Solution strchr_rec.c

32
strchr_rec.c
  • char strchr_rec(char str, char c)
  • if (str '\0')
  • return NULL
  • if (str c)
  • return str
  • return rec_func(str1, c)

33
Exercise _at_ home
  • Write a recursive implementation of strcmp
  • Input two strings
  • Output 0 if both are equal, 1 if not
  • Write a program that accepts two strings from the
    user and checks whether they are equal

34
Solution
  • strcmp_rec.c

35
Recursive Max step by step _at_ home
  • Write a recursive maximum function
  • Input an int array its size
  • Output the maximum element
  • Go over it _at_ home for better understanding of the
    recursive call

36
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

37
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

38
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

39
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

40
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

41
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

42
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

43
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

44
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

45
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

46
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

47
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

48
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

49
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

50
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

51
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

52
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

53
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

54
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

55
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

56
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

57
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

58
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

59
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

60
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

61
max_rec step by step
  • int rec_max(int arr, int size)
  • int rest
  • if (size 1)
  • return arr0
  • else
  • rest rec_max(arr1, size-1)
  • if (arr0 gt rest)
  • return arr0
  • else
  • return rest

62
Some problems are naturally recursive.
  • For example, fibonacci series.
  • Because the nth term is the sum of the (n-1)th
    and (n-2)th terms.
  • Therefore, the recursive definition is
  • fib(1) 0 fib(2) 1. / base /
  • fib(n) fib(n-1) fib(n-2).

63
Or, in the C form
  • int fib_iter(int n)
  • int prev1 0, prev2 1,
  • current 0, count
  • for(count1countltncount)
  • current prev1 prev2
  • prev1 prev2
  • prev2 current
  • return prev1
  • int fib_rec(int n)
  • if(n 1)
  • return 0
  • if(n 2)
  • return 1
  • return fib_rec(n-1) fib_rec(n-2)

64
Recursion and Efficiency
  • The recursive form, however elegant, may be much
    less efficient
  • The number of redundant calls grow exponentially!

65
Efficient Recursive Fibonacci
  • Pass the values that were already calculated to
    the recursive function
  • This technique is called Memoization

66
Fibonacci with Memoization
  • int main(void)
  • int fibSIZE,n,i
  • fib1 0 fib2 1
  • for (i 3 i lt SIZE i)
  • fibi -1
  • printf("Please enter a non-negative number\n")
  • scanf("d",n)
  • printf("fib(d) d\n",n,fib_rec_mem(n,fib))
  • return 0

67
Fibonacci with Memoization
  • int fib_rec_mem(int n,int fib)
  • if (fibn ! -1)
  • return fibn
  • fibn fib_rec_mem(n-1,fib)
    fib_rec_mem(n-2,fib)
  • return fibn

68
Odd-Even
  • Given a function int odd(int n)
  • Return 1 on n that are odd, 0 on even n
  • Write a function int even(int n) that
  • Return 1 on n that are even, 0 on odd n
  • This is easy

69
Odd-Even
int odd(int n) if (n1) return 1 if
(even(n-1) 1) return 1 return 0
  • int even(int n)
  • if (n0)
  • return 1
  • if (odd(n-1) 1)
  • return 1
  • return 0

70
Decimal-gtBinary _at_ home
  • We want to print the binary representation of a
    decimal number.
  • Example
  • 0 -gt 0
  • 8 -gt 1000
  • 12 -gt 1100
  • 31 -gt 11111

71
Decimal-gtBinary Recursively
  • The conversion of a number d from decimal to
    binary
  • If d is 0 or 1 write d to the left and stop.
    Else
  • If d is even, write 0 to the left.
  • If d is odd, write 1 to the left.
  • Repeat recursively with floor(d/2).

72
Example d 14
d Output at the end of stage. Action at the end of stage
14 0 Insert 0 to left of output divide d by 2
7 10 Insert 1 to left of output divide d by 2 and round down
3 110 Insert 1 to left of output divide d by 2 and round down
1 1110 Insert 1 to left of output return.
73
How to implement this?
  • We want to print the binary representation of a
    decimal number.
  • dec2bin_rec.c .
  • Notice that we first call the function
    recursively and only afterwards print it (why?).

74
Solution
  • void dec2bin_rec(int d)
  • if(d 0 d 1)
  • printf("d",d)
  • return
  • / call recursively first /
  • dec2bin_rec(d/2)
  • printf("d", d2)
Write a Comment
User Comments (0)
About PowerShow.com