1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)

Slides:



Advertisements
Similar presentations
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
Advertisements

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Recursion. Binary search example postponed to end of lecture.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
1 CSE1301 Computer Programming Lecture 9 Selection.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
Csci1300 Introduction to Programming Recursion Dan Feng.
ICS103 Programming in C Lecture 11: Recursive Functions
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
1 Recursion Overview l Introduction to recursion and recursive methods l Simple popular recursive algorithms l Writing recursive methods l Preview: 1-D.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
FIT FIT1002 Computer Programming Unit 20 Recursion.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Recursion. Definition Recursion is a function calling on itself over and over until reaching an end state. One such example is factorial. 10! = 10 * 9.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ASET RECURSION. ASET RECURSIVE FUNCTIONS A recursive function is a function that calls itself to solve a smaller version of its task until a final call.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Recursive Function Computer Programming. Recursive Function Recursive function is a function that calls itself There is nothing special about calling.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
CPT: Functions/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to describe C functions –illustrate recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CGS 3460 Function – I n What is a function lOne named program module that does one primary task lConsists of a set of statements n Why we need functions?
Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Recursion Function calling itself
Computer Programming Techniques Semester 1, 1998
5.13 Recursion Recursive functions Functions that call themselves
Computer Science 4 Mr. Gerb
Java 4/4/2017 Recursion.
Programming application CC213
Recursion Recursion is a math and programming tool
Functions Recursion CSCI 230
CSI 121 Structure Programming Language Lecture 9 Selection
Module 1-10: Recursion.
Yan Shi CS/SE 2630 Lecture Notes
Main() { int fact; fact = Factorial(4); } main fact.
ICS103 Programming in C Lecture 11: Recursive Functions
ITEC324 Principle of CS III
Presentation transcript:

1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)

2 Topics Recursion Unary Recursion N-ary Recursion

3 Recursion Queue processing Sorting Searching

4 What is Recursion? A procedure defined in terms of (simpler versions of) itself Components: –Base case –Recursive definition –Convergence to base case

5 Example: Queue processing procedure ProcessQueue ( queue ) { if ( queue not empty ) then { process first item in queue remove first item from queue ProcessQueue ( rest of queue ) }

6 Example: Queue processing procedure ProcessQueue ( queue ) { if ( queue not empty ) then { process first item in queue remove first item from queue ProcessQueue ( rest of queue ) } Base case: queue is empty

7 Example: Queue processing procedure ProcessQueue ( queue ) { if ( queue not empty ) then { process first item in queue remove first item from queue ProcessQueue ( rest of queue ) } Recursion: call to ProcessQueue

8 Example: Queue processing procedure ProcessQueue ( queue ) { if ( queue not empty ) then { process first item in queue remove first item from queue ProcessQueue ( rest of queue ) } Convergence: fewer items in queue

9 Unary Recursion Functions calls itself once (at most) Usual format: function RecursiveFunction ( ) { if ( ) then return else return RecursiveFunction ( ) } Winding and unwinding the “stack frames”

10 Example: Factorial n! = n  (n - 1)  (n - 2) ...  2  1 0! = 1 Example: 5! = 5  4  3  2  1 = 120 Given n  0:

11 Example: Factorial Problem: Write a recursive function Factorial(n) which computes the value of n! Base Case: If n = 0 or n = 1: Factorial(n) = 1

12 Example: Factorial Recursion: n! = n  (n - 1)  (n - 2) ...  2  1 (n - 1)! If n > 1: Factorial(n) = n  Factorial(n - 1)

13 Example: Factorial Convergence: Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

14 Example: Factorial The Factorial function can be defined recursively as follows: Factorial(0) = 1 Factorial(1) = 1 Factorial(n) = n  Factorial(n - 1)

15 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial

16 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Base case

17 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial General Case

18 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Recursion

19 function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Convergence

20 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

21 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

22 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  12 

23 Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  12 

24 Example: Factorial Factorial(4) Factorial(3)4  23 

25 Example: Factorial Factorial(4) Factorial(3)4  23 

26 Example: Factorial Factorial(4) 64 

27 Example: Factorial Factorial(4) 64 

28 Example: Factorial 24

29 /* Compute the factorial of n */ int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial(n-1); } Computes the factorial of a number function Factorial ( n ) { if ( n is less than or equal to 1) then return 1 else return n  Factorial ( n - 1 ) } Example: factorl.c

30 Example: “Frames” during calculation of factorial(4) printf(“%d”, factorial(4));

31 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n:

32 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n:

33 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } n:

34 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3

35 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } n:

36 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 1 2 n: 2 2

37 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 1 2 n: 2 2 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 1 1 n:

38 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 1 2 n: 2 2 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 1 1 n: return 1 ; Base case: factorial(1) is 1

39 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 1 2 n: 2 2 1

40 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } n: 1

