0% found this document useful (0 votes)
343 views35 pages

Introduction To Structured Programming Topic 1 and 2

BEST NOTES

Uploaded by

Song Benard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views35 pages

Introduction To Structured Programming Topic 1 and 2

BEST NOTES

Uploaded by

Song Benard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

MINISTRY OF EDUCATION

DIPLOMA IN
INFORMATION COMMUNICATION
TECHNOLOGY

KENYA INSTITUTE OF CURRICULUM DEVELOPMENT


STUDY NOTES

Structured Programming

MODULE I: SUBJECT NO 4

Page 1 of 172
Contents
CHAPTER 1: INTRODUCTION TO STRUCTURED PROGRAMMING ................................................................. 4
Basic Programming Concept ..................................................................................................................... 4
Structured programming and other programming languages ................................................................. 4
Development of Programming Language ................................................................................................. 5
Programming Paradigms ........................................................................................................................... 6
Computer hardware and software consideration .................................................................................... 6
CHAPTER 2: PROGRAM DEVELOPMENT AND DESIGN ................................................................................ 10
Introduction to program development................................................................................................... 10
Program Development Cycle .................................................................................................................. 10
Programming Techniques ....................................................................................................................... 11
Programming Tools ................................................................................................................................. 18
CHAPTER 3 PROGRAM STRUCTURE ............................................................................................................ 36
Introduction to program structure ......................................................................................................... 36
Format of a structured programming language ..................................................................................... 36
Basic C Programs - Variable and I/O instructions ................................................................................... 38
Program Structures in Pascal .................................................................................................................. 41
Data types, identifiers and operators ..................................................................................................... 44
Variable Types ......................................................................................................................................... 46
Operator Types ....................................................................................................................................... 48
CHAPTER 4: PROGRAM WRITING................................................................................................................ 60
Content of structured programming ...................................................................................................... 60
Steps to Develop a Program ................................................................................................................... 60
Error Handling ......................................................................................................................................... 61
CHAPTER 5: CONTROL STRUCTURES ........................................................................................................... 65
Introduction to control structures .......................................................................................................... 65
Sequence structure ................................................................................................................................. 65
Selection/Decision Structure .................................................................................................................. 65
Loops/Iterations ...................................................................................................................................... 74
Loop Control Statements ........................................................................................................................ 81
CHAPTER 6: DATA STRUCTURES.................................................................................................................. 87
Introduction to Data Structures .............................................................................................................. 87

Page 2 of 172
Primitive and Non-Primitive data Types ................................................................................................. 87
Algorithm ................................................................................................................................................ 88
Array........................................................................................................................................................ 91
Stack ........................................................................................................................................................ 95
Queue...................................................................................................................................................... 99
Linked Lists ............................................................................................................................................ 103
Graph Data Structure ............................................................................................................................ 112
Tree ....................................................................................................................................................... 118
Searching and Sorting ........................................................................................................................... 137
CHAPTER 7: SUB PROGRAMS .................................................................................................................... 150
Introduction to Subprograms ............................................................................................................... 150
Procedures and Functions .................................................................................................................... 151
Design Issues for Subprograms ............................................................................................................. 151
Local Referencing Environment ............................................................................................................ 151
Parameter-Passing Methods................................................................................................................. 152
Writing subprograms - Functions ......................................................................................................... 154
CHAPTER 8: FILE HANDLING...................................................................................................................... 158
Introduction to files in programming.................................................................................................... 158
Importance of file handling................................................................................................................... 158
File organization techniques ................................................................................................................. 158
Files Handling (Input/Output) in C programming ................................................................................. 159
CHAPTER 9: PROGRAM DOCUMENTATION .............................................................................................. 169
Define program documentation ........................................................................................................... 169
Importance of programming documentation ....................................................................................... 169
Types of program documentation ........................................................................................................ 169
Writing System/Program documenting ................................................................................................ 171
CHAPTER 10: EMERGING TRENDS OF STRUCTURED PROGRAMMING ..................................................... 172

Page 3 of 172
CHAPTER 1: INTRODUCTION TO STRUCTURED
PROGRAMMING
Basic Programming Concept
Program and Programming
A computer program is a series of organized instructions that directs a computer to perform
tasks. Without programs, computers are useless.
A program is like a recipe. It contains a list of variables (called ingredients) and a list of
statements (called directions) that tell the computer what to do with the variables.

Programming is a creation of a set of commands or instructions which directs a computer in


carrying out a task.

A programming language is a set of words, symbols and codes that enables humans to
communicate with computers.

