Recursion.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Introduction to C Programming CE
TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of.
1 Review of Chapter 6: The Fundamental Data Types.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Storage Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters.
Functions, Pointers, Structures Keerthi Nelaturu.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5.
CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
A First Book of ANSI C Fourth Edition
Windows Programming Lecture 03. Pointers and Arrays.
Pointers and Dynamic Arrays
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Computer Skills2 for Scientific Colleges
C Functions -Continue…-.
Command Line Arguments
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
INC 161 , CPE 100 Computer Programming
Instructor: Ioannis A. Vetsikas
Recursion.
Dynamic memory allocation and Intraprogram Communication
Data Type.
CSC 253 Lecture 8.
Scope, Parameter Passing, Storage Specifiers
5. Arrays, Pointers and Strings
Pointers.
Pointers.
CSC 253 Lecture 8.
Data Type.
Pointers  Week 10.
Dynamic Memory Allocation
Pointers.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Computer Skills2 for Scientific Colleges
Recursion.
Local Variables, Global Variables and Variable Scope
Pointers.
prepared by Senem Kumova Metin modified by İlker Korkmaz
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Pointers (continued) Chapter 11
CS111 Computer Programming
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C++ Pointers and Strings
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 9: Pointers and String
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Data Type.
Programming Languages and Paradigms
Arrays, Pointers, and Strings
C++ Pointers and Strings
Pointers.
C Programming Lecture-17 Storage Classes
Programming fundamentals 2 Chapter 3:Pointer
Structures, Unions, and Enumerations
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Recursion

Review role of functions scoping rules storage classes auto enforces the scoping rule

Recursion A function calls itself. A recursive function can be converted to an iterative one

Recursion Recursion is said to be more elegant and requires fewer variables, but function calls are costly in time and space.

Recursion Fibonacci numbers Exponential increase in function calls fi+1 = fi + fi-1 Exponential increase in function calls

Recursion iterative version

write backward /* Write a line backwards. */ #include <stdio.h> void wrt_it(void); int main(void) { printf("Input a line: "); wrt_it(); printf("\n\n"); return 0; } void wrt_it(void) { int c; if ((c = getchar()) != '\n') wrt_it(); putchar(c); }

void wrt_it(void) { int c; if ((c = getchar()) != '\n') wrt_it(); putchar(c); }

Arrays, Pointers, and Strings

One Dimensional Array An array is a set of subscripted variables of the same type In C, arrays and pointers are interrelated array name is a pointer pointer parameters can implement “call-by-reference” arrays can be initialized int a[100] = {0}; external or static arrays are initialized to zero by default array declaration without size int a[] = {2, 3, 4, 7}; char s[] = “abc”;

Pointers address pointers take addresses as values Usage NULL == 0 == FALSE Usage p = & a; b = *p; v == *&v (for any variable v) p == &*p (if p is a pointer)

call-by-reference

Arrays and Pointers If the variable p is a pointer to a type(say, integer) then p+1 yields the address for the next variable of that type int i; /* 4 bytes integer */ int a[ ]; /* array or pointer */ a[i] == *(a+i) (a + i) == &a[i]