Skip to main content

Algorithm,Flow Chart and Pseudo code


I.Algorithm

Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for completing a task. Algorithm is an English-like representation of the logic which is used to solve the problem. It is a step- by-step procedure for solving a task or a problem. The steps must be ordered, unambiguous and finite in number.

For accomplishing a particular task, different algorithms can be written. The different algorithms differ in their requirements of time and space. The programmer selects the best-suited algorithm for the given task to be solved.

(Note:The word algorithm is derived from the name of the 9th-century Persian mathematician Muḥammad ibn Mūsā al-Khwārizmī, whose nisba (identifying him as from Khwarazm) was Latinized as Algoritmi (Arabized Persian الخوارزمی c. 780–850).[14][15] Muḥammad ibn Mūsā al-Khwārizmī was a mathematician, astronomergeographer, and scholar in the House of Wisdom in Baghdad, whose name means 'the native of Khwarazm', a region that was part of Greater Iran and is now in Uzbekistan.)

Example:
Algorithm to find the greatest among three numbers
ALGORITHM 1.
Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A and B. If A is greater perform step 4 else perform step 6.
Step 4: Compare A and C. If A is greater, output “A is greatest” else output “C is greatest”.
Step 5: goto step 7.
Step 6: Compare B and C. If B is greater, output “B is greatest” else output “C is greatest”.
Step 7: Stop
ALGORITHM 2.
Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A and B. If A is greater, store A in MAX, else store B in MAX.
Step 4: Compare MAX and C. If MAX is greater, output “MAX is greatest” else output “C is greatest”.
Step 5: Stop

Both the algorithms accomplish the same goal, but in different ways. The programmer selects the algorithm based on the advantages and disadvantages of each algorithm. For example, the first algorithm has more number of comparisons(steps), whereas in the second algorithm an additional variable MAX is required.

II. Flow Chart

A flowchart is be defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a task.A flowchart is also a type of diagram that represents a workflow or process.

The flowchart shows the steps as boxes of various kinds, and their order by connecting the boxes with arrows. This diagrammatic representation illustrates a solution model to a given problem. Flowcharts are used in analyzing, designing, documenting or managing a process or program in various fields.

Common symbols

The American National Standards Institute (ANSI) set standards for flowcharts and their symbols in the 1960s.The International Organization for Standardization (ISO) adopted the ANSI symbols in 1970. The current standard, ISO 5807, was revised in 1985.Generally, flowcharts flow from top to bottom and left to right.


Note: You can use MS word or LibreOffice Draw to draw the flowchart.

Preparing a Flowchart
A flowchart may be simple or complex. The most common symbols that are used to draw a flowchart are—Process, Decision, Data, Terminator, Connector and Flow lines. While drawing a flowchart, some rules need to be followed—(1) A flowchart should have a start and end, (2) The direction of flow in a flowchart must be from top to bottom and left to right, and (3) The relevant symbols must be used while drawing a flowchart. While preparing the flowchart, the sequence, selection or iterative structures may be used wherever required. Figure below  shows the sequence, selection and iterative structures.


We see that in a sequence, the steps are executed in linear order one after the other. In a selection operation, the step to be executed next is based on a decision taken. If the condition is true (yes) a different path is followed than if the condition evaluates to false (no). In case of iterative operation, a condition is checked. Based upon the result of this conditional check, true or false, different paths are followed. Either the next step in the sequence is executed or the control goes back to one of the already executed steps to make a loop.

Examples: (a) product of two numbers  (b) biggest of 3 numbers (c) sum of numbers 1-100


III. Pseudo  code

In computer science, pseudo code is a plain language description of the steps in an algorithm. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine reading. It typically omits details that are essential for machine understanding of the algorithm, such as variable declarations and language-specific code. The programming language is augmented with natural language description details, where convenient, or with compact mathematical notation.

The purpose of using pseudocode is that it is easier for people to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications to document algorithms and in planning of software and other algorithms.

Using pseudo code, it is easier for a programmer or a non-programmer to understand the general working of the program, since it is not based on any programming language. It is used to give a sketch of the structure of the program, before the actual coding. It uses the structured constructs of the programming language but is not machine-readable. Pseudo code cannot be compiled or executed. Thus, no standard for the syntax of pseudo code exists ( here we will use C like syntax)