Examples of programming languages are:

Pascal
C
Smalltalk.

Structured programming and other programming languages


Structured programming (sometimes known as modular programming) is a subset of
procedural programming that enforces a logical structure on the program being written to
make it more efficient and easier to understand and modify. Certain languages such as Ada,
Pascal, and dBASE are designed with features that encourage or enforce a logical program
structure.

Structured programming frequently employs a top-down design model, in which developers map out
the overall program structure into separate subsections.Program flow follows a simple hierarchical
model that employs looping constructs such as "for," "repeat," and "while." Use of the "Go To"
statement is discouraged.

C is called a structured programming language because to solve a large problem, C


programming language divides the problem into smaller modules called functions or
procedures each of which handles a particular responsibility. The program which solves the
entire problem is a collection of such functions. One major drawback of C language is that
similar functions cannot be grouped inside a module or class. Also functions cannot be
associated to a type or structure. Thus data and functions cannot be bound together. C++

Page 4 of 172
language overcomes these problems by introducing object oriented functionality in its
programming capabilities.

Development of Programming Language


FIRST GENERATION OF PROGRAMMING LANGUAGE
The first generation of programming language, or 1GL, is machine language. Machine language
is a set of instructions and data that a computer's central processing unit can execute directly.
Machine language statements are written in binary code, and each statement corresponds to one
machine action.

SECOND GENERATION PROGRAMMING LANGUAGE


The second generation programming language, or 2GL, is assembly language. Assembly
language is the human-readable notation for the machine language used to control specific
computer operations. An assembly language programmer writes instructions using symbolic
instruction codes that are meaningful abbreviations or mnemonics. An assembler is a program
that translates assembly language into machine language.

THIRD GENERATION PROGRAMMING LANGUAGE


The third generation of programming language, 3GL, or procedural language uses a series of
English-like words, that are closer to human language, to write instructions.
High-level programming languages make complex programming simpler and easier to read,
write and maintain. Programs written in a high-level programming language must be translated
into machine language by a compiler or interpreter.
PASCAL, FORTRAN, BASIC, COBOL, C and C++ are examples of third generation
programming languages.

FOURTH GENERATION PROGRAMMING LANGUAGE


The fourth generation programming language or non-procedural language, often abbreviated as
4GL, enables users to access data in a database.
A very high-level programming language is often referred to as goal-oriented programming
language because it is usually limited to a very specific application and it might use syntax that is
never used in other programming languages.
SQL, NOMAD and FOCUS are examples of fourth generation programming languages.

FIFTH GENERATION PROGRAMMING LANGUAGE


The fifth generation programming language or visual programming language is also known as
natural language. Provides a visual or graphical interface, called a visual programming
environment, for creating source codes. Fifth generation programming allows people to interact
with computers without needing any specialized knowledge. People can talk to computers and
the voice recognition systems can convert spoken sounds into written words.
Prolog and Mercury are the best known fifth-generation languages.
EXT : OPEN PROGRAMMING LANGUAGE
The Open Programming Language (OPL) is an embedded programming language found in
portable devices that run the Symbian Operating System. For example mobile telephones and
PDAs. OPL is an interpreted language that is analogous to BASIC.

Page 5 of 172
Programming Paradigms
A programming paradigm programming. Some languages make it easy
to write in some paradigms programming language
paradigm paradigm is a way of doing something (like programming), and not a concrete
thing (like a language).

Some of the more common paradigms are

Imperative Control flow is an explicit sequence of commands.


Declarative Programs state the result you want, not how to get it.
Structured Programs have clean, goto-free, nested control structures.
Procedural Imperative programming with procedure calls.
Functional (Applicative) Computation proceeds by (nested) function calls that avoid
any global state.
Function-Level (Combinator) Programs have no variables. No kidding.
Object-Oriented Computation is effected by sending messages to objects; objects
have state and behavior.
o Class-based Objects get their state and behavior based on membership in a
class.
o Prototype-based Objects get their behavior from a prototype object.
Event-Driven Control flow is determined by asynchronous actions (from humans or
sensors).
Flow-Driven Computation is specified by multiple processes communicating over
predefined channels.
Logic (Rule-based) Programmer specifies a set of facts and rules, and an engine
infers the answers to questions.
Constraint Programmer specifies a set of constraints, and an engine infers the
answers to questions.
Aspect-Oriented Programs have cross-cutting concerns applied transparently.
Reflective Programs manipulate their own structures.
Array Operators are extended to arrays, so loops are normally unnecessary.