41 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ) ; } 2 2 n: 2 factorial(2) is 2

42 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 3 2

43 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 3 3 n: 3 3 2

44 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } 2 3 n: 3 6 factorial(3) is 6

45 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: 6

46 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n: 6

47 printf(“%d”, factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) 4 4 n: 24 factorial(4) is 24

48 Example: “Frames” during calculation of factorial(4) printf(“%d”, factorial(4)); 24 Output: 24

49 #include #include “factorl.c” /* Main program for testing factorial() function */ int main(void) { int n; printf("Please enter n: "); scanf("%d", &n); printf("%d! is %d\n", n, factorial(n)); return 0; } Example: testprog.c

50 N-ary Recursion Sometimes a function can only be defined in terms of two or more calls to itself. Efficiency is often a problem.

51 Example: Fibonacci A series of numbers which –begins with 0 and 1 –every subsequent number is the sum of the previous two numbers 0, 1, 1, 2, 3, 5, 8, 13, 21,... Write a recursive function which computes the n-th number in the series (n = 0, 1, 2,...)

52 Example: Fibonacci The Fibonacci series can be defined recursively as follows: Fibonacci(0) = 0 Fibonacci(1) = 1 Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

53 /* Compute the n-th Fibonacci number, when=0,1,2,... */ long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } function Fibonacci ( n ) { if ( n is less than or equal to 1 ) then return n else return Fibonacci ( n - 2 ) + Fibonacci ( n - 1 ) } Example: fibonacc.c

54 Example: Computation of fib(4) + fib(2)fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

55 Example: Computation of fib(4) fib(2)fib(3) + fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } fib(0)fib(1)

56 Example: Computation of fib(4) fib(2)fib(3) + fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } fib(0)fib(1) 0

57 Example: Computation of fib(4) fib(2)fib(3) + fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } fib(1) 0

58 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(1)0 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

59 Example: Computation of fib(4) fib(2)fib(3) + fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } fib(1)0 1

60 Example: Computation of fib(4) fib(2)fib(3) + fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

61 Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

62 Example: Computation of fib(4) 1fib(3) + fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

63 Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

64 Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } fib(1)fib(2)

65 Example: Computation of fib(4) + fib(3) fib(4) fib(1)fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } 1 1 1

66 Example: Computation of fib(4) + fib(3) fib(4) fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } 1 1 1

67 Example: Computation of fib(4) + fib(3) fib(4) fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

68 Example: Computation of fib(4) + fib(3) fib(4) fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } fib(0)fib(1)

69 Example: Computation of fib(4) + fib(3) fib(4) fib(2) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } 0 0 0

70 Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 0 fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } 0 0 0

71 Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 0 fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

72 Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 0fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } 1 1 1

73 Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } 1 1 1

74 Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

75 Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

76 Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

77 Example: Computation of fib(4) + 2 fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

78 Example: Computation of fib(4) + 2 fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

79 Example: Computation of fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

80 Example: Computation of fib(4) Thus, fib(4) returns the value 3.

81 int main(void) { long number; printf("Enter number: "); scanf("%ld", &number); printf("Fibonacci(%ld) = %ld\n", number, fib(number)); return 0; } Example: fibonacc.c Sample main() for testing the fib() function:

82 Reading King Chapter 9 Section 9.6 D&D Chapter 5 Sections 5.13 to 5.14 Kernighan & Ritchie Chapter 4 Section 4.10