Upload
korene
View
53
Download
0
Embed Size (px)
DESCRIPTION
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 . - PowerPoint PPT Presentation
Citation preview
CS1010E Programming MethodologyTutorial 5
Macros and Recursion
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
returndouble 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
proportionHigherWe assume futureValue is implemented!
Question 1
futureValueNote 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)
• 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
Exponent 1 Exponent 2 Exponent 3
13 13 5
128 128 8
Thank you See you next week!!