Paradigms are not meant to be mutually exclusive; you can program in a functional, object-
oriented, event-driven style.

Computer hardware and software consideration


A computer system consists of two major elements: hardware and software.
Hardware the physical machines that make up a computer installation (ex. printer, monitor,
motherboard)
Software the collection of programs used by a computer (ex. Word, Excel, Turbo Pascal)
Computer software, or just software, is any set of machine-readable instructions (most often in
the form of a computer program) that directs a computer's processor to perform specific
operations.

On most computer platforms, software can be grouped into a few broad categories:
Page 6 of 172
System software is the basic software needed for a computer to operate (most notably
the operating system);
Application software is all the software that uses the computer system to perform
useful work beyond the operation of the computer itself;
Embedded software resides as firmware within embedded systems, devices dedicated
to a single use. In that context there is no clear distinction between the system and the
application software.

Factors of consideration during Choosing hardware and software


Software factors
Factors influencing choice of software includes:
(i) User requirements: the selected software or package should fit user
requirement as closely as possible
(ii) Processing time: these involves the response time e.g. if the response time is
slow the user might consider the software or package as unsuccessful
(iii) Documentation: the software should be accompanied by manual, which is
easy to understand by non-technical person. The manual should not contain
technical jargon.
(iv) User friendliness: the package should be easier to use with clear on screen
prompts, menu driven and extensive on screen help facility
(v) Controls: the software should have in-built controls which may include
password options, validation checks, audit trails or trace facilities etc
(vi) Up-to-date: the software should be up-to-date e.g. should have changes or
corrections in line with business procedures
(vii) Modification: one should consider whether the user could freely change the
software without violating copyright.
(viii) Success in the market: one should consider how many users are using the
software and how long it has been in the market
(ix) Compatibility of the software: how the software integrates with other software
particularly the operating system and the user programs
(x) Portability: one should consider how the software runs on the user computer
and whether there will be need for the user to upgrade his hardware
(xi) Cost: the user company should consider its financial position to establish
whether it can afford the software required for efficient operations rather than
the least cost package software available.

Software contracts

Software contracts include the costs, purpose and capacity of the software. The
following are covered in software contracts:

Page 7 of 172
Warrant terms
Support available
Arrangement for upgrades
Maintenance arrangements
Delivery period/time especially for written software
Performance criteria
Ownership

Software licensing
Software licensing covers the following:
Number of users that can install and use the software legally
Whether the software can be copied without infringing copyrights
Whether it can be altered without the developers consent
Circumstances under which the licensing can be terminated
Limitation of liability e.g. if the user commits fraud using the software
Obligation to correct errors or bugs if they exist in the software
Hardware factors
Custom-built hardware is a rare necessity. Most hardware is standard, compatible, off-
the-shelf components. It is cheaper, easy to maintain, and ensures compatibility with
equipment in your organization and your partners and clients.
The system analysis and design should have precisely determined what sort of
hardware is needed - down to the make and model.

The decision of hardware choice must consider many factors:

Future needs - can the equipment be expanded or added to?


Availability (is it only available overseas?)
Capacity (e.g. is the hard disk big enough to hold all your data? Is it fast
enough?)
Reliability - can it be depended on?
Cost - initial cost, running costs, upgrade costs, repair costs, training costs
Compatibility - with your other equipment, and that of your partners and clients
Warranty and support - in case of failure or problems
Ease of use and installation
Compliance with local conditions (e.g. power supplies must be 240V or
compliant with telecommunication systems)

Page 8 of 172
Choosing a supplier
After choosing the hardware equipment and the equipment makers (manufacturers),
one must choose a supplier or reseller (in other words, once you know what you want
to buy, what shop will you choose?)
Factors to consider:
Reputation for support (e.g. phone support, onsite visits, website help)
Reputation for reliability, honesty, permanence (very important!)
Knowledge of the equipment
Geographic location - can you get to them easily if you need to?
Ability to offer onsite support or repair
Prices cheap, affordable

Installation

organization appointed person to avoid the risks associated with improper installation
of the equipment. The system analyst and other development team members may be
called to assist where appropriate.

User training
It is important that the system users be trained to familiarize themselves with the
hardware and the system before the actual changeover.
The aims of user training are:

a) To reduce errors arising from learning through trial and error


b) To make the system to be more acceptable to the users
c) To improve security by reducing accidental destruction of data
d) To improve quality of operation and services to the users
e) To reduce the cost of maintenance by minimizing accidental destruction of
data or hardware
f) To ensure efficiency in system operation when it goes live
The persons to be trained include system operators, senior managers, middle managers
and all those affected by the system directly or indirectly. Training should cover current
staff and recruited personnel.