Preparing a Pseudo Code

Pseudo code is written using structured English.

In a pseudo code, some terms are commonly used to represent the various actions. For example, for inputting data the terms may be (INPUT, GET, READ), for outputting data (OUTPUT, PRINT, DISPLAY), for calculations (COMPUTE, CALCULATE), for incrementing (INCREMENT), in addition to words like ADD, SUBTRACT, INITIALIZE used for addition, subtraction, and initialization, respectively.

The control structures—sequence, selection, and iteration are also used while writing the pseudo code.
Figure below shows the different pseudo code structures. 

The sequence structure is simply a sequence of steps to be executed in linear order. 

There are two main selection constructs—if- statement and case statement. In the if-statement, if the condition is true then the THEN part is executed otherwise the ELSE part is executed. There can be variations of the if-statement also, like there may not be any ELSE part or there may be nested ifs. 

The case statement is used where there are a number of conditions to be checked. In a case statement, depending on the value of the expression, one of the conditions is true, for which the corresponding statements are executed. If no match for the expression occurs, then the OTHERS option which is also the default option, is executed.

WHILE and DO-WHILE are the two iterative statements. The WHILE loop and the DO- WHILE loop, both execute while the condition is true. However, in a WHILE loop the condition is checked at the start of the loop, whereas, in a DO-WHILE loop the condition is checked at the end of the loop. So the DO-WHILE loop executes at least once even if the condition is false when the loop is entered.



Note: You can also use BEGIN or START statement as a first statement.
 
Advantages of Pseudo code
• Improves the readability of any approach. It's one of the best approaches to start implementation of an algorithm.
• Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough documentation, so the program of one developer can be understood easily when a pseudo code is written out. In industries, the approach of documentation is essential. And that's where a pseudo-code proves vital.
• The main goal of a pseudo code is to explain what exactly each line of a program should do, hence making the code construction phase easier for the programmer.

Examples
Write Algorithm, Flowchart and Pseudo code

1.Area and Circumference of the circle

Step 1: Start
Step 2: Read radius r
Step 3: Calculate area A=3.14*r*r
Step 4: Calculate circumference C=2*3.14*r
Step 5: Display A,C
Step 6: Stop


BEGIN
READ r
CALCULATE A and C
A=3.14*r*r
C=2*3.14*r
DISPLAY A, C
END

2.To check greatest of two numbers a,b

Step 1: Start
Step 2: Get a,b value
Step 3: check if(a>b) print 'a is greater'
Step 4: else print 'b is greater'
Step 5: Stop


BEGIN
READ a,b
IF (a>b) THEN
    DISPLAY 'a is greater'
ELSE
    DISPLAY 'b is greater'
END IF
END

To check greatest of three numbers A,B,C

Step1: Start
Step2: Get A, B, C
Step3: if(A>B) goto Step4 else goto step5
Step4: If(A>C) print A else print C
Step5: If(B>C) print B else print C
Step6: Stop


















BEGIN
READ a, b, c

IF (a>b) THEN
    IF(a>c) THEN
            DISPLAY a is greater
    ELSE
        DISPLAY c is greater
    END IF
ELSE
    IF(b>c) THEN
            DISPLAY b is greater
    ELSE
        DISPLAY c is greater
    END IF
END IF
END

Print odd numbers upto n

step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check if(i<=n) goto step 5 else goto step 8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop



















BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
    PRINT i
    i=i+2
ENDWHILE
END

Factorial of a given number

Step 1: start
step 2: get n value
step 3: set initial value i=1, fact=1
Step 4: check i value if(i<=n) goto step 5 else goto step8
step 5: calculate fact=fact*i
step 6: increment i value by 1
step 7: goto step 4
step 8: print fact value
step 9: stop






















BEGIN
GET n
INITIALIZE i=1,fact=1
WHILE(i<=n) DO
    fact=fact*i
    i=i+1
ENDWHILE
PRINT fact
END

Comments

Popular posts from this blog

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar