軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop.

Slides:



Advertisements
Similar presentations
Looping Structures: Do Loops
Advertisements

Programming in R 2007 R 統計軟體研習會 蔡政安 Associate Professor Department of Public Health & Biostatistics Center China Medical University.
軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.
Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.
CMPS 1371 Introduction to Computing for Engineers
ITC 240: Web Application Programming
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Some loop programs 1: Read in 10 integers and output their sum
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Unit 1: Real Numbers Created by Educational Technology Network
Homework: Cumulative Review
= 9 = 8 = 12 = 19 = 2 = 3 = 17 = 5 = 11 = 5.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
EXAMPLE 5 Rewrite a conditional statement in if-then form
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The Complex Numbers The ratio of the length of a diagonal of a square to the length of a side cannot be represented as the quotient of two integers.
4. Python - Basic Operators
軟體實作與計算實驗 1 Lecture 9 Classification Two mappings from position to color Linear combination of distances to centers RBF: Linear combination of exponential.
數值方法 2008, Applied Mathematics NDHU1  Looping for loop while loop  Break and return  Recursion Lecture 2 Iteration.
6:12-4 Exploring square roots and rational numbers.
Loops CS 103 February 19, 2007 Presented by Nate.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
Loops CS 103 February 13, 2009 Author: Nate Hamm.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
軟體實作與計算實驗 1  While-loop flow chart  Decimal to binary representations Lecture 6 While-Loop programming.
軟體實驗 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.
Preview to the Exponential Number System September 4th, 2015.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Real Number and the number Line. Number System Real numbers: is number that can be positive or negative and have decimal places after the point. Natural.
COMP Loop Statements Yi Hong May 21, 2015.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
軟體實驗:實作以二分法開根號 虞台文. 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.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
CSE123 - Lecture 4 Structured Programming- Loops.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Fundamentals of Programming Most.
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.
Lecturer: Santokh Singh
Making Sense of Rational and Irrational Numbers
Program design Program Design Process has 2 phases:
Chapter 6: Loops.
ECE Application Programming
COUNTING IN BINARY Binary weightings 0 x x x x 8
Square Roots Practice © T Madas.
Programming Fundamentals
Introducing Do While & Do Until Loops & Repetition Statements
Algebra II (Honors) Chapter 1
Outline Altering flow of control Boolean expressions
Conditional Construct
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
CS2011 Introduction to Programming I Loop Statements (II)
Loops.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
COUNTING IN BINARY Binary weightings 0 x x x x 8
Program Flow.
Rational and Irrational numbers
Natural Numbers The first counting numbers Does NOT include zero
CS2011 Introduction to Programming I Loop Statements (I)
Chapter 4: Loops and Iteration
Presentation transcript:

軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop

軟體實作與計算實驗 2 While loop statements true while halting_condition statements; end Hc end

軟體實作與計算實驗 3 Flow chart statements true Execute body statements repeatedly until the halting condition holds The halting condition should eventually become true by iterative executions of body statements Hc end

軟體實作與計算實驗 4 Integer square root Given a positive integer M, find its positive integer square root. If there exists no positive integer square, return zero

軟體實作與計算實驗 5 Strategy statements true Hc end Count positive integers one by one until n^2 > M If (n-1)^2 equals M, return n-1, otherwise return zero

軟體實作與計算實驗 6 Strategy n = n+1 true (n+1)^2 < M end Count positive integers one by one until (n+1)^2 > M If n^2 is not M, n=0 n = 1 n^2~=M n=0

軟體實作與計算實驗 7 Matlab codes n = n+1 true (n+1)^2 < M end n=1; while (n+1)^2 < M n=n+1; end If n^2 ~= M n = 0; end n = 1 n^2~=M n=0

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

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

軟體實作與計算實驗 10 Decimal to binary transition M is a positive integer b is an array of length n b is the binary representation of M if

軟體實作與計算實驗 11 Decimal to binary transition Given M, find array b Strategy : Iteratively find b i

軟體實作與計算實驗 12 Find b 1 b(1) = mod(N(1),2); N(2) = floor(N(1)/2);

軟體實作與計算實驗 13 Find b 2 b(2) = mod(N(2),2); N(3) = floor(N(2)/2);

軟體實作與計算實驗 14 Find b i b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); b(2) = mod(N(2),2); N(3) = floor(N(2)/2); 2  i

軟體實作與計算實驗 15 Strategy statements true Hc end Calculate N(i+1) and b(i) iteratively until N(i+1) equals zero return b

軟體實作與計算實驗 16 While loop b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); i=i+1 true Hc end Calculate N(i+1) and b(i) iteratively until N(i+1) equals zero return b i=1;N(i)=M;

軟體實作與計算實驗 17 Halting condition Check if N(i+1) is zero before increasing i Hc = N(i+1) == 0 Chech if N(i+1) is zero after increasing i Hc = N(i) == 0

軟體實作與計算實驗 18 While loop b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); i=i+1 true N(i) > 0 end Calculate N(i+1) and b(i) iteratively until N(i+1) equals zero return b i=1;N(i)=M;

軟體實作與計算實驗 19 Matlab codes b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); i=i+1 true N(i) > 0 end i=1;N(i)=M; while N(i) > 0 b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); i=i+1 end

軟體實作與計算實驗 20 Equivalent flow charts b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); i=i+1 true N(i) > 0 end i=1;N(i)=M; b(i) = mod(N,2); N = floor(N/2); i=i+1 true N > 0 end i=1;N=M;

軟體實作與計算實驗 21 Equivalent Matlab codes i=1;N(i)=M; while N(i) > 0 b(i) = mod(N(i),2); N(i+1) = floor(N(i)/2); i=i+1 end i=1;N=M; While N > 0 b(i) = mod(N,2); N = floor(N/2); i=i+1 end

軟體實作與計算實驗 22 b(i) = mod(N,base); N = floor(N/base); i=i+1 true N > 0 end i=1;N=M;base=2; i=1;N=M; base =2 While N > 0 b(i) = mod(N,base); N = floor(N/base); i=i+1 end

軟體實作與計算實驗 23 Decimal to Octal transition b(i) = mod(N,base); N = floor(N/base); i=i+1 true N > 0 end i=1;N=M;base=8; i=1;N=M; base =8 While N > 0 b(i) = mod(N,base); N = floor(N/base); i=i+1 end

軟體實作與計算實驗 24 Decimal to Octal transition Flow Chart: S=[ ]; quo=a; rm=mod(quo,b); quo=floor(quo/b); T quo >= b end S=[rm S]; if quo > 0 S=[quo S] end

軟體實作與計算實驗 25 Matlab codes dec2oct.m

軟體實作與計算實驗 26 rm=mod(quo,b); quo=floor(quo/b); T quo >= b S=[rm S];

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

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