Page 9 of 172
CHAPTER 2: PROGRAM DEVELOPMENT AND
DESIGN
Introduction to program development
Program development in computing; refer to the coding of an individual software program or
to the creation on an entire information system and all related software.

Program design is the activity of progressing from a specification of some required program to a
description of the program itself.

It is the process that organizations use to develop a program. Ideally, the process is
collaborative, iterative, and tentative stakeholders work together to repeat, review, and refine a
program until they believe it will consistently achieve its purpose.

A program design is also the plan of action that results from that process. Ideally, the plan is
developed to the point that others can implement the program in the same way and consistently
achieve its purpose.

Program Development Cycle

1. Analyze Define the problem.


You must have a clear idea of what data (or input) is given and the relationship between the
input and the desired output.
2. Design Plan the solution to the problem.
Find a logical sequence of precises steps that solve the problem (aka the algorithm). The logical
plan may include flowcharts, psuedocode, and top-down charts.

Page 10 of 172
3. Design the interface Select objects (text boxes, buttons, etc.).
Determine how to obtain input and how the output will be displayed. Objects are created to
receive input and display output. Appropriate menus, buttons, etc. are created to allow user to
control the program.
4. Code Translate algorithm into a programming language.
During this stage that program is written.
5. Test and debug Locate and remove errors in program.
Testing is the process for finding errors. Debugging is the process for correcting errors.
6. Complete the documentation Organize all materials that describe the program.
Documentation is necessary to allow another programmer or non-programmer to understand
the program. Internal documentation, known as comments, is created to assist a programmer.
An instruction manual is created for the non-programmer. Documentation should be done
during the coding stage.

Programming Techniques
Software designing is very anesthetic phase of software development cycle. The beauty of heart,
skill of mind and practical thinking is mixed with system objective to implement design.

The designing process is not simple, but complex, cumbersome and frustrating with many curves
in the way of successful design.

Here are some approaches:

Structural Programming
Modular Designing
Top Down Designing
Bottom Up Designing
Object Oriented Programming

The objective of Program design are:


(i) Replace old system: The new system is used to replace old system because maintenance cost
is high in old system and efficiency level low.

(ii) Demand of Organization: The new system is developed and installed on the demand of
organization and working groups.

(iii) Productivity: The new system is installed to increase productivity of company or


organization.

(iv) Competition: The new system is a matter of status also. In the age of roaring competition, if
organization does not cope with modem technology failed to face competitions.

(v) Maintenance: The new system is needed to maintain organization status.

Page 11 of 172
1. Structured Programming
This is the first programming approach used widely in beginning. Professor Edsger Wybe
Dijkstra (1960) coins the term Structural Programming. Italian computer scientist C. Bohm and
G. Jacopini (1966) give the basic principal that supports this approach. The structured
programming movement started in 1970, and much has been written about it. It is often regarded
goto-less

The program is divided into several basic structures. These structures are called building blocks.

These are following:

(a) Sequence Structure: This module contains program statements one after another. This is a
very simple module of Structured Programming.

Sequence Structure

(b) Selection or Conditional Structure: The Program has many conditions from which correct
condition is selected to solve problems. These are (a) if-else (b) else-if, and (c) switch-case

Page 12 of 172
Conditional structure

(c) Repetition or loop Structure: The process of repetition or iteration repeats statements
blocks several times when condition is matched, if condition is not matched, looping process is
terminated. In C, (a) goto, (b) for (), (c) do, (d) do while are used for this purpose.

Loop Structure

Advantage:
Problem can be easily described using Flowchart and flowchart can be easily coded into
program because the nature of this technique is like as flowchart.
The program is easily coded using modules.
The testing and debugging is easy because testing and debugging can be performed
module-wise.
Program development cost low.
Higher productivity, high quality program production.
Easy to modify and maintain

Page 13 of 172
gotoless programming because use of goto for unconditional branching is
strongly avoided. The goto is a sign of poor program design, so many designing concepts
are not favoring it but it is used in all programming language widely. When more goto is
used in program, program become less readable and its impact is negative in program

Disadvantage:
More memory space is required. When the numbers of modules are out of certain range,
performance of program is not satisfactory.

2. Modular Programming
When we study educational philosophy, the concept of modulation can be clear without any
ambiguity. Rene Descartes (1596-1650) of France has given concept to reconstruct our
knowledge by piece by piece. The piece is nothing, but it is a module of modem programming
context.

