軟體實作與計算實驗 1  While-loop flow chart  Decimal to binary representations Lecture 6 While-Loop programming.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Programming in R 2007 R 統計軟體研習會 蔡政安 Associate Professor Department of Public Health & Biostatistics Center China Medical University.
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture 4 Discrete Mathematics Harper Langston. Algorithms Algorithm is step-by-step method for performing some action Cost of statements execution –Simple.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
軟體實作與計算實驗 1 Lecture 9 Classification Two mappings from position to color Linear combination of distances to centers RBF: Linear combination of exponential.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Chapter 3 Section 1 Number Representation Modern cryptographic methods, unlike the classical methods we just learned, are computer based. Representation.
數值方法 2008, Applied Mathematics NDHU1  Looping for loop while loop  Break and return  Recursion Lecture 2 Iteration.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Do-while loop Syntax do statement while (loop repetition condition)
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.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
軟體實作與計算實驗 1  Binary search  While loop  Root Finding Lecture 6II While Loop.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
Dividing Decimals by a Whole Number 3.6 ÷ 3.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
軟體實驗 Perfect Number Enumerating 虞台文. Perfect Numbers A perfect number is such that it is equal to the sum of its proper divisors, which are any divisors.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The Real Zeros of a Polynomial Function Section 5.2 Also Includes Section R.6 : Synthetic Division 1.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
Unary Not operator !  !true = false  !false = true.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
軟體實驗:實作以二分法開根號 虞台文. The Lab Problem Write a function to compute by bisection the square root of a positive integer n given by the caller. The function’s.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop.
CPS120: Introduction to Computer Science Session 5.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Convert Decimal to Binary
C# and the .NET Framework
Ch 7: JavaScript Control Statements I.
Programming Fundamentals
Repetition and Loop Statements
Introduction to IT By: Muhammed s. anwar.
Comparing and Ordering Rational Numbers
Loops Prof.Gaikwad Varsha P. Information Technology Dept.
A LESSON IN LOOPING What is a loop?
Intro to Computer Science Loops
Dividing a whole number to get decimal
Topics Introduction to Repetition Structures
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CS2011 Introduction to Programming I Loop Statements (I)
CSCI 1100/1202 February 6, 2002.
COMPUTING.
Presentation transcript:

軟體實作與計算實驗 1  While-loop flow chart  Decimal to binary representations Lecture 6 While-Loop programming

軟體實作與計算實驗 2 While loop statements false while entry_condition statements; end EC end

軟體實作與計算實驗 3 Flow chart statements false Execute body statements repeatedly until the entry condition fails The entry condition should eventually become false by iteratively executing body statements EC end

軟體實作與計算實驗 4 Decimal to binary representations Input : positive decimal numbers Output : binary representations of the input

軟體實作與計算實驗 5 Modulus: find remainder >> rm=mod(10,3) ans = 1 >> rm=mod(100,7) ans = 2

軟體實作與計算實驗 6 Quotient >> quo=floor(100/7) ans = 14 >> quo=floor(10/3) ans = 3

軟體實作與計算實驗 7 Decimal to binary representation Let M be a positive decimal number Let b be an array of length n b denotes the binary representation of M if

軟體實作與計算實驗 8 Example M > 0 M=1, b 1 =1 M=5, 1 01 C=‘101’

軟體實作與計算實驗 9 Example M=25, C: ‘11001’ The problem is to find b for given M

軟體實作與計算實驗 10 Example M > 0 M=1, b 1 =1 M=5, b=[1 0 1] M=25, b=[ ] The problem is to find b for given M

軟體實作與計算實驗 11 Decimal to binary representation Given M>0, find array b Strategy : Iteratively find b 1,b 2,…,b n Append from left to b

軟體實作與計算實驗 12 Find b 1 r = mod(q,2); q_new = (q-r)/2; remainder

軟體實作與計算實驗 13 Find b 2 r = mod(q,2); q_new = (q-r)/2; Append remainder from left-hand-side

軟體實作與計算實驗 14 Find b i r = mod(q,2); q_new = (q-r)/2; Append remainder from left-hand-side

軟體實作與計算實驗 15 An iterative approach q=M; b=[]; Calculate r, q new and b i iteratively Append b i to b from left-hand-side q q new until q equal zero return b

軟體實作與計算實驗 16 Strategy statements true EC end q=M; b=[]; Calculate r, q new and b i iteratively Append b i to b from left-hand-side q q new until q equal zero return b

軟體實作與計算實驗 17 An iterative approach q=M; b=[]; Calculate r, q new and b i iteratively Append b i to b from left-hand-side q q new until q equal zero return b Calculate r, q new and b i Append b i to b from left-hand-side q q new true q>0 end q=M; b=[]

軟體實作與計算實驗 18 Calculate r Append r to b from left-hand-side Update q An iterative approach q=M; b=[]; Calculate r, q new and b i iteratively Append b i to b from left-hand-side q q new until q equal zero return b true q>0 end q=M; b=[]

軟體實作與計算實驗 19 Entry condition q > 0 Halt if q==0

軟體實作與計算實驗 20 Determine b r = mod(q,2); b=[r b]; q = (q-r)/2; true q > 0 end q=M;b=[];

軟體實作與計算實驗 21 Change base r = mod(q,2); b=[r b]; q = (q-r)/2; true q > 0 end q=M;b=[]; r = mod(q,base); b=[r b]; q = (q-r)/base; true q > 0 end q=M;b=[];base=2

軟體實作與計算實驗 22 Decimal to Octal representation r = mod(q,base); b=[r b]; q = (q-r)/base; true q > 0 end q=M;b=[];base=8

軟體實作與計算實驗 23 >> dec2oct(7) ans = 7 >> dec2oct(8) ans = 1 0

軟體實作與計算實驗 24 >> dec2oct(18) ans = 2 2 >> dec2oct(64) ans = 1 0 0