Extra Practice for Recursion

Slides:



Advertisements
Similar presentations
Lab 8 User Defined Function.
Advertisements

TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
ICS103 Programming in C Lecture 11: Recursive Functions
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
M180: Data Structures & Algorithms in Java
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
Hopefully this lesson will give you an inception of what recursion is.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
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.
More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program.
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
UMBC CMSC 104 – Section 01, Fall 2016
User-Written Functions
“Studying C programming excluding pointers is meaningless.” d0m3z
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Functions and Pointers
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CS1010 Programming Methodology
Module 4 Functions – function definition and function prototype.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Pointers.
CHAPTER 7 RECURSIVE FUNCTION
Pointers.
Functions and Pointers
Formatted and Unformatted Input/Output Functions
Recursive functions.
Lesson #6 Modular Programming and Functions.
Recursion Output Input
Compiled and ready to run Memory Stack /*
Pointers.
Arrays & pointers C How to Program, 8/e.
JavaScript: Functions Part II
Lec 7.
Pointers  Week 10.
Programming application CC213
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
Functions Recursion CSCI 230
Functions, Part 2 of 3 Topics Functions That Return a Value
Pointers Call-by-Reference CSCI 230
A function with one argument
Qsort.
CS1201: Programming Language 2
Lesson #6 Modular Programming and Functions.
Conversion Check your class notes and given examples at class.
In C Programming Language
CS148 Introduction to Programming II
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Recursion.
Main() { int fact; fact = Factorial(4); } main fact.
ICS103 Programming in C Lecture 11: Recursive Functions
Assignment Operators Topics Increment and Decrement Operators
CSCE 206 Lab Structured Programming in C
FUNCTION ||.
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to Pointers
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

Extra Practice for Recursion

Recursion is simply the calling of a function by itself. We know that we can call a function from within a function ,That function could the function itself. Recursion is very powerful and very easy to understand but hard to program and debug.

Q? /* CALCULATE FACTORIAL OF AN INTEGER NUMBER USING RECURSIVE FUNCTION */

/* PROGRAM # 42 */ /* Recursion:A function calling itself*/ /* CALCULATE FACTORIAL OF AN INTEGER NUMBER USING RECURSIVE FUNCTION */ #include <stdio.h> int factorial(int num); main( ){ int var, result; printf("Enter a number: "); scanf("%d",&var); result=factorial(var); printf("\n%d! equals %5d", var, result); } /* Recursive function gets an integer number, calls itself until num gets 0 ,returns the result */ int factorial(int num){ if (num == 0) return 1; else return num * factorial(num - 1);

How is the recursion working? We discuss this by a way of an example. In the previous program, if the user inputs 4 as the value of the var then the following would happen: Main program calls factorial(4) factorial(4) calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) calls factorial(0) factorial(0) returns 1 factorial(1) returns 1  1 factorial(2) returns 2  1  1 factorial(3) returns 3  2  1  1 factorial(4) returns 4  3  2  1  1

A recursive function -10 int multiply (int m, int n) { int answer; if (n == 1) answer = m; else answer = m + multiply (m, n-1); return (answer); }

/* PROGRAM #57 */ /* num3=num1*num2-num3 */ #include<stdio.h> void point(int var, int *ptr2, int *ptr3); main( ){ int num1=1, num2=2, num3=3; int *pnum2, *pnum3; printf(“num1, num2 and num3 are: %d %d %d\n”, num1, num2, num3); pnum2=&num2; pnum3=&num3; point(num1, pnum2, pnum3); /* Call the function point and display the result */ } /* Function point, uses pointers to do the calculation*/ void point(int var, int *ptr2, int *ptr3){ int tmp1, tmp2; tmp1=var* (*ptr2); tmp2=tmp1- (*ptr3); *ptr3=tmp2; RUN: num1, num2 and num3 are: 1 2 3 num1, num2 and num3 are: 1 2 -1

GAME?-8 /* Problem: This is just a silly program playing with pointers */ #include <stdio.h> int main (void) { /* a and e are integers */ int a, e; /* b is a pointer to an integer */ int* b; /* c is a pointer to a pointer to an integer */ int** c; /* d is a pointer to a pointer to a pointer to an integer */ int*** d; a = 25; /* a contains the integer 25 */ b = &a; /* b contains the address of a */ c = &b; /* c contains the address of b */ d = &c; /* d contains the address of c */ /* Do you understand that ***d is actually a? */ e = ***d * 2; printf ("%d", e); return (0); }