In modular approach, large program is divided into many small discrete components called
Modules. In programming language, different names are used for it.

For example:

Q-basic, Fortran Subroutine


Pascal Procedure or Function
C, C+, C#, Java Function

It is logically separable part of program. Modules are independent and easily manageable.
Generally modules of 20 to 50 lines considered as good modules when lines are increased, the
controlling of module become complex.

Modules are debugged and tested separately and combined to build system. The top module is
called root or boss modules which charges control over all sub-modules from top to bottom. The
control flows from top to bottom, but not from bottom to top.

The evaluation of modeling is called coupling and cohesion. The module coupling denotes
number of interconnections between modules and module cohesion shows relationship among
data or elements within a module.

Page 14 of 172
Modular Programming

3. Top down Approach


(a) The large program is divided into many small module or subprogram or function or
procedure from top to bottom.

(b) At first supervisor program is identified to control other sub modules. Main modules are
divided into sub modules, sub-modules into sub- sub- modules. The decomposition of modules is
continuing whenever desired module level is not obtained.

(c) Top module is tested first, and then sub-modules are combined one by one and tested.

Top down Approach

Example: The main program is divided into sub-program A, B, and C. The A is divided into
subprogram A1, A2 and A3.The B is into B1, and B2. Just like these subprograms, C is also
divided into three subprogram C1, C2 and C3. The solution of Main program is obtained from
sub program A, B and C.

4. Bottom up Approach

In this approach designing is started from bottom and advanced stepwise to top. So, this
approach is called Bottom up approach.
At first bottom layer modules are designed and tested, second layer modules are
designed and combined with bottom layer and combined modules are tested. In this
way, designing and testing progressed from bottom to top.

Page 15 of 172
In software designing, only pure top down or Bottom up approach is not used. The
hybrid type of approach is recommended by many designers in which top down and
bottom up, both approaches are utilized.

5. Object oriented programming


In the object-oriented programming, program is divided into a set of objects. The emphasis given
on objects, not on procedures. All the programming activities revolve around objects. An object
is a real world entity. It may be airplane, ship, car, house, horse, customer, bank Account, loan,
petrol, fee, courses, and Registration number etc. Objects are tied with functions. Objects are not
free for walk without leg of functions. One object talks with other through earphone of functions.
Object is a boss but captive of functions.

Features of Object oriented Language


The program is decomposed into several objects. In this language, emphasis is given to
the objects and objects are central points of programming. All the activities are object
centered.
Objects occupy spaces in memory and have memory address like as records in PASCAL
and structure in C language.
Data and its functions are encapsulated into a single entity.
Reusability: In C++, we create classes and these classes have power of reusability. Other
programmers can use these classes.
It supports bottom up approach of programming. In this approach designing is started
from bottom and advanced stepwise to top.

Some technical terms supporting object-oriented languages are:

(i) Abstraction: The abstraction is an important property of OOP. The use of essential features
over less essential features is called abstraction. The following examples will help to understand
abstraction.

Example: The computer operators know only to operate computer, but they are unaware to
internal organization of computer.

In OOP, there are many devices used for data abstraction such as class, encapsulation, data
hiding etc.

(ii) Class: A class is a collection of similar objects. Objects are members of class. Once a class is
declared, its many members can be easily created in programs. The class binds attributes (data
and functions or methods) of member objects.

Page 16 of 172
Examples:

Class employee

char name[30];

float basic;

void getdata();

void show();

};

(iii) Polymorphism: The ability to find in many forms is called polymorphism (Poly: many,
Morphe: shape / form). For instance, + is mathematical operator, it concatenates two strings and
give sum of two digits (numbers). Here, operator + has different behavior for numerical data and
strings. Just like it, once declared function has different meaning that is called function
overloading. If operator has different meaning, it is called operator overloading.

(iv) Encapsulation: The encapsulation is a very striking feature of OOP in which data and
function is bound into single unit. Array, records, structure are also example of low level
encapsulation but term encapsulation is mostly used in object oriented language. The data and
function are encapsulated into class. External world or external function cannot access the data.
It can be accessed by its own function or method encapsulated with it into class. It hides private
elements of objects behind public interface.

(v) Inheritance: Inheritance is a hierarchy of class in which some properties of base class is
transferred to derived class. The main types of inheritance are:

(a) Single Inheritance: A derived class (child class or sub class) of single base (super or parent)
class is called single Inheritance.

