WEEK 10 Class Activities Lecturer’s slides.

Slides:



Advertisements
Similar presentations
WEEK 12 Class Activities Lecturer’s slides.
Advertisements

CS1010 Programming Methodology
WEEK 13 Class Activities Lecturer’s slides.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
WEEK 5 Class Activities Lecturer’s slides.
CS1020 Week 3: 29th January 2015.
CS1010 Programming Methodology
UNIT 12 UNIX I/O Redirection.
CS1010 Programming Methodology
Programming Recursion.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
What’s in a Language naming objects (variables) naming processes (methods/functions) making decisions (if) making repetitions (for, while) (recursion)
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
WEEK 9 Class Activities Lecturer’s slides.
CS1101: Programming Methodology Aaron Tan.
Rules of Integers. Positive numbers are numbers that are above zero. Negative numbers are numbers below zero.
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
CS1101: Programming Methodology Aaron Tan.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CS1010: Programming Methodology
Recursion Chapter 2 Objectives Upon completion you will be able to:
UNIT 17 Recursion.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
WEEK 4 Class Activities Lecturer’s slides.
UNIT 14 Functions with Pointer Parameters.
CS1010: Programming Methodology
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
WEEK 6 Class Activities Lecturer’s slides.
UNIT 10 Multidimensional Arrays.
WEEK 8 Class Activities Lecturer’s slides.
WEEK 1 Class Activities.
CS1020 Data Structures and Algorithms I Lecture Note #2 Arrays.
CS1101: Programming Methodology
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Concepts of Algorithms CSC-244 Unit 5 and 6 Recursion Shahid Iqbal Lone Computer College Qassim University K.S.A.
CS1010: Programming Methodology
UNIT 17 Recursion: Towers of Hanoi.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Week 12 Class Activities.
WEEK 1 Class Activities.
CS212: Data Structures and Algorithms
Chapter 19: Recursion.
Greatest Common Divisor
Greatest Common Divisor
CS1010 Programming Methodology
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion Chapter 12.
Functions in C++ Eric Roberts CS 106B January 7, 2015.
CS1010 Programming Methodology
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Exam 2 Review 1.
Hacettepe University Computer Engineering Department
Recursion.
Presentation transcript:

WEEK 10 Class Activities Lecturer’s slides

Week 10: Recursion CS1010 (AY2014/5 Semester 1)Week10 - 2© NUS  Unit #17: Sections 1 – 3  Exercise #1: Greatest Common Divisor  Exercise #2: Power function  Exercise #3: Tracing recursive codes  Unit #17: Section 4 Thinking Recursively  Exercise #4: Sum Digits  Exercise #5: Sum Array  Unit #17: Sections 5 – 9

Week 10 Programs CS1010 (AY2014/5 Semester 1)Week10 - 3© NUS  Download the programs from this web page   The files are:  Week10_GCD.c  Week10_Pow.c  Week10_SumArray.c  Week10_SumDigits.c  Week10_Trace.c  You may also copy the above files directly into your sunfire account using the following UNIX command, where xxx is the name of one of the above files: cp ~cs1010/public_html/lect/prog/2014/week10_for_students/xxx.

Unit #17: Sections 1 – 3 CS1010 (AY2014/5 Semester 1)Week10 - 4© NUS 1.Introduction 2.Two Simple Classic Example 3.Gist of Recursion

Exercise #1: Greatest Common Divisor CS1010 (AY2014/5 Semester 1)Week10 - 5© NUS  The recurrence relation for Greatest Common Divisor (GCD) of two non-negative integers a and b, not both zero, is given below:  Write a function int gcd(int a, int b) to compute the GCD of a and b. Skeleton program Week10_GCD.c is given.

Exercise #2: Power CS1010 (AY2014/5 Semester 1)Week10 - 6© NUS  The math function double pow(double x, double y) computes x y. Write your own, simpler function double mypow(double x, int n) to compute x n, where n is a non-negative integer.  Skeleton program Week10_Pow.c is given. The recurrence relation is not given, can you derive it before writing the function?

Exercise #3: Tracing CS1010 (AY2014/5 Semester 1)Week10 - 7© NUS  Given the following 2 recursive functions, trace mystery1(3902) and mystery2(3902) using the trace tree method. void mystery1(int n) { if (n>0) { printf("%d", n%10); mystery1(n/10); } void mystery2(int n) { if (n>0) { mystery2(n/10); printf("%d", n%10); } The order of statements does matter!

Unit #17: Section 4 CS1010 (AY2014/5 Semester 1)Week10 - 8© NUS Thinking recursively

Exercise #4: Sum Digits CS1010 (AY2014/5 Semester 1)Week10 - 9© NUS  Write a recursive function int sum_digits(int n) that sums up the digits in n, assuming that n is a non- negative integer.  Skeleton program Week10_SumDigits.c is given.  This exercise is mounted on CodeCrunch.  Sample runs: Enter a non-negative integer: Sum of its digits = 32 Enter a non-negative integer: 6543 Sum of its digits = 18

Exercise #5: Sum Array CS1010 (AY2014/5 Semester 1)Week © NUS  Write a program Week10_SumArray.c to read data into an integer array with at most 10 elements, and sum up all values in the array, using a recursive function.  This exercise is mounted on CodeCrunch.  Sample runs: Enter number of elements: 6 Enter 6 values: Array read: Sum = 9 Enter number of elements: 8 Enter 8 values: Array read: Sum = 166

Unit #17: Sections 5 – 9 CS1010 (AY2014/5 Semester 1)Week © NUS 5.Auxiliary Function 6.Types of Recursion 7.Tracing Recursive Codes 8.Recursion versus Iteration 9.Towers of Hanoi

Things-To-Do CS1010 (AY2014/5 Semester 1)Week Continue to do practice exercises on CodeCrunch © NUS

End of File CS1010 (AY2014/5 Semester 1)Week © NUS