munotes®

Pseudocode Statements and Flowchart Symbols

Chapter Two

Syllabus topic 1, "Introduction: Algorithms, History of C, Structure of C Program. Program Characteristics, Compiler, Linker and preprocessor, pseudo code statements and flowchart symbols, Desirable program characteristics."

Pages 6 to 10 of 222

In one line

Pseudocode writes an algorithm in structured, language-free statements that look like code without being code, and a flowchart draws the same algorithm as boxes joined by arrows, so that its path through every decision can be seen at a glance.

Why the same algorithm gets written three ways

Chapter 1 wrote algorithms as numbered steps in ordinary English. That is the loosest of the three notations and the easiest to write. It has one weakness: as soon as an algorithm has decisions inside loops, plain numbered steps stop showing you the shape of the thing, and you have to hold the structure in your head.

Pseudocode fixes that by borrowing the structure of a programming language, and nothing else. It has an IF that visibly ends, a loop whose body is visibly indented, and no semicolons, no data types and no library to remember.

A flowchart fixes it a different way, by making the structure visible instead of readable. Every path the program can take is a path your finger can follow.

Neither replaces the other, and your University asks for both. All three parts of her first practical end with the same instruction:

Write algorithm & draw flowchart for the same.

So the working order for this subject is: steps in English, then pseudocode when the logic gets complicated, then the flowchart, then the C.

Pseudocode

There is no official pseudocode, and that is the point

No standards body defines it. That sounds like a weakness and is actually the whole idea: pseudocode exists so that a human reader understands the algorithm without knowing any particular language. What matters is that it is consistent, indented, and unambiguous.

What follows is the ordinary academic convention, and it is the one to use in an answer book.

The statements

Input and output. READ or INPUT takes a value from the user. PRINT or DISPLAY shows one.

READ P, R, T
PRINT "Simple interest is", SI

Assignment. A value is put into a name. Write it as an arrow or as a plain equals sign, and then do not mix the two in one answer.

SET SI = (P * R * T) / 100

Decision. IF, with an optional ELSE, and always a closing ENDIF. The closing keyword is what makes the extent of the branch visible.

IF marks >= 40 THEN
    PRINT "Pass"
ELSE
    PRINT "Fail"
ENDIF

Multi-way decision. ELSE IF chains, closed once.

IF A > B AND A > C THEN
    SET MAX = A
ELSE IF B > C THEN
    SET MAX = B
ELSE
    SET MAX = C
ENDIF

Loops. WHILE tests before the body, REPEAT tests after it, and FOR counts. Two operators appear here that pseudocode spells out in words: MOD is the remainder after division, and DIV is division that throws the fraction away, so 27 MOD 10 is 7 and 27 DIV 10 is 2.

munotes.in6

Pseudocode Statements and Flowchart Symbols

WHILE n > 0 DO
    SET digit = n MOD 10
    SET n = n DIV 10
ENDWHILE

FOR i = 1 TO 10 DO
    PRINT i
ENDFOR

Modules. A named piece of work called from elsewhere.

CALL swap(x, y)

Two conventions carry most of the weight. Keywords go in capitals so the structure can be seen without reading the words. The body of every IF and every loop is indented, and the matching ENDIF or ENDWHILE sits at the same indentation as the keyword that opened it.

The same problem, in steps and in pseudocode

Chapter 1 wrote the simple interest algorithm as five numbered steps. Here it is as pseudocode:

BEGIN
    READ P, R, T
    SET SI = (P * R * T) / 100
    PRINT SI
END

For a problem this small the two notations are equally clear, and the numbered steps are shorter. The difference appears the moment there is a decision, because pseudocode shows you where the branch closes and numbered steps only tell you.

Flowcharts

The symbols

A flowchart is drawn with a fixed set of shapes, and the shape carries the meaning. Using a rectangle where a diamond belongs is a mistake even when the words inside it are right.

SymbolShapeWhat it meansExample
TerminalOval, or a rectangle with rounded endsWhere the flowchart starts and where it stopsSTART, STOP
Input / OutputParallelogramA value is read from the user, or shown to themREAD P, R, T
ProcessRectangleA calculation, or a value put into a nameSI = (PRT)/100
DecisionDiamondA question with two answers, and two arrows outIs A > B?
Predefined processRectangle with a double bar down each sideWork done by a module defined elsewhereCALL swap(x, y)
ConnectorSmall circle with a letter in itJoins two points on the same page without a long lineA
Off-page connectorFive-sided tagContinues the chart on another page1
Flow lineArrowThe order the boxes are carried out in

Two shapes do the work in nearly every chart a first-year student draws: the rectangle for doing something, and the diamond for asking something.

The rules

  1. Every flowchart begins with exactly one START terminal and ends with a STOP terminal.
  2. Arrows carry the flow, and every arrow has a head. A line without an arrowhead does not say which way the work goes.
  3. A decision diamond has one arrow in and exactly two arrows out, and both are labelled, normally Yes and No.
  4. Every other symbol has one arrow in and one arrow out.
  5. The chart flows top to bottom and left to right, unless a loop takes it back up.
  6. Where two paths finish, they join before the chart carries on, so that the flow after them is drawn once and not twice.
munotes.in7

