1 / 16

CS1010E Programming Methodology Tutorial 5 Macros and Recursion

CS1010E Programming Methodology Tutorial 5 Macros and Recursion. C14,A15,D11,C08,C11,A02. Question 1. Analysis: We need to simulate our investment return Simulation is done by run an experiment 1000 times An experiment is based on the formula: is the interest rate at year , it is in .

korene
Download Presentation

CS1010E Programming Methodology Tutorial 5 Macros and Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1010E Programming MethodologyTutorial 5Macros and Recursion C14,A15,D11,C08,C11,A02

  2. Question 1 • Analysis: • We need to simulate our investment return • Simulation is done by run an experiment 1000 times • An experiment is based on the formula: • is the interest rate at year , it is in

  3. 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)

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

  5. Question 1 • proportionHigher • We assume futureValue is implemented!

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

  7. Question 1 • When writing main function, we assume proportionHigheris 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!

  8. 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) intt=x;x=y;y=t; • Not correct, due to int t How to swap two variable without using temp variables ?

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

  10. 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?

  11. 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

  12. Question 3 (a) • Find exponent • Given x, and non-negative integer k, find • Loop version:

  13. Question 3 (b) • Recursive version: • Analyze: • If we know , we know how to compute • Computing and are similar problem • Recursive formula: • Baseline ?

  14. Question 3 (c) • Analyze: • There could be multiple recursive formula: • We can alternative compute by considering parity • Example • So we can use the formula: • Baseline:

  15. Question 3 • Now let’s look at efficiency: • Efficiency is determined by number of operations performed in computing • Here we count number of multiplications used forand

  16. Thank you See you next week!!

More Related