A Typical Algorithm Start Inputs Calculations Most simple

  • Slides: 47
Download presentation

ඇලග රතමයකන ක රයය සදවන A Typical Algorithm බ හ ඇලග රතම සදවනන බහකරම

ඇලග රතමයකන ක රයය සදවන A Typical Algorithm බ හ ඇලග රතම සදවනන බහකරම ඒව ප නවමය වලන ප රතද නයක Start Inputs සඳහ දතත ලබ ග න ගණනය කරන ආක රය Calculations . Most simple algorithms that you will develop will involve inputting some data, performing some calculation and Outputs Stop 8

ග ලම සටහන වල මලක ග The logic constructs �ඕන ම වසඳමක කරම ද

ග ලම සටහන වල මලක ග The logic constructs �ඕන ම වසඳමක කරම ද ග ලම මලක ග සටහනකන 3 ක ය ද ගන ඉදරපත. �It has been proven that three basic constructs for flow of control are sufficient to implement any "proper" algorithm. • Sequence • Selection • Iteration / Loop 10

SEQUENCE �එක ක රයයකට පස තවත ක රයයක අනපළව ලන සදකරමය. �ක රයයන ලය

SEQUENCE �එක ක රයයකට පස තවත ක රයයක අනපළව ලන සදකරමය. �ක රයයන ලය ඇත පළව ලටම ක රය තමක වමද සදව . �SEQUENCE is a linear progression where one task is performed sequentially after another… �solution steps must follow each other in a logical sequence �Statements are executed in the same order as they are written. 11

Start SEQUENCE Wake Up Get Out of Bed START Have A Shower Statement 1

Start SEQUENCE Wake Up Get Out of Bed START Have A Shower Statement 1 Dry Yourself Get Dressed Statement 2 Have Breakfast Pack Bag/Lunch Statement 3 STOP Leave Stop 14

වසඳම Solution Start Sum = 0 Average = 0 Input Number 1 Input Number

වසඳම Solution Start Sum = 0 Average = 0 Input Number 1 Input Number 2 Sum = Numbe 1 + Number 2 Average = Sum / 2 Display Sum, Average Stop 16

SELECTION �වකලප අවසථ වන ඇත අවසථ වල ද ත ර ග නමක සදකරමට ග

SELECTION �වකලප අවසථ වන ඇත අවසථ වල ද ත ර ග නමක සදකරමට ග ලම සටහන වලද ය ද ගනන ක රමයය. �There may be alternative steps that could be taken subject to a particular condition �ම ව න අවසථ වලද බ හ වට ග ලම සටහන වලද එය ප නවමට ය ද ගනන THEN-ELSE ව න වධ නය. IF- �IF-THEN-ELSE is a decision (selection) in which a choice is made between two alternative courses of 22 action

SELECTION ආක තය සරළව IF True False Condition? Statement sequence 1 Statement sequence 2

SELECTION ආක තය සරළව IF True False Condition? Statement sequence 1 Statement sequence 2 ENDIF 23

Start Wake Up Get Out of Bed උද හරණ Am I Dirty? yes Have

Start Wake Up Get Out of Bed උද හරණ Am I Dirty? yes Have A Shower Dry Yourself No Get Dressed yes Am I Hungry ? No Is it a weekday ? Read a Book Have Breakfast yes Pack Bag/Lunch Leave Stop 24

Start Get Length Get Width Is Length equal to Width ? Y N Display

Start Get Length Get Width Is Length equal to Width ? Y N Display “Figure is Square/ Rhombus” Stop 26

Start Get Length Get Width Is Length equal Width ? Y N Display “Figure

Start Get Length Get Width Is Length equal Width ? Y N Display “Figure is Rectangle” Display “Figure is Square” Stop 28

සටහන Note § ග ලම සටහනක දරඝ කහපයක ඇතනම ඒව ව තතයක ස ක

සටහන Note § ග ලම සටහනක දරඝ කහපයක ඇතනම ඒව ව තතයක ස ක තය § ඒව ස ඛ ප හ දලව ය වක නම සහ එහ එකන කට ය ද ගන හ අකරක සමබනධ ල බ හඳන ග නම නරපණය ක ටස සඳහ පට කරමට . ව ත තය තල කරන ල බ . § When a flowchart is too long to fit on a page and is to be continued on another page a connector symbol is used. § A small circle is used to represent a connector in a flowchart. § An alphabet or a number is written inside the circle to identify the pairs of connectors to be joined. 31

A Start Add number to value of total Set value of Counter to 0

A Start Add number to value of total Set value of Counter to 0 Increment counter by 1 Set value of Total to 0 B Get Number Is Counter = 10? N B Y A Write total Stop 32

ITERATION § ය ද ගනන ආක රය අනව ලපයන වරග කපයකට බ ද .

ITERATION § ය ද ගනන ආක රය අනව ලපයන වරග කපයකට බ ද . § There are three different types of loops in structured programming, all of which are available in all programming language. § බහලව ය ද ගනන පළම ක රමය වනන while loop ක රමයය. § The first, and most common of these is the while loop . 34

ITERATION § ප සලට ය ම පළබඳ ගනන. මලම උද හරණය ස ලකලලට §

ITERATION § ප සලට ය ම පළබඳ ගනන. මලම උද හරණය ස ලකලලට § Consider the example of getting out of bed § එම උද හරණය , මහණ කට ස ද ග නම සඳහ පහත ලපය ආද ශ කළ හ ක. යන පයවර § For the step --having a shower-- the following loop could be substituted; while I am dirty wash myself test cleanliness § අපරසඳකම ව . න තවන තර ඉහත ලපය ක රය තමක § The loop continues until the condition of cleanliness changes 35

ITERATION- While Loop § ලපයට අද ල ක ටස න වත ක රය තමක

ITERATION- While Loop § ලපයට අද ල ක ටස න වත ක රය තමක ව . § Section(s) of an algorithm are repeated over and over (obviously these loops must eventually terminate) § කසයම බ ලමට තරකයක සත ය ද අසත ය ද යනන ස ය ම ව නනක ය ද ගන. § This is achieved by a test of whether a condition is true or false § while loop ය මගන කසයම තර කයක සත යද අසත යද ස වමත , තරකය සත ය වන තර කසයම ක රය වක සදකර ග නමත සදව . තරකය අසත ය ව වට ලපය නවත. § In a while loop we continue to repeat something while a condition is true – we terminate the loop when it is false 36

ITERATION- While Loop While false Condition ? true End. While Statement sequence 37

ITERATION- While Loop While false Condition ? true End. While Statement sequence 37

ITERATION- for Loop For Control_value = expression Control_value < end_value ? false true Statement-sequence

ITERATION- for Loop For Control_value = expression Control_value < end_value ? false true Statement-sequence Control_value = Control_value +1 Stop 39

Start no = 1 No no <= 100 Yes Stop Display no no =

Start no = 1 No no <= 100 Yes Stop Display no no = no + 1 41

Start counter = 0 total = 0 No yes Counter< 10 ? Display total

Start counter = 0 total = 0 No yes Counter< 10 ? Display total Get Number Stop total = total+number counter=counter+1 42

START Sum = 0 A decision step is used when the computer has to

START Sum = 0 A decision step is used when the computer has to make a comparison Read A & B Num = A No If Num <= B Print Sum Yes Num = Num + 1 Sum = Sum + Num STOP 44

The End 47

The End 47