CS1010E Programming Methodology Tutorial 5 Macros and Recursion C14,A15,D11,C08,C11,A02.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

C++ Programming:. Program Design Including
Lecture 12 Recursion part 1
MATH 224 – Discrete Mathematics
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Monte Carlo Simulation A technique that helps modelers examine the consequences of continuous risk Most risks in real world generate hundreds of possible.
CSE 373 Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III.
Dynamic Programming.
Probability Distributions
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Csci1300 Introduction to Programming Recursion Dan Feng.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS1010E Programming Methodology Tutorial 3 Control Structures and Data Files C14,A15,D11,C08,C11,A02.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
CS1010E Programming Methodology Tutorial 1 Basic Data Type and Input/output, Characters and Problem Solving C14,A15,D11,C08,C11,A02.
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
CS1010E Programming Methodology Tutorial 6 Arrays and Search C14,A15,D11,C08,C11,A02.
Algebra Concepts Chapter 3 Notes Note: We will only cover sections 3-1 and 3-3.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
Recursion Unit 15. Recursion: Recursion is defined as the process of a subprogram calling itself as part of the solution to a problem. It is a problem.
Risk Analysis Simulate a scenario of possible input values that could occur and observe key financial impacts Pick many different input scenarios according.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
COP INTERMEDIATE JAVA Recursion. The term “recursion” refers to the fact that the same computation recurs, or occurs repeatedly, as a problem is.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Lecture #3 Analysis of Recursive Algorithms
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
User-Written Functions
Chapter 15 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Chapter 15 Recursion.
Week 12 - Wednesday CS221.
Introduction to Computer Science - Alice
Algorithm design and Analysis
CSE 1342 Programming Concepts
Recursion Data Structures.
Recursion.
This Lecture Substitution model
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Mathematical Background 2
CS148 Introduction to Programming II
This Lecture Substitution model
Parameter transmission
This Lecture Substitution model
Parameter transmission
Parameter transmission
Presentation transcript:

CS1010E Programming Methodology Tutorial 5 Macros and Recursion C14,A15,D11,C08,C11,A02

Question 1

Breakdown the problem into small parts: For single experiment, we need a function to compute the return double futureValue( double lower, double upper, double initial) For simulation, we need to run 1000 exps and count how many percent will can earn double proportionHigher(double lower, double upper, double initial)

Question 1 Main function. We assume proportionHigher() is already implemented

Question 1 proportionHigher We assume futureValue is implemented!

Question 1 futureValue Note the trick of computing random double in range [lower, upper]

Question 1 When writing main function, we assume proportionHigher is written and works correctly When writing proportionHigher, we assume futureValue is written and works correctly This kind of thinking method is called wishful thinking Assume some functions are ready, you can focus more on how to use them to solve problem. If those function aren’t really implemented, we then implement them!

Question 2 Using SWAP to change two variables: Standard code for changing (x, y): t=x; x=y; y=t; Macro Version: #define SWAP(x,y) int t=x;x=y;y=t; Not correct, due to int t How to swap two variable without using temp variables ?

Question 2 Function Version: Will it work by calling: ? x = 10; y = 20; swap(x, y); Notice the Pass-By-Value nature of functions!!

Question 2(b) Find maximum value of 3 numbers: #define MAX(A,B) ((A) > (B) ? (A) : (B)) max=MAX(MAX(a, b), c) Note that parenthesis are important !! Macros vs. Functions? Macros has no local variables, easily mess up Program size increases Normally only 1 line No wishful thinking can be used, has to know implementation before calling Why do we use Macro?

Recursion When function calls itself, it is called recursion Problem solving with recursion Needs to identify that if a similar problem with smaller input value is solvable ? If it so, can we use that to solve our problem? It is essentially a special case of wishful thinking Recursive Program: A baseline (stopping condition) A recursive formula How to solve bigger problem using similar small problems

Question 3 (a)

Question 3 (b)

Question 3 (c)

Question 3 Exponent 1Exponent 2Exponent