Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop."— Presentation transcript:

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

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

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

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

10 軟體實作與計算實驗 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 軟體實作與計算實驗 11 Decimal to binary transition Given M, find array b Strategy : Iteratively find b i

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

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

14 軟體實作與計算實驗 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 軟體實作與計算實驗 15 Strategy statements true Hc end Calculate N(i+1) and b(i) iteratively until N(i+1) equals zero return b

16 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 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 軟體實作與計算實驗 25 Matlab codes dec2oct.m

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

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

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


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

Similar presentations


Ads by Google