(b) Multiple Inheritances: A derived class of multiple base classes is called Multiple Inheritance.

(c) Multilevel Inheritance: When derived class is derived from another derived class, such type
of inheritance is called Multilevel Inheritance.

Monolithic Design (architectural style or a software development Design pattern), describes a


single-tiered software application in which the user interface and data access code are combined
into a single program from a single platform.

Page 17 of 172
A monolithic application is self-contained, and independent from other computing applications.
The design philosophy is that the application is responsible not just for a particular task, but can
perform every step needed to complete a particular function.

Programming Tools
The programming is a solution of different problems of our real life. If efficient programming
tools are used, problems are effectively solved. We code some instructions to instruct computer
for problem solving purposes. The choice of tools depends on nature of problems.

There are many tools for programmers for programming. For instance, algorithms, flowcharts,
pseudocodes, data dictionary, decision table, data flow diagrams etc are effective tools.
Enough and adequate knowledge of programming tools are essential for programming (Software
development).

1. Flowchart
The pictorial presentation of program is called Flowchart. It is a tool and technique to find out
solution of programming problems through some special symbols. It is a way to represent
program using geometrical patterns. Prior to 1964, every manufactures use different types of
symbols, there was no uniformity and standards of flowcharting. The Standard symbols were
developed by American Standard National Institute (ANSI).

There are two types of Flow Chart:

(a) Program Flow Chart and

(b) System Flow Chart.

Page 18 of 172
Program Flow Chart

(i) Start / Stop: This oval is used to represent START and STOP of program.

(ii) Input/Output: Parallelogram is used to denote input and output of data. We take data
through it and display result also using this symbol.

(iii) Process: Rectangle is used to denote process, formula, increment, and decrement and
assigned value also.

(iv) Pre-defined Process: The predefined process is denoted by this symbol. Example: Sorting
and Display is pre-defined process and represented as:

Pre-defined Process

Page 19 of 172
(iv) Decision box: It is a symbol of decision. All type of decisions is written in it.

(v) Connector: It is used to link to segment of flowchart to complete as one.

(vi) Data flow: It is used to show data flow from one component to other component, one
process to other and one symbol to other symbol.

The John Von Neumann used flowchart to solve problem in 1945 and explained its importance
in program designing.

Illustration: In our everyday life, there is a sequence of works. These sequences of works are
called routine. We all are tied
toilet, after coming from toilet, clean hands with soap and water, brush our teeth then take bath.
Here, one sequence of work is formed.

In computer world, programming language is used to solve certain problems. The solution of
problem is a sequence of processes. In one problem, there may be many processes, the flowchart
help to arrange processes in definite order. Suppose, we have to convert Fahrenheit temperature
into Celsius, it has following five steps:

Step-1: Start
Step-2: Enter Fahrenheit temperature (F)
Step-3: Application of formula: C=(F-32)(5/9)
Step-4: Displaying the result (C)
Step-5: Stop

These types of processes involved in solution of problem are called Algorithm. It is a stepwise
presentation of problem solution in simple English without using confusing words and phrases.

When we draw flowchart, it is only conversion of steps or algorithm in special symbols.

Page 20 of 172
Flow Chart

Flowchart is like as city map. Travelers or tourists search places of historical importance through
city map. Buildings are built on the basis of building-design (map) drawn by Architect engineers.

Just like it, program is coded on the basis of flowchart. Flowchart is a language free concept, if it
is prepared, any language can be used for program coding. In software development cycle,
flowcharting is one essential phase.

System Flowchart:
System flowchart is a pictorial representation of procedure flows inside and outside of the
systems. It is also called Data Flow Chart or Procedure Chart.

Page 21 of 172
System Flowchart Symbols

(i) Manual Input: It is used to enter data manually. For example, keyboard is a manual input
device.

(ii) Process: All type of processes is denoted by Rectangle.

(iii) Magnetic Disk: Mass storage device (Hard Disk).

(iv) Magnetic Tape: Magnetic tape is also storage device.

(v) Display: This is a symbol for online display, Example: Monitor (VDU).

(vi) Document: The print out document is denoted by this symbol.

Program Flowchart and System Flowchart:

Sn. Program Flow Chart System Flow Chart


1. The pictorial presentation of program is System flowchart is a pictrorial
called Flowchart. It us a tool and technique representation of procedure flows inside and
to find out solution of programming outside of the systems. It is also called Data
problems through some special sysmbols. It Flow Chart or procedure Chart. The special
is a way to represent program using type of symbols is used to represent system
geometrical patterns. flowchart.
2. It is tool of programmers. The flowchart This is a flowchart to show data flow inside
designing to solve any problem is called or out to designed system. The system
program flowchart. The candidate system is designers draw it to show data flow inside
designed using program flowchart. the current system.

