Introduction To Structured Programming Topic 1 and 2
Introduction To Structured Programming Topic 1 and 2
DIPLOMA IN
INFORMATION COMMUNICATION
TECHNOLOGY
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.
A programming language is a set of words, symbols and codes that enables humans to
communicate with computers.
Pascal
C
Smalltalk.
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.
Page 4 of 172
language overcomes these problems by introducing object oriented functionality in its
programming capabilities.
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).
Paradigms are not meant to be mutually exclusive; you can program in a functional, object-
oriented, event-driven style.
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.
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.
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:
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.
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.
Structural Programming
Modular Designing
Top Down Designing
Bottom Up Designing
Object Oriented Programming
(ii) Demand of Organization: The new system is developed and installed on the demand of
organization and working groups.
(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.
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.
(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:
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
(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.
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.
(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.
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).
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.
(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.
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.
(v) Display: This is a symbol for online display, Example: Monitor (VDU).
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-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).
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
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)
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-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-3: Slop=(y2-y1)/(x2-x1)
Step-4: x-(x1+x2)/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:
(b) Conditional statements: if -endif and if else-end-if are used for conditions.
Page 25 of 172
Example:
Endif
i. do while (condition)
Statement(s)
endo
statement(s)
endfor
Fact = 1
Read number
Endfor
End
Page 26 of 172
Problem-1: Draw a flow chart to input two numbers and display sum.
Problem-2: Draw a flowchart to enter principal,rate and time and display simple interest.
Page 27 of 172
Problem-3: Draw a flowchart to find the sum of the given series.
Solution,
(c) Phrase less statements, contains noun and simple verb of English , and
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.
(i) Imperative Sentence: The imperative sentences are used to show actions.
(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)
ENDCASE.
(iv) Looping: The looping or repetition of statements is shown by FOR, FOR DO, ENDFOR,
DO, DO WHILE, DO UNTIL, ENDDO etc.
Example:
ENDFOR.
Example:
IF age >=18
ELSE
Page 29 of 172
WAIT until age is not 18
ENDIF
BEGIN
WRITE a letter
IF bicycle is available
ELSE
ENDIF
BUY a stamp
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 - - -
Pay cash
Page 31 of 172
A3: Supply Stock - - - -
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:
Problem: The Delta gas company bills its customers according to the following rate schedule:
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:
Page 32 of 172
Charge table:
Go To C1 C2 C3 C4
C1: Charge=50
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
Solution
If deposit>=25000
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:
Page 35 of 172