1 / 24

Lecture 6 While-Loop programming

Lecture 6 While-Loop programming. While-loop flow chart Decimal to binary representations. While loop. false. end. while entry_condition statements; end. EC. statements. Flow chart. false. end. EC. statements. Execute body statements repeatedly until the

vanig
Download Presentation

Lecture 6 While-Loop programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

  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, b1=1 M=5, C=‘101’ 1 0 1 軟體實作與計算實驗

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related