Page 22 of 172
Problem-1: The source program is stored in hard disk and after compilation; the object program
(Machine code) is also stored in hard disk. The rectangle represents compilation process. The
error messages are printed out and displayed on VDU also.

Compilation Process

Problem-2: Draw a system flow chart to update master-file.

System Flowchart to update master file

Problem-3: The headquarter of ABC Private Ltd. is linked with production, marketing and sale
department. The day-by-day information of each department is compiled at headquarter, stored
in master file and report is given to higher authority of company.

Page 23 of 172
2. Algorithm
Algorithm is a stepwise presentation of procedures of program in simple and phraseless English.
It is also a programming tools used by programmer to develop software. It is a logical
representation of program procedures in order to obtain result. So, sometimes, it is called
Procedure also. An algorithm, named after the ninth Centaury scholar Abu Jafar Muhammad
Ibn Musa Al-Khowarizmi of Bagdad (Iraq).

A program is an expression of an idea in any programming language. A programmer starts with a


general idea of a task for the computer to perform. The problem is flesh out the outline into a
complete, unambiguous, step-by-step procedure for carrying out the task. Such a procedure is
Algorithm algorithm is not same as program. An algorithm is more like the idea
behind the program.

Language independent
Simple, complete, unambiguous, step-by-step program flow
no standard format or syntax for the algorithm
helpful to understand problems and plan out solution

Example: Following algorithm is written to find out roots of quadratic equations:

Step-1: Start
Step-2: Read a,b and c
Step-3: d=b*b-4*a*c
Step-4: if D<0, root is imaginary
Step-5: x1 = (-b + sqrt (D))/(2*a)

X2=(-b-sqrt(D))/(2*a)

Step-6: Display x1 and x2

Page 24 of 172
Step-7: Stop

Example: Write an algorithm to find out area of triangle when sides are given.

Algorithm:

Step-1: start

Step-2: read a,b and c

Step-3: S=(a+b+c)/2

Step-4: A=sqrt((s-a)(s-b)(s-c))

Step-5: Display A

Step-6: Stop

Example: Write an algorithm to find out slop and mid point of coordinate.

Algorithm:

Step-1: Start

Step-2: Read x1, x2, y1 and y2

Step-3: Slop=(y2-y1)/(x2-x1)

Step-4: x-(x1+x2)/2

Step-5: y=(y1 + y2)/2

Step-6: Stop

There are no standard rule for algorithm designing, but we can use some common rules which
are not ambiguous to another computer literates:

(a) Value to variable: variable (Variable or Expression)

(b) Conditional statements: if -endif and if else-end-if are used for conditions.

Page 25 of 172
Example:

Endif

(c) looping statements:

i. do while (condition)

Statement(s)

endo

ii. for variable=value1 to final value step stepvalue

statement(s)

endfor

Example: Write an algorithm to display factrolial of given number.

Fact = 1

Read number

For count =1 to count number step 1

Fact = fact * count

Endfor

End

Page 26 of 172
Problem-1: Draw a flow chart to input two numbers and display sum.

Flow Chart to display sum of two numbers

Problem-2: Draw a flowchart to enter principal,rate and time and display simple interest.

Flowchart to Calculate Simple Interest

Page 27 of 172
Problem-3: Draw a flowchart to find the sum of the given series.

Solution,

Flow Chart to find sum of series

3. Pseudo-code or Structured English

A logical construction, which has

(a) No strict rules like as programming language,

(b) Unambiguous statements,

(c) Phrase less statements, contains noun and simple verb of English , and

(d) No adjective and adverbs structure is called Pseudo-code or Structured English.

Page 28 of 172
It is a Program Design Language (PDL) provides skeleton for program design and it can
replace flowchart used to describe system design.

General rules used in writing Pseudo-codes are as follow:

(i) Imperative Sentence: The imperative sentences are used to show actions.

Example: Add x to sum, Display result, Sort list etc.

(ii) Operators: Mostly arithmetic operators (+, -, *, / etc) and relational operators (=, =>, =>, <,
> etc.) are used.

(iii) Decision: IF, THEN, ELSE, ENDIF, CASE, ENDCASE are used for decision-making.

Example:

CASE (choice)

(Choice = 1):Display Employee, Department

