Investment Return Simulation and Macro Recursion in Programming Methodology
In this tutorial, we explore the concept of simulating investment returns by running experiments multiple times. This involves breaking down the problem into smaller functions, such as computing future values and the proportion of higher returns. We analyze how to swap variables using macros and functions, highlighting the differences between them. Additionally, we delve into recursion, including how to compute exponent values and optimize recursive solutions for efficiency. Practical coding techniques and examples enhance understanding of these core programming concepts.
Investment Return Simulation and Macro Recursion in Programming Methodology
E N D
Presentation Transcript
CS1010E Programming MethodologyTutorial 5Macros 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
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 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!
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 ?
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) • Find exponent • Given x, and non-negative integer k, find • Loop version:
Question 3 (b) • Recursive version: • Analyze: • If we know , we know how to compute • Computing and are similar problem • Recursive formula: • Baseline ?
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:
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
Thank you See you next week!!