軟體實作與計算實驗 1 while-loop flow chart decimal to binary representations lecture 6...
TRANSCRIPT
軟體實作與計算實驗 1
While-loop flow chart Decimal to binary representations
Lecture 6 While-Loop programming
軟體實作與計算實驗 2
While loop
statements
falsewhile entry_condition
statements;
end
ECend
軟體實作與計算實驗 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 numbersOutput : 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
'...'C
2...22M
11
01
21
1
bbb
bbb
nn
nn
nn
0 where
],,...,[ 12
n
n
b
bbbb
軟體實作與計算實驗 8
Example
M > 0M=1, b1=1
M=5, 0
11
22
3 222M bbb
101
C=‘101’ ],,[ 123 bbbb
軟體實作與計算實驗 9
Example
M=25, C: ‘11001’
The problem is to find b for given M
01
12
23
34
45 22222M bbbbb
1 1 0 0 1
],,,,[ 12345 bbbbbb
軟體實作與計算實驗 10
Example
M > 0M=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
01
21
1 2...22M bbb nn
nn
軟體實作與計算實驗 11
Decimal to binary representation
Given M>0, find array bStrategy :
Iteratively find b1 ,b2 ,…,bn
Append from left to b
],,,...,[ 123 bbbbb n
軟體實作與計算實驗 12
Find b1
1
10
22
11
01
12
11
2
)2...22(2
22...22
bq
bbbb
bbbbq
Mq
new
nn
nn
nn
nn
r = mod(q,2);q_new = (q-r)/2;
remainder
][ 1bb
軟體實作與計算實驗 13
Find b2
2
20
33
12
02
21
1
2
)2...22(2
2...22
bq
bbbb
bbbq
new
nn
nn
nn
nn
new
r = mod(q,2);q_new = (q-r)/2;
Append remainder from left-hand-side
],[ 12 bbb
軟體實作與計算實驗 14
Find bi
inew
iiin
nin
n
iin
nin
n
new
bq
bbbb
bbbq
2
)2...22(2
2...220
11
1
01
1
r = mod(q,2);q_new = (q-r)/2;
Append remainder from left-hand-side
],,,...,[ 123 bbbbb i
軟體實作與計算實驗 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 zeroreturn b
軟體實作與計算實驗 16
Strategy
statements
true
ECend
q=M; b=[];Calculate r, qnew and bi iterativelyAppend bi to b from left-hand-sideq qnew
until q equal zeroreturn b
軟體實作與計算實驗 17
An iterative approach
q=M; b=[];Calculate r, qnew and bi iterativelyAppend bi to b from left-hand-sideq qnew
until q equal zeroreturn b
Calculate r, qnew and bi Append bi to b from left-hand-sideq qnew
true
q>0end
q=M; b=[]
軟體實作與計算實驗 18
Calculate rAppend r to b from left-hand-sideUpdate q
An iterative approach
q=M; b=[];Calculate r, qnew and bi iterativelyAppend bi to b from left-hand-sideq qnew
until q equal zeroreturn b
true
q>0end
q=M; b=[]
軟體實作與計算實驗 19
Entry condition
Entry condition
q > 0Halt if q==0
軟體實作與計算實驗 20
Determine b
r = mod(q,2);b=[r b];q = (q-r)/2;
true
q > 0end
q=M;b=[];
軟體實作與計算實驗 21
Change base
r = mod(q,2);b=[r b];q = (q-r)/2;
true
q > 0end
q=M;b=[];
r = mod(q,base);b=[r b];q = (q-r)/base;
true
q > 0end
q=M;b=[];base=2
軟體實作與計算實驗 22
Decimal to Octal representation
r = mod(q,base);b=[r b];q = (q-r)/base;
true
q > 0end
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