(Choice = 2): Display Employee, BasicSal, Allowance, Gross

(Choice = 3): Terminate Program

ENDCASE.

(iv) Looping: The looping or repetition of statements is shown by FOR, FOR DO, ENDFOR,
DO, DO WHILE, DO UNTIL, ENDDO etc.

Example:

FOR roll1 TO roll=10 DO

Display roll, Name, Address

ENDFOR.

Example:

IF age >=18

THEN CASTE vote

ELSE

Page 29 of 172
WAIT until age is not 18

ENDIF

Example: Mailing a letter

BEGIN

WRITE a letter

IF bicycle is available

THEN GO to Post Office by Bicycle

ELSE

GO to Post Office on foot

ENDIF

BUY a stamp

STICK a stamp on the letter

PUT into letterbox

END.

4. Decision Table
A decision table defines a logical procedure by means of a set of conditions and related actions.
It is used for communicating and documenting complex decision procedures.

Procedure: The decision table is divided into four quadrants: Condition stub, Condition entry,
Action stub, and action entry. The conditions are answered as Y (yes) or N (No). The blank
space shows that the condition involved has not been tested. X (or check marks) represents
response to the answer.

Page 30 of 172
Condition Stub C1 R1 R2 R3 R4

C2 Condition entry

Cn
Action Stub A1 Action entry

A2

An

Problem
following rules:

(i) If the customer order <= that in stock and his credit is OK, supply his requirement.

(ii) If the customer credit is not OK, do not supply. Send him intimation.

(iii) If the customer credit is OK but items in stock are less than his order, supply what is in
stock. Enter the balance to be sent in a back-order file.

Decision Table:

Order <=Stock Y Y N N
y N Y N
Credit OK
A1: Supply order X - - -

A2: Credit not OK X - X

Pay cash

Page 31 of 172
A3: Supply Stock - - - -

A4: Enter (order- - - -


Stock) in back
order file

Problem: Create a decision table to decide whether on a given data a student in an University
has completed 3 years of enrolment or not.

Decision Table:

Let data enrolment Dj Mj Yj (day-month-year) and current date is Dt Mt yt (say Month


Year)

If (Yj Yt) >3 =3 =3 Else

If (Mj Mt) - >0 =0

If (Dj Dt) - - >=0


3 years enrolment? Y Y Y N

Problem: The Delta gas company bills its customers according to the following rate schedule:

(i) First 500liters Rs. 50 (flat)

(ii) Next 300 Liters Rs. 5.25 per 100 liter

(iii) Next 30,000 liters Rs. 2.50 per 100 liters

(iv) Above this Rs.2.00 per 100 liters.

The input record has customer identification, name and address, new meter reading, past and
present. Create decision table to obtain bill for customers.

Decision Table:

Consumption= old meter reading-new meter reading

Page 32 of 172
Charge table:

Consumption <=500 501 to 800 801 to 30800 >30801

Go To C1 C2 C3 C4

C1: Charge=50

C2: Charge=50+ (consumption-500)*0.0525

C3: Charge=65.75+ (consumption-800)*0.0250

C4: Charge=815.75+ (consumption-30800)*0.02

Problem. A bank has the following policy on deposits: on deposits of Rs. 25000 and above and
for three years or above the interest is 10%. On the same deposit for a period less than three
years it is 8%. On deposits below Rs. 25000 the interest is 6% regardless of the period of deposit.
Write the above process using

(a) Structural English

(b) A decision Table.

Solution

(a) for each deposit do

If deposit>=25000

Then if period>=3 years

Then interest=10%

Else interest=8%

Endif

Else interest 6%

Endfor

Page 33 of 172
(b) Decision table:

Deposit>=25000 Y Y N

Y N -
Interest % 10 8 6

5. Decision Tree
The tree like presentation of condition and actions are called decision tree. Every node denotes
conditions. It is used for logical variations and problems involving few complex decisions with
complex branching routines.

Example: The person which age is greater or equal to 18 can caste vote in the favour of his leader
otherwise has to wait until age of 18. There is two conditions (a)age<18, and (b)age>=18, both
the conditions have actions.

Decision Tree

Example: The hotel ABC offers new discount scheme foe university students. It provides 15%
and 10% discount to monthly customers for vegetarian and non-vegetarian meal respectively.
The student attend hotel occasionally have discount rate 10% and 5% for vegetarian and non-
vegetarian meal.

Page 34 of 172
Draw decision tree to show discount scheme:

Example of Decision tree

Page 35 of 172

You might also like