Dec. 8 2010. Important Steps 1. Precise Specification What does the method do? What are the preconditions? 2. Write the base case What is the most basic.

Slides:



Advertisements
Similar presentations
Thinking Mathematically
Advertisements

May 11, What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification.
Dec 7, What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification.
1 Computing Functions with Turing Machines. 2 A function Domain: Result Region: has:
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Number System and Codes
Binary Numbers.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Simple Data Type Representation and conversion of numbers
Fractions and Decimals
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Computer Science 111 Fundamentals of Programming I Number Systems.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Integer Conversion Between Decimal and Binary Bases Conversion of decimal to binary more complicated Task accomplished by –Repeated division of decimal.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
1 Week 2: Binary, Octal and Hexadecimal Numbers READING: Chapter 2.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Section 2.6 Representation of numbers. Decimal representation (base 10) Given a positive integer X, the decimal representation of X is a string of digits.
Recursion Pepper. Recursion Definition English –procedure that can repeat a version of itself indefinitely – such as mirrors looking at each other. Math.
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
CS 206 Introduction to Computer Science II 02 / 23 / 2009 Instructor: Michael Eckmann.
NUMBER SYSTEMS TWSSP Wednesday. Wednesday Agenda Finish our work with decimal expansions Define irrational numbers Prove the existence of irrationals.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
1 Positive numbers are well understood An n-bit number represents numbers from 0 to 2 n -1 n+m bits can be used to represent n-bit integer and m-bit fraction.
Addition and Substraction
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
1 Recursion Recursive Thinking Recursive Programming Recursion versus Iteration Direct versus Indirect Recursion Reading L&C 3 rd : 7.1 – nd :
IT1004: Data Representation and Organization Negative number representation.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
$100 $200 $300 $400 $100 $200 $300 $400 $300 $200 $100 Adding multiples of 10 Add 1 to 2 digit numbers with regrouping Add 3 two digit numbers Ways.
Rational Numbers SWBAT identify rational numbers; identify rational numbers as termination or nonterminating repeating decimal; locate rational numbers.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
Remarks on Fast Exp (4/2) How do we measure how fast any algorithm is? Definition. The complexity of an algorithm is a measure of the approximate number.
CONVERTING TO RATIONAL NUMBERS. Whole numbers are 0, 1, 2, 3, 4, … Integers include all the whole numbers and also their negative versions: …, -3, -2,
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
1 Computing Functions with Turing Machines. 2 A function Domain Result Region has:
8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists.
Prof. I. J. Chung Data Structure #1 Professor I. J. Chung.
1 Advanced Programming Examples. using System; class Test { static void Main( string[] args ) { int a = 7; int b = 3; int c = 5; int d = 9; Console.WriteLine(!(d.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
Recitation3: CS 3843 Computer Organization 4th week
Data Representation Binary Numbers Binary Addition
2.5 Another Java Application: Adding Integers
Fractions to Decimals.
Rational Numbers SWBAT define the set of rational numbers; identify subsets of rational numbers; write rational numbers in equivalent forms.
Computing Functions with Turing Machines
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Computing Functions with Turing Machines
Objective - To add and subtract decimals.
CPS120: Introduction to Computer Science
Recursion Based on slides by Alyssa Harding
Convert a TERMINATING DECIMAL to a FRACTION
slides created by Alyssa Harding
Real Numbers Natural Numbers Whole Numbers Integers Rational Numbers
Prof. Giancarlo Succi, Ph.D., P.Eng.
Recursion Method calling itself (circular definition)
Java
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
COMS 161 Introduction to Computing
CSE 1020:Software Development
Number Systems Unit Review Day 1.
slides created by Alyssa Harding
Presentation transcript:

Dec

Important Steps 1. Precise Specification What does the method do? What are the preconditions? 2. Write the base case What is the most basic case? What causes termination of the recursive method? 3. Write the recursive case How do we make progress toward termination? Is your computation correct?

What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification (note preconditions) Given a problem description and specification: Write the recursive base case Write the recursive call Verify that it is correct

Writing Specifications Write a specification for a Method that: 1. Converts an input integer to a string representation with commas. ie is converted to 5,923,821. /** = String representation of integer with commas added*/ 2. Compute the complement of a positive integer. ie. The complement of is /** = the complement of n, formed by replacing each decimal digit of n by 10-n. ie. the result for the integer is Precondition: n > 0 and no digit of n is 0 */

Writing Specifications Write a specification for a Method that: 3. Calculate the number of digits in an input integer. /** = number of digits in integer n. */ 4. Reduce the positive input integer to a single digit. ie > = 21 -> 2+1 = 3 /** = n reduced to a single digit (by repeatedly summing its digits). Precondition: n > 0 */

/** = String representation of integer with commas added*/ public static String addCommas(int n) { // Base case if (n < 1000) return “” + n; // Recursive Case String number = "" + n; return addCommas (n/1000) + ",“ + number.substring(number.length()-3); } Problem: Properly add commas to an integer and return the string representation. ie is converted to 5,923,821.

/** = String representation of integer with commas added*/ public static String addCommas(int n) { if (n < 0) return "-" + addCommasHelper(-n); else return addCommasHelper(n); } /** = String representation of a positive integer with commas added. Precondition: n >= 0*/ private static String addCommasHelper(int n) { // Base case if (n < 1000) return “” + n; // Recursive Case String number = "" + n; return addCommasHelper(n/1000) + ",“ + number.substring(number.length()-3); } Problem: Properly add commas to an integer and return the string representation. ie is converted to 5,923,821.

Complement of an Integer /** = the complement of n, formed by replacing each decimal digit of n by 10-n. ie. the result for the integer is Precondition: n > 0 and no digit of n is 0 */ public static int complement(int n) { // Base Case if (n < 10) return 10 - n; // Recursive Case return complement(n/10) * 10 + (10 - n%10); }

Spring 2009 Prelim 3 /** = number of digits for n. */ public static int length(int n) { // Base case if (n == 0) return n; // Recursive Case return 1 + length(n/10); }

Spring 2008 Prelim 3 /** = n reduced to a single digit (by repeatedly summing its digits). Precondition: n > 0 */ public static int addUp (int n) { // Base case if (n < 10) return n; // Recursive Case return addUp(n/10 + n%10); }