Presentation is loading. Please wait.

Presentation is loading. Please wait.

5. Function (2) and Exercises

Similar presentations


Presentation on theme: "5. Function (2) and Exercises"— Presentation transcript:

1 5. Function (2) and Exercises
Minhaeng Lee

2 Random Number Generate random number between 0 to 1.0
Math.random(); Generate random number between 0 to N (int)(Math.random() * N); Exercise: Try to print 10 times of randomly generated numbers using loop statement

3 Exercise : coin gambling
Make function that return true/false Inside of the function, do random between 0 to 1, if the value is less than 0.5 then return true, otherwise return false Run 1000 times, then count true and false

4 Factorial Factorial Define function Exercise : Using loop statement
N! = N*(N-1)*(N-2)*… Define function Exercise : Using loop statement

5 Recursion of recursive call
Call function itself Need termination phrase Sometimes very convenient Not memory efficient

6 Recursion Example : Factorial

7 Recursion Exercise : Fibonacci
Wikipedia.org

8 Write Fibonacci Iterative way Recursive Way

9 Function Overloading To make functions Same name but different body
Why? Sometimes, we need make multiple functions have similar name for program simplicity Example : add functions using integer input and String inputs. They should have similar name but different body to process different inputs For overloading, at least one of parameters must be different to the others

10 Overloading. When do we need?
They have similar function name but parameter types are different.

11 Overloading Example They MUST have at least one parameter which has different TYPE (not name) Because a function is distinguished with function name and parameter types.

12 Useful library Vector Array has size limit (defined at the first time)
But sometimes, we cannot expect its optimized size By using Vector, we don’t have to think about it Vector<Type> <variable Name> = new Vector<Type>();

13 Vector Example Type it and see how it works

14 Vector Example Type specified
Type it and see how it works Type cannot be primitive types. Use Integer instead of int, Double instead of double, Float instead of float

15 Exercise: Big number calculator

16 Exercise: Big number calculator
How can we store a number over the memory size of variable types? We knows the upper-bound of variables Store each digit of input number as individual letter Then compute add, minus, multi, divide as we’ve learned in elementary school

17 Big number calculator 1 2 3 4 5 6 8 3

18 Big number calculator 1 2 3 4 5 6 8 3 8

19 Big number calculator 1 2 3 4 5 6 8 3 12 8

20 Big number calculator 1 2 3 4 5 6 8 3 1 2 8 Carry

21 Big number calculator 1 2 3 4 5 6 8 3 1 2 8 10

22 Big number calculator 1 2 3 4 5 6 8 3 1 2 8 1 Carry

23 Big number calculator 1 2 3 4 5 6 8 3 1 2 8 1 Carry 3

24 Big number calculator 1 2 3 4 5 6 8 3 1 2 8 1 1 3 1 3 2 8

25 Exercise Time! Write Big Number Calculator for bigAdd
Use this template Reverse ordering makes it easy to compute Because human being and machine have different order of numbers Human being: right to left Machine: left to right

26 Homework Step1. Complete bigSub Step2. Complete {bigMul or bigDiv}
Integrate with interactive calculator


Download ppt "5. Function (2) and Exercises"

Similar presentations


Ads by Google