Pseudocode Statements and Flowchart Symbols

Rule 3 is the one that is broken most often. A diamond with three arrows out is not a decision, it is two decisions that have been drawn on top of each other.

How these are drawn here

The charts below are drawn in text so they read on a phone and in a printout. The shapes stand in for the real ones like this, and in your journal and your answer book you draw the shapes from the table above, not these:

Drawn here asMeans
( ... )Terminal, an oval
/ ... /Input or output, a parallelogram
[ ... ]Process, a rectangle
< ... >Decision, a diamond
A vertical bar, and -->Flow lines

Flowchart 1: simple interest

Her practical, part (a):

To calculate simple interest taking principal, rate of interest and number of years as input from user.

        (  START  )
             |
             v
     / READ P, R, T /
             |
             v
   [ SI = (P * R * T) / 100 ]
             |
             v
      / DISPLAY SI /
             |
             v
        (  STOP  )

There is no decision in it, so the chart is a straight line. Read it against the rules: one START, one STOP, every symbol has one arrow in and one out, and the two parallelograms are input and output rather than processes.

Flowchart 2: the greatest of three numbers

Her practical, part (b):

Write a program to find greatest of three numbers using conditional operator.

Now there are decisions, and the chart earns its keep.

              (  START  )
                   |
                   v
           / READ A, B, C /
                   |
                   v
        < A > B AND A > C ? >--- Yes -->[ MAX = A ]
                   |                          |
                   No                         |
                   |                          |
                   v                          |
           < B > C ? >--- Yes -->[ MAX = B ]  |
                   |                    |     |
                   No                   |     |
                   |                    |     |
                   v                    |     |
             [ MAX = C ]                |     |
                   |                    |     |
                   +<-------------------+<----+
                   |
                   v
           / DISPLAY MAX /
                   |
                   v
              (  STOP  )

The three paths set MAX and then join, and the join is the part beginners leave out. DISPLAY MAX is drawn once, not three times, because whichever branch ran, the work after it is the same.

Follow A as 14, B as 27, C as 9 with your finger. The first diamond asks whether 14 is greater than both 27 and 9, and the answer is No, so you go down. The second asks whether 27 is greater than 9, and the answer is Yes, so you go right into MAX = B. Then you come back to the join, display 27, and stop.

munotes.in8

Pseudocode Statements and Flowchart Symbols

Flowchart 3: the leap year

Her practical, part (c):

Write a program to check if the year entered is leap year or not.

           (  START  )
                |
                v
           / READ Y /
                |
                v
  < Y divisible by 400 ? >-- Yes -->[ R = "Leap year" ]
                |                            |
                No                           |
                |                            |
                v                            |
  < Y divisible by 100 ? >-- Yes -->[ R = "Not a leap year" ]
                |                            |
                No                           |
                |                            |
                v                            |
  < Y divisible by 4 ? >---- Yes -->[ R = "Leap year" ]
                |                            |
                No                           |
                |                            |
                v                            |
     [ R = "Not a leap year" ]               |
                |                            |
                +<---------------------------+
                |
                v
            / DISPLAY R /
                |
                v
           (  STOP  )

Three diamonds in a chain, each with its two labelled exits, and one join at the end. Every process box holds an action, R = "Leap year", and not a bare piece of text. A box containing only the words Leap year would not say what the flowchart is supposed to do with them. Chapter 1 showed why the order has to be 400, then 100, then 4; the chart shows the same fact as a shape, because each No arrow leads into a test that no longer has to worry about the case above it.

The mistakes that cost marks

  • A decision with one exit, or three. Two exits, both labelled.
  • Unlabelled arrows out of a diamond. The reader cannot tell which branch is which, and neither can you a week later.
  • No arrowheads. A flowchart without arrowheads is a picture of some boxes.
  • A rectangle used for input. READ and PRINT are parallelograms. This is the commonest shape error.
  • Branches that never join. Two copies of the same ending drawn under two branches is a sign the join was forgotten.
  • C code inside the boxes. printf("%d", si); in a process box defeats the purpose. Write DISPLAY SI.
  • Pseudocode with no closing keyword. An IF without an ENDIF leaves the extent of the branch to the reader's guess.

What can be asked on this, and how to answer it

"Draw the flowchart symbols and state their use." Draw each shape, name it, and give one example of what goes inside it. A named shape with no example is half an answer.

munotes.in9

Pseudocode Statements and Flowchart Symbols

"Write algorithm & draw flowchart for the same." This is her own wording, and both halves are wanted. They must also agree with each other. Write the algorithm first, then draw the chart from it, then check that every step appears in the chart and every box appears in the algorithm.

"What is pseudocode? How does it differ from an algorithm and from a program?" Pseudocode is a structured, language-free way of writing an algorithm, using keywords and indentation but no particular language's grammar. An algorithm may be written in plain English or as pseudocode, so pseudocode is one form an algorithm can take. A program is the algorithm in a real language, which a compiler can translate and a machine can run.

A trace question on a flowchart. You may be given a chart and asked what it prints. Put your finger at START and walk it, writing down each quantity as it changes, exactly as chapter 1 traced a table.

munotes.in10

The rest of this subject

These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.

Report or request
Done!