Recursion.

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Kernighan/Ritchie: Kelley/Pohl:
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 Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
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.
Functions, Pointers, Structures Keerthi Nelaturu.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
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 =
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
Pointers and Dynamic Arrays
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Computer Skills2 for Scientific Colleges
Recursion.
CSE 303 Lecture 9 C programming: types, functions, and arrays
C Functions -Continue…-.
Command Line Arguments
EPSII 59:006 Spring 2004.
INC 161 , CPE 100 Computer Programming
Instructor: Ioannis A. Vetsikas
Recursion.
Dynamic memory allocation and Intraprogram Communication
Pointers and Arrays Chapter 12
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.
INC 161 , CPE 100 Computer Programming
Computer Skills2 for Scientific Colleges
Local Variables, Global Variables and Variable Scope
Pointers.
prepared by Senem Kumova Metin modified by İlker Korkmaz
Pointers (continued) Chapter 11
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
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.
Java Programming: Chapter 9: Recursion Second Edition
C++ Pointers and Strings
Variables, Pointers, and Arrays
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
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 excessive 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”;

Pointer a variable that contains an address value Usage NULL == 0 == FALSE Usage p = & a; b = *p; v == *&v (for any variable v) p == &*p (if p is a pointer) think about a swap function without pointers

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 (p+4 for an integer) int i; /* 4 bytes integer */ int a[ ]; /* array or pointer */ a[i] == *(a+i) (a + i) == &a[i]

more about pointers next week