240 likes | 421 Views
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
E N D
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 • entry condition fails • The entry condition should eventually become • false by iteratively executing body statements 軟體實作與計算實驗
Decimal to binary representations • Input : positive decimal numbers • Output : binary representationsof the input 軟體實作與計算實驗
Modulus: find remainder >> rm=mod(10,3) ans = 1 >> rm=mod(100,7) ans = 2 軟體實作與計算實驗
Quotient >> quo=floor(10/3) ans = 3 >> quo=floor(100/7) ans = 14 軟體實作與計算實驗
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 軟體實作與計算實驗
Example M > 0 M=1, b1=1 M=5, C=‘101’ 1 0 1 軟體實作與計算實驗
Example M=25, C: ‘11001’ The problem is to find b for given M 1 0 0 1 1 軟體實作與計算實驗
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 軟體實作與計算實驗
Decimal to binary representation Given M>0, find array b Strategy : Iteratively find b1 ,b2,…,bn Append from left to b 軟體實作與計算實驗
Find b1 remainder r = mod(q,2); q_new = (q-r)/2; 軟體實作與計算實驗
Find b2 Append remainder from left-hand-side r = mod(q,2); q_new = (q-r)/2; 軟體實作與計算實驗
Find bi Append remainder from left-hand-side r = mod(q,2); q_new = (q-r)/2; 軟體實作與計算實驗
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 軟體實作與計算實驗
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 軟體實作與計算實驗
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 軟體實作與計算實驗
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 軟體實作與計算實驗
Entrycondition • Entry condition q > 0 • Halt if q==0 軟體實作與計算實驗
Determine b q=M;b=[]; end q > 0 true r = mod(q,2); b=[r b]; q = (q-r)/2; 軟體實作與計算實驗
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; 軟體實作與計算實驗
q=M;b=[];base=8 end q > 0 true r = mod(q,base); b=[r b]; q = (q-r)/base; Decimal to Octal representation 軟體實作與計算實驗
>> dec2oct(7) ans = 7 >> dec2oct(8) ans = 1 0 軟體實作與計算實驗
>> dec2oct(18) ans = 2 2 >> dec2oct(64) ans = 1 0 0 軟體實作與計算實驗