5. Function (2) and Exercises

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Chapter 2 Algorithm Analysis
Information and Computer Sciences University of Hawaii, Manoa
4. Java language basics: Function
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
User Interaction and Variables
Chapter 15 Recursion.
While loop statement condition list
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
Lesson #6 Modular Programming and Functions.
Introduction to Python
Analysis of Algorithms
Lesson #6 Modular Programming and Functions.
2. Java language basics (2)
Chapter 15 Recursion.
Methods Chapter 6.
Foundations of Programming: Arrays
CS1371 Introduction to Computing for Engineers
Variables, Expressions, and IO
Intro to C Tutorial 4: Arithmetic and Logical expressions
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Algorithm Analysis CSE 2011 Winter September 2018.
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Lesson #6 Modular Programming and Functions.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Applied Algorithms (Lecture 17) Recursion Fall-23
Arrays, For loop While loop Do while loop
Unit 2 Programming.
Algorithm design and Analysis
Chapter 13 Vector of Vectors (2D Arrays)
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Recursion Data Structures.
<INSERT_WITTY_QUOTE_HERE>
Repetition Structures
Java Basics.
Unit 3 Test: Friday.
Building Java Programs
Arrays.
Building Java Programs
Lesson #6 Modular Programming and Functions.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Introduction to Programming
Review Lab assignments Homework #3
ITM 352 Functions.
SPL – PS1 Introduction to C++.
Introduction to Computer Science
Presentation transcript:

5. Function (2) and Exercises Minhaeng Lee

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

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

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

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

Recursion Example : Factorial

Recursion Exercise : Fibonacci Wikipedia.org

Write Fibonacci Iterative way Recursive Way

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

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

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.

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>();

Vector Example Type it and see how it works

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

Exercise: Big number calculator

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

Big number calculator 1 2 3 4 5 6 8 3

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

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

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

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

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

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

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

Exercise Time! Write Big Number Calculator for bigAdd Use this template https://github.com/mhlee1215/java_class/blob/master/CodingExercise/src/BigNumberCalculatorTemp.java 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

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