Sunday, 17 June 2012

Sir Asdullah Bhatti The 5th lecture


LECTURE#05
Algorithms Designing

SUMMARY
·         What is algorithm?
·         Why do we have need of algorithm
·         Algorithm v/s flow charts

What is algorithm?
Algorithm is set of finite steps that represent any process and each step takes the process more close to its solution or termination.
Unlike flow charts algorithm steps are written in native language like English, for example
Problem Statement: write an algorithm that takes a number and tell us either the number is “EVEN” or “ODD”

Description: X is a variable for getting input from user; Remainder is variable for holding answer of modulus 
STEP-01:          Get X
STEP-02:          Set Remainder: = X Mod 2
STEP-03:          [Start if block]

If Remainder = 0 then
                                    Print “number is even”
                        Else
                                    Print “number is odd”

                        [End if block]
STEP-04:          Exit


Consider another example for grading system of BISE SUKKUR

Description: variable TOT-OBT is used for storing total obtained marks of student; MAX is used for storing maximum marks and PER is used to hold the percentage






STEP-01:          Get TOT-OBT
STEP-02:          Get MAX
STEP-03           :           Set PER: =TOT-OBT*100/MAX
STEP-04:          [start if block]
                        If PER>=80 then
                                    Print “A-Grade”
                        Else if PER>=70 then
                                    Print “B-Grade”
                        Else if PER>=60 then
                                    Print “C-Grade”
                        Else if PER>=50 then
                                    Print “D-Grade”
                        Else if PER>=40 then
                                    Print “E-Grade”
                        Else
                                    Print “Fail”
                        [End if Block]
STEP-05:          Exit

                                   
                               
Consider another example if a mobile manufacturer HTC decided to increase in salary of employees but according to their grades

Grade = 20, 20%
Grade = 19, 19%
Grade = 18, 18%
Grade = 17, 17%
………
………
Grade 1, 1%

We have to write an algorithm that calculates salary for next month for this situation, so

Description: GRADE is holding for grade of employee; SAL is holding current salary of employee, INCREASED is used to hold increment in RUPEES and NEW-SAL is used to store new calculated salary of employee, which he/she will withdraw from next
Month

STEP-01:               INCREASED: = GRADE * SAL/100
STEP-02:               NEW-SAL: = SAL + INCREASED
STEP-03:               Print NEW-SAL
STEP-04:               Exit


You was thinking that condition flow if/else structure should be used to tackle this situation, but we got solution without using condition flow, is my readers confused, am I right?

Probably “yes”

Do you know why you are confused because you forget the first step to keep in mind while programming and that is

1.       Detail analysis of problem

So when you are going to write any solution for a particular problem, first try to understand the problem and just not understand but detailed analysis is required for example what is input, what type of factors are involved in input, what output is required and how to transform the input into output to get exact solution etc

 Suppose again consider the same problem but this time for different conditions

 Grade 15 to 20, 10%
Grade = 10 to 14, 5%
Grade = 5 to 9, 3%
Grade below 5, 2%

Now this problem is depend on complex condition, therefore we use logical AND operator to check the range of grade between specific values like 15 to 20

STEP-01:               [IF BLOCK START]
If GRADE >=15 AND GRADE<=20 then
INC–SAL = 10xSAL/100
Else if GRADE>=10 AND GRADE<=14 then
                INC-SAL = 5*SAL/100
Else if GRADE>=5 AND GRADE<=9 then
                INC-SAL = 3*SAL/100
Else
                INC-SAL = 2*SAL/100
[END IF BLOCK]
STEP-02:               NEW-SAL = SAL + INC-SAL;
STEP-03:               print NEW-SAL
STEP-04:               EXIT



Assignment for tomorrow: Draw flow chart for above algorithm





Calculator example, we have to draw an algorithm that takes two numbers and a binary arithmetic operator to perform operation between these two


Description: Consider X and Y are two numbers; OP is used for storing operator symbol and ANS is used to hold the result of operation

STEP-01:               GET X
STEP-02:               GET Y
STEP-03:               GET OP
STEP-04:               [IF BLOCK STARTS]
If OP = ‘+’ then
                ANS = X+Y
Else if OP=’-‘then
                ANS = X-Y
Else if OP=’*’ then
                ANS=X*Y
Else if OP=’/’ then
                ANS=X/Y
Else
                Print “Invalid Operator”
                EXIT
[END IF BLOCK]
STEP-05:               Print ANS
STEP-06:               EXIT


Why do we have need of algorithm?
I have one reason algorithm represent process in a very high level language, that’s more easily understandable by the programmer; any programmer can implement algorithm into a code that are specific to its language, so in short we can say logic is easily transferable to any language specific to your needs.

Algorithm v/s flow chart
Both represent finite process no doubt but the way they represent is different, flow charts depicts process by using graphical symbols while algorithm represent process in English like or any native language.  OK






No comments:

Post a Comment