Algorithms, Flowcharts, Pseudocode Programs
Algorithms
Algorithm គឺជាការកំនត់ជំហានការងារតាមលំដាប់លំដោយដែលត្រូវតែគោរពតាមដើម្បីដោះស្រាយចំណោទ ណាមួយជាក់ស្ដែង។
Algorithm is a sequence of step instruction must be followed in order to solve a specific problem.
Flowchart
Flowchart គឹជាវិធីរឺបច្ចេកទេសទាំងឡាយដែលគេប្រើដើម្បីអត្ថាធិប្បាយអំពីចលនការទាំងឡាយទៅតាមបែបបនៃបញ្ហារឺសកម្មភាពជាច្រើនដោយបង្ហាញចេញជាគំនូសសញ្ញាផ្សេងៗដែលមានរាងដូចខាងក្រោមៈ
-សំរាប់បង្ហា្ញញពីការចាប់ផ្ដើមរឺបញ្ឈប់ដំនើរការ។
-សំរាប់បង្ហាញពីដំនើរការនៃការងារ។
-សំរាប់បង្ហាញពីការបញ្ជូល រឺ បញ្ជេញទិន្នន័យ។
-សំរាប់ត្រួតពិនិត្យមើលលក្ខខណ្ឌ។
-សំរាប់បង្ហាញពីការរៀបចំ។
-សំរាប់បង្ហាញពីទិសដៅនៃការProcess ។
Algorithms: ដើម្បីគណនារកចំនួនធំបំផុតនៃពីរចំនួន
Flowchart:
Input a, b
if a>b then Max=a
else ab Max=b
output(“The minimum of the end b is Min”)
stop
Algorithms: ដើម្បីគណនារកចំនួនតូចបំផុតនៃពីរចំនួន
1- Input a, b
2- if a>b then Min=b
3- else ab then Min=a
4- output(“The minimum of the end is Min”)
5- stop
Algorithms: ដើម្បីដោះស្រាយសមីការដឺក្រេទី1
Algorithms: ដើម្បីដោះស្រាយសមីការដឺក្រេទី2
ax^2+bx+c=0 a, b, c R
1-Input a,b,c
2-=b2-4ac
3-if =< 0
4-outpout “No root”
5-stop
6-if =0
7-x=-b/2a
8-output “x”
9-stop
10-if >0
11- x_1=(-b-√∆)/2a ; x_(2= -) (-b+ √∆)/2a
12- output "x_(1 ; ) x_2 "
13- stop
រំលឹក
ប្រព័ន្ធសមីការដឺក្រេទីមួយមានពីរអញ្ញាតិ មានរាង
a_1 x+ a_2 y=d_(1 )
a_3 x+a_4 y=d_2
D=|■(a_1&a_2@a_3&a_4 )|=a_1 a_4-a_2 a_3
D_x = |■(d_1&a_2@d_2&a_4 )|=d_1 a_4-a_2 d_4
D_y = |■(a_1&d_1@a_3&d_2 )|=a_1 d_2-a_3 d_1
x=D_x/D; y=D_y/D
ប្រពន្ធ័សមីការ ដឺក្រេទី1 មាន3 អញ្ញតិមានរាង
a_1 x+a_2 y+a_3 z=d_1
a_4 x+a_5 y+a_6 z=d_2
a_7 x+a_8 y+a_9 z=d_3
D=|■(a_1&a_2&a_3@a_4&a_5&a_6@a_7&a_8&a_9 )|=a_1 |■(a_5&a_6@a_8&a_9 )|-a_2 |■(a_2&a_6@a_7&a_9 )|+a_3 |■(a_5&a_6@a_8&a_9 )|
D=|■(■(+@a_1 )&■(+@a_2 )&■(+@a_3 )@a_4&a_5&a_6@a_7&a_8&a_9 )|+|■(■(-@a_1 )&■(-@a_2 )&■(-@a_3 )@a_4&a_5&a_6@a_7&a_8&a_9 )|
Algorithms: ដើម្បីគណនារកឬសរបស់ប្រព័ន្ឋដឺក្រេទី 1 មាន2 អញ្ញាតិ
a_1 x+a_2 y=d_1
a_3 x+a_4 y=d_2
`Input a1, a2, a3, a4, d1, d2
D = a1 a4_ a2 a3
Dx = d1 a4 a2 d2
Dy = a1 d2 a3 d1
if D = 0
if Dx = 0 ; Dy= 0
output “ Many roots”
stop
else Dx ≠ 0 ; Dy ≠0
output “Not root”
stop
else (D ≠0) then
x=D_x/D ;y=D_y/D
output "x ,y"
stop
Algorithms: ដើម្បីគណនារកក្រឡាផ្ទៃត្រីកោណតាមរូបមន្តហេរ៉ុង
Input a,b,c
If a< b+c ; b<a + c ; c<a+b
p=((a+b+c))/2
S=sqrt(p(p-a)(p-b)(p-c))
output ˝S”
stop
else
output “a,b,c are not side of triangle”
stop
Flowchart:
A salesman’s Commission is 5% of his total weekly sales, with an extra $100 if sales exceed $10000. Writ apseudo code program with identifiesman and calcualates his Commission.
Start
Read “Sales, Name”
if Saise>10000
Commission=sales *0.05+100
else
Commission=sales *0.05
end if
Write “Commission, Name”
Stop.
The monthly charge for local telephone calls is as follows:
$8 for up to 100 calls.
plus 65 per call for any of the next 100 calls.
plus 45 per call for any calls beyond 200.
Draw a flowchart or write a pseudo code program with input the number of local calls and output charge.
Read “Number of local”
if local >200
charge=8+0.06*100+0.04*(local-200)
else if local >100
charge=8+0.06*(local-100)
else
charge=8
end if
Write “Charge”
Stop
Flowchart:
Algorithms: សំរាប់គណនាប្រាក់ចំណាយសំរាប់កម្មករម្នាក់ៗដោយដឹងថាបើម៉ោង ធ្វើការប្រចាំសប្តាហ៍លើសពី 40ម៉ោង នោះម៉ោងលើសស្មើនឹងពីដងម៉ោងធម្មតា
Input Hourlyworked, HourlyRate
Overtime = 0
If (Hourlyworked>40) then
Overtime = Hourlyworked-40
Total pay=(Hourlyworked*HourlyRate)+2*(Overtime*HourlyRate)
Output(“Total pay”)
Stop
Flowchart:
Algorithms for if…………else
if (condition)
statement1;
else
statement2;
Flowchart:
Algorithm: សំរាប់គណនារកតំលៃធំបំផុតនៃ3 ចំនួន
Start
Input a, b, c
if (a>b) Max1=a;
else Max1=b;
if(Max1>c) Max=Max1;
else Max=c;
Output “ Max”
Stop
Flowchart:
Algorithm for if…………else if
if(condition)
statement1;
else if(condition)
statement2;
..............................
else
statement(n);
Flowchart:
Algorithm: ដើម្បីទូទាត់ថ្ងៃយាមកង់ដោយដឹងថាៈ ផ្ញើរកង់មុនម៉ោង 16 គឺយកថ្លៃ100រៀល ក្នុង1h និងបើផ្ញើរក្រោយម៉ោង 16គឺយកថ្លៃ200រៀលក្នុងមួយម៉ោង។
Start
input St, Et
if(St<16)
if(Et<16)
amount=(Et-St)*100;
else
amount=(16-St)*100+(Et-16)*200;
else
amount=(Et-St)*200;
Output “amount”
Stop
Algorithm: ដើម្បីត្រួតពិនិត្យ Autovoltage
ដើម្បីត្រួតពិនិត្យ autovoltage នោផ្ទះរបស់លោកអ្នកដោយដឹងថា
-បើសិនv>225 ប្រកាស់ថាប្រយ័ត្នដោយលោតattention
-បើសិន(v>210) and (v<=225)ប្រកាស់ថាNormal
-បើសិន(v>150) and (c=210) ប្រកាស់ថា Low Voltage
-បើសិនv<=150 ប្រកាស់ថា Not Work
Start
Input v
if (v>225)
Output “Attention Interruption “
if((v>210 && v<=250)
Output “ Normal “
if (v>150 && v<=210)
Output “Low Voltage “
if (v<=150)
Output “Not Work “
stop
Flowchart:
Algorithm: ដើម្បីបញ្ចូលពន្ទុ និង វាយតំលៃនិទេ្ទស តាមលក្ខខ័ណដូចខាងក្រោម
score>=95 Excellence
85<=score<95 Very good
75<=score<85 Good
60<=score<75 Fair
50<=score<60 Passed
score<50
Start
Input score
if (score >=95) Output “Excellence” ;
elseif (score>=85) Output “Very good” ;
elseif (score>=75) Output “Good” ;
elseif (score>=60) Output “Fair” ;
elseif (score>=50) Output “Passed” ;
else Output “Failed”;
Stop
Algorithm: សំរាប់រកប្រាក់ពន្ឋលើប្រាក់ខែ ការយកពន្ធលើបា្រក់ខែរបស់បុក្គលិកម្នាក់ៗកំនត់ដូចខាងក្រោម :
បើបា្រក់ខែតិចជាង$2000នោះរដ្ឋយកពន្ធ$150 ។
បើបា្រក់ខែនៅចន្លោះពី$2000 ទៅ $3000 នោះរដ្ឋយកពន្ធ$150 ហើយបន្តែម 1%លើបា្រក់ខែដែលលើសពី$2000 ។
បើបា្រក់ខែច្រើនជាង$3000 នោះយកពន្ធ$250 ហើយបបន្តែម1.5% នៃបា្រក់ដែលលើសពី$3000 ។
រក Algorithm សំរាប់បា្រក់ពន្ធបុក្គលិកត្រូវបង់ក្នុងមួយខែៗ ។យើងឃើញថាការយកពន្ធទៅលើបា្រក់ខែចែកចេញជា 3 ផ្នែកខុសៗគ្នាដោយអាស្រ័យទៅលើបា្រក់ខែបុគ្គលិកបានទទួល ។
Star
Input salary
If (salary>3000) tax=250+0.015 *(salary-3000);
else if (salary>2000) tax =150+0.01*(salary-2000);
else tax=150;
Output “tax”
Stop
*Algorithm:(DANGLING else PROBLEM)
if (expression 1)
if (expression 2)
statement 1
else
statement 2
Algorithm: (Selection-Making Decisions)
if (expression 1)
{
if (expression 2)
statement 1
}
else
statement 2
*-Flowchart:( Loop in C )
Algorithm: (While loop)
while (expression)
statement
Algorithm: (For loop)
for (expression 1: expression 2: expression 3)
{
statements
}
Algorithm: (Do…While loop)
do
statement
while (expression):
Algorithm: ដើម្បីគណនាផលបូក n ចំនួន
1+2+3+.......+20+...+n
Input n
S = 0
S1=S+1
S2=S1+2
........................
n+2 - Sn=Sn-1+n
n+3 - output “Sn”
n+4 – stop
Algorithm: ដើម្បីគណនាផលបូក n ចំនួនគត់សេស
1+3+5+...+21
Flowchart: ដើម្បីគណនាផលគុណ n ចំនួន
P=1 * 2 * 3 * ... *n
Flowchart: ដើម្បីគណនាផលបូក
Sum = -5 - 10 - 15 ... -105
Flowchart: ដើម្បីគណនាបូក
Sum= 1/(5+6)+ 1/(6+7)+ …+1/(100+101)
Algorithm: ដើម្បីទូទាត់ថ្លៃប្រើប្រាស់ភ្លើងអគ្គិសនី
- ចំនួនប្រើប្រាស់ <50 នោះគេយកថ្លៃ 350R/1KW
- 50<=ចំនួនប្រើប្រាស់ <100 នោះគេយកថ្លៃ 400R/1KW
- 100<=ចំនួនប្រើប្រាស់ <150 នោះគេយកថ្លៃ 500R/1KW
- 150<=ចំនួនប្រើប្រាស់ <200 នោះគេយកថ្លៃ 600R/1KW
- 200<=ចំនួនប្រើប្រាស់ នោះគេយកថ្លៃ 700R/1KW
Input oldnumber, newnumber
if (oldnumber >= newnumber) then
printf(“Please Input Again”)
stop
else (oldnumber <newsnumber) then
Use =newnumber – oldnumber
if (Use < 50) then
Pay = Use * 350
else if (Use <100) then
Pay = Use * 400
else if (Use <150) then
Pay = Use * 500
else if (Use <200) then
Pay = Use * 600
else then
Pay = Use * 700
output (“Pay”)
stop
Flowchart: ដើម្បីគណនាផលបូកៈ
e^x=1+x+ x^2/2!+ x^3/3!+ + x^n/n!
Flowchart: ដើម្បីគណនាផលបូកៈ
Sum=1- 1/2+1/3+1/4+ ∓1/n
*Trace the value of A and B through the flowchart to fine the output givent the input:
a – A = 10 , B = 5
b – A = 3 , B =5
c – A = 5 , B = 10
a – A = A2 + B
A = 102 + 5
A = 105 ; B = 530
b - A = A + 2 * B
A = 3 + 10 = 13
B = B + 10
B = 5 + 10 = 15
B = 15 ; A = 13
c - A= A + 2 * B
A = 5 + 20 * B
B = B + 5 * A
B = 10 + 5 * 25
B = 25 ; A = 135
Find the output of the following flowchart
i=1 sum=0+1=1
i=3 sum=1+3=4
i=5 sum=4+5=9
i=7 sum=9+7=16
i=9 sum=16+9=25
Algorithm: ដើម្បីរក PGCD នៃពីរចំនួន
Input a , b
if a > b then
a = a – b
if a < b then
b = b – a
………….
………….
if a = b then
PGCD = a
else (a ≠ b)
ត្រលប់ទៅមើលជុំហ៊ានទីពីរម្តងទៀត
Output “PGCD”
stop
Algorithm: ដើម្បីរក PPCM នៃពីរចំនួន
PGCD(a;b)= (a*b)/(PGCD(a;b))
Input a , b
គណនា PGCD (a ; b)
if a>b then a = a – b
else (a < b) then b = b – a
……………………
អនុវត្តន៏របៀបនេះរហូតដល់ a = b
if a = b then PGCD = a
else (អនុវត្តន៏a = b)
PPCM= (a*b)/(PGCD(a;b))
Printf(“PPCM”)
stop
របៀបបំលៃងគោលផ្សេងៗ គោលផ្សេងៗ
ប្រពន្ឋ័គោលពីរ 2 ទៅគោលដប់ 10
Binary to Decimal
(101011)2 = (?)10
= 1*25 + 0*24 + 1*23 + 0*22 + 1*21 + 1*20
= 32+8+2+1=43
= (43)10
ប្រពន្ខ័គោល 8 ទៅគោល 10
Octal to Deciaml
(125)8 = (?)10
= 1*82 + 2*81 + 5*80 =85
(125)8 = (8510
Hexadecimal to Decimal
(0 1 2 3 4 5 6 7 8 9 A10 B11 C12 D13 E14 F15)
(4AB)16 = (?)10
= 4*162 + A*161 + B*160
= 4*162 + 10*161 + 11*160
= 1024+160+11=1195
(4AB)16 = (1195)10
Decimal to Binary
(25)10 = (?)2
25 2
1 12 2
0 6 2
0 3 2
1 1 2
0
(25)10 = (11001)2
Decimal to Octal
(25)10 = (?)8
25 8
1 3 8
3 0
(25)10 = (31)8
Decimal to Hexadecimal
(43)10 = (?)16
48 16
11 2 16
2 0
(48)10 = (2B)16
Binary to Octal
(101011)2 = (?)8
(101011)2 = (?)10 = (?)8
+(101011)2 = (?)10
=1*25+0*24+1*23+0*22+1*21+1*20
=32+8+2+1=43
(101011)2 = (43)10
(43)10 =(?)8
43 8
3 5 8
5 0
(48)10 = (2B)16
(43)10 =(53)8
(101011)2
011 = 3
101 = 5 = (53)8
(1010111)2 = (?)8
111 = 7
010 = 3 = (127)8
001 = 1
Ex: 6+5= 11
110+101=(1011)2 = (11)10
12 = 1100
6 = 110
1100 110
0000 (10)2 = (2)10
*Octal to Binary
(125)8 = (?)2
(1)8 = (001)2
(2)8 = (010)2 (125)8 = (001010101)2
(5)8 = (101)2
*Binary to Hexadecimal
(7AC)16 =(?)2
(7)16 = (0111)2
(A)16 = (1010)2 (7AC)16 = (011110101100)2
(C)16 = (1100)2
Algorithm: ដើម្បីបំលែងពីប្រព័ន្ឋគោល 10 ប្រព័ន្ឋគោល2
Input n
កំនត់ i = 0
កំនត់ ai = n 2
កំនត់ n /= 2
បើ n != 0 ត្រលប់ទៅជុំហ៊ានទីបី រួចកំនត់ i++
Output “ai”
បើ i>0 ត្រលប់ទៅជុំហ៊ានទី 6 រួចកំនត់ i—
Stop
Algorithm: ដើម្បីគណនាផលបូក
S= (1*2*3)/(4*5)+(2*3*4)/(5*6)+(3*4*5)/(6*7)+ +(n(n+1)(n+2))/((n+3)(n*4))
Algorithm: ដើម្បីគណនា n! មានន័យថា n!=1*2*3*4*……*n, 0!=1
Input n
if (n==0) Output : “The factoriel of n! is 1”;
else
អនុវត្តន៏ Algorithm ផលគុណ n តួ
Stop
Algorithm: ដើម្បីគណនាផលបូក
S=√(1&1!)+√(2&2!)+∛3!+ +√(n&n!)
Input n
S=0 , i=1 , P=1
P = P * i
S = S + pow (P, 1/(float)i)
បើ i<=n ត្រលប់ទៅ ជំហ៊ានទី 3
Output “S”
Stop
Algorithm: ដើម្បីត្រលាប់លេខ
1824 ទៅជា 4281
Input n
a=n%10
n=n/10
Output “a”
បើ n>0 ត្រលប់ទៅជុំហ៊ានទី២
Stop
Algorithm: រកចំនួនបឋម និងមិនបឋម
Input n
i=2 , k=0
if(n%1= =0)
{Output “n is not prime number”
k++
break;}
i++
បើ i<=n-1 ត្រលប់ទៅជាជុំហ៊ានទី3
if(k= =0) Output “n is prime number”
Stop
Algorithm: ដើម្បីគណនា Fibonacci
Fn=Fn-1+Fn-2
F1=1 , F2=1 , n=3 , …
1 1 3 5 8 13 21 ……….
Input n
Fibonacci F1=1 , F2=1
Fibonacci F3=F2+F1
Fibonacci F4=F3+F2
…………………….
Fibonacci Fn= Fn-1+ Fn-2
Output “Fn”
Stop
Algorithm: (Combination C_n^p)
Start
Input n
Input P (n>=P)
C=1
C = C*((n-i+1))/i (i=1)
……………….
n) C=C*((n-i+1))/i (i = p)
n+1 – Output “C”
n+2 – Stop
Flowchart:
Where Flowchart
ReplyDelete