Programming Methodology
Programming Methodology
Introduction
Algorithm is a step-by-step process of solving a well-defined computational problem. In
practice, in order to solve any complex real life problems, first we have to define the
problem and then, design algorithm to solve it. Writing and executing a simple
program may be easy; however, for executing a bigger one, each part of the program
must be well organized. In short, algorithms are used to simplify the program
implementation. The next step is making the flowchart. It is a type of diagram that
represents an algorithm or process, showing the steps as boxes of various kinds and
their order by connecting them with arrows. Then, the flowchart will be converted into
program code.
Algorithm
An algorithm is an effective method expressed as a finite list of well defined
instructions for calculating a function, starting from an initial state and initial input. The
instructions describe a computation, which will eventually produce output, when
executed. We can use algorithm to solve any kind of problems. However, before writing
a program, we need to write the steps to solve the problem in simple English language.
This step-by-step procedure to solve the problem is called algorithm.
Example
Let us take one simple day-to-day example by writing algorithm for making Maggi
Noodles as a food.
71
Step 1: Start
Step 2: Take pan with water
Step 3: Put pan on the burner
Step 4: Switch on the gas/burner
Step 5: Put magi and masala
Step 6: Give two minutes to boil
Step 7: Take off the pan
Step 8: Take out the magi with the help of fork/spoon
Step 9: Put the maggi on the plate and serve it
Step 10: Stop.
Further, the way of execution of the program shall be categorized into three ways: (i)
sequence statements; (ii) selection statements; and (iii) iteration or looping statements.
This is also called as control structure.
Sequence statements: In this program, all the instructions are executed one after
another.
Example
Write an algorithm to print Good Morning.
Step 1: Start
Step 2: Print Good Morning
Step 3: Stop
Example
Write an algorithm to find area of a rectangle.
Step 1: Start
Step 2: Take length and breadth and store them as L and B?
Step 3: Multiply by L and B and store it in area
72
73
Flowchart
In the previous section of this chapter, we have learnt to write algorithms, i.e. step-bystep process of solving a problem. We can also show these steps in graphical form by
using some symbols. This is called flowcharting.
Flowchart Symbols
Some of the standard symbols along with respective function(s) that are used for
making flowchart are as follows:
74
Symbols
Functions
1.
Start/stop
2.
Input/output
3.
Processing
4.
Decision Box
5.
Flow of control
6.
Connector
75
Solution:
Start
Input P,R,T
SI=P*R*T/100
Print SI
Stop
The following flowchart is an example of a selective execution.
Example
Draw a flowchart to find bigger number among two numbers (selective)
Solution:
Start
Input A, B
Yes
No
IS
A>B
Print B is Big
Print A is Big
Stop
76
The following are the examples (VIII & IX) of an iterative execution.
Example
Draw a flow chart to find factorial of any number.
Solution:
Start
Input N
I=1
F=1
Is I<=N
No
Yes
Print F
F=F*I
I=I+1
Stop
Example
Draw a flow chart to find biggest number among n numbers.
77
Solution:
Start
Input n
Input A
I=1
Big =A
No
Big=A
I=I+1
Is
I<=n
Yes
Input A
Print Big
Is Big<A
Yes
Stop
No
I=I+1
78
Example
Draw a flow chart to print the number from 1 to .
Solution:
Start
Input N
I=1
No
Yes
Is I<=N
Stop
Print I
In the above example I value is not at all incremented, so it will create endless loop.
This is also called infinite loop.
Note: Set of statements is executed again and again without any end is called infinite
loop.
79
EXERCISE
Multiple choice questions:
1.
2.
3.
4.
5.
program
(b)
Flowchart
(c)
statement
(d)
Algorithm
Looping
(b)
Selective
(c)
Sequence
(d)
None
Set of statements is executed again and again based upon conditional test.
(a)
Looping
(b)
Selective
(c)
Sequence
(d)
None
program
(b)
Flowchart
(c)
statement
(d)
Algorithm
Looping
(b)
Selective
(c)
Sequence
(d)
None
Define Algorithm.
2.
Define Flowchart.
3.
4.
80
5.
6.
Write an algorithm to find the sum of all even number up to given number.
7.
9.
10.
11.
Mona is confused about finite loop and infinite loop, explain her with the help of
example.
12.
81
Chapter 2
Programming Methodology
After studying this lesson, the students will be able to
understand the need for good programs;
understand how to solve problems using different ways;
get clear idea about problem solving methodology; and
understand the types of errors normally occur while writing programs.
Introduction
Learning to write computer program is very much like learning any skill. First, we
should understand the problems well and then try to solve it in a logical manner. For
example: We have read many books available in the market for describing the car
driving methods. However, we can learn driving once we actually get into the car and
start driving it. The same logic is applied in computer programming also. Computer
programming is the process of writing, testing, troubleshooting, debugging and
maintaining of a computer program.
An effective program is that which gives result of all different inputs, including wrong
input also. While creating program, we need to follow certain systematic approach. This
systematic approach comprises two steps/things, viz., program structure and program
representation. The program structure is implemented by using top-down or bottom-up
approach and is known as popular approach, while the program representation plays
an important role in making the program more readable and understandable.
82
(ii)
X+Y
83
(i)
(ii)
(iii) Do not use same name like custom, customer or account, accountant.
(iv) Do not use one letter identifiers.
Comments
A comment is a programming language construct, which is used to embed
programmer-readable annotations in the source code of a computer program. Those
annotations are potentially significant to programmers but typically ignorable to
compilers and interpreters. Comments are usually added with the purpose of making
the source code easy to understand. Hence, add comments to your code in simple
English language that describes the function of the code and the reason for your
decision to do it in a particular way as well. They are generally categorized as either
block comment or line comment. Block comment is implemented in python by
and and line comment is implemented by #.
Example
"Write a program to print all numbers from 1 to 100 using while loop in python"
A=1
while (a<100):
# While statement
print a
a = a+1
84
Indentation
Leading white space (spaces and taps) at the beginning of each statement, which is used
to determine the group of statement, is known as indentation.
Example
If A > B :
print A is Big
# Block1
else:
print B is Big
# Block2
In the above example, if statements are a type of code block. If the if expression
evaluates to true, then Block1 is executed, otherwise, it executes Block2. Obviously,
blocks can have multiple lines. As long as they are all indented with the same amount
of spaces, they constitute one block.
computer
needs
proper
instruction
set
(programs)
to
perform
the
required/assigned task. The quality of the program depends upon the instructions
given to it. However, it is required to feed/provide the proper and correct instructions
to the computer in order to yield/provide a correct and desired output. Hence, a
program should be developed to ensure proper functionality of the computer and also
should be easy to understand. A computer program should have some important
characteristics, which are as follows:
Flexibility
A program should be flexible enough to handle most of the changes without having to
rewrite the entire program. A flexible program is used to serve many purposes. For
example, CAD (Computer Aided Design) software is used for different purposes such
as; engineering drafting, printing circuit board layout and design, architectural design,
technical drawing, industrial art, etc. Most of the programs are being developed for
certain period and they need updation during the course of time.
85
User Friendly
A program that can be easily understood by a beginner is called user friendly. It must
interact with user through understandable messages. In addition, the proper message
for the user to input data and to display the result, besides making the program easily
understandable and modifiable.
Portability
Portability refers to the ability of an application to run on different platforms (operating
systems) with or without minimal changes. Since the change of platform is a common
phenomenon nowadays, due to the developments in hardware and the software,
portability has to be taken care of it. In case, a program is developed for a particular
platform, it would become obsolete after a certain period of time. At the same time, if a
program that is developed does have the ability to work on different platforms, it
makes software more useable. High language programs are often more portable than
assembly language programs.
Reliability
It is the ability of a program to do its intended function accurately even if there are even
small changes in the computer system. Moreover, the program must be able to handle
unexpected situation like wrong input or no input. The programs, which save such
ability are known as reliable. For example, if the user does/gives wrong information to
input, it should display a proper error message.
Self-Documenting Code
The source code, which uses suitable name for the identifiers (variables and methods),
is called self-documenting code. Also, giving proper name for variables and methods
would tell the reader of your code clearly -what is it doing? Hence, a good program
must have a self-documenting code.
86
(i)
(ii)
Devising a plan;
Devising a plan
It means drawing an action plan to solve the problem, once understood. A plan is
devised from data processing to the result according to the relationship that links both
of them. If the problem is trivial, this step will not require much thinking.
Evaluation
Finally, the result should be examined in order to make sure that it is valid and that the
problem has been solved completely.
method, which is closely related to the software life cycle (the various stages in the life
87
of a program), that can be adapted by each person to solve the problem in their own
style. They are given as under:
1.
Problem Definition
2.
Problem Analysis
3.
4.
Coding
5.
6.
Documentation
7.
Program Maintenance
88
Problem Analysis
In this step, the problem has to be fragmented into smaller and manageable parts. The
original problem has to be analyzed and divided into a number of sub-problems as
these sub-problems are easier to solve and their solutions would become the
components of the final program. Each sub-problem is divided into further smaller ones
and this fragmentation has to be continued to achieve simple solutions. The use of
modular programming is to get proper solution.
Modular Programming: Modular Programming is the act of designing and writing
programs as functions (a large program is divided into the small individual
components) that each one performs, a single well-defined function, which has minimal
interaction between the sub-programs. It means that the content of each function is
cohesive and there is low coupling between them. There are two methods available for
modular programming. They are: top-down design and bottom-up design.
Top-Down design: The principles of top-down design dictate that a program should be
divided into a main module and its related module. Each module should also be
divided into sub modules according to software engineering and programming style.
The division continues till the module consists only of an elementary process that is
intrinsically understood and cannot be further sub-divided.
Bottom-up design: Bottom-up design is just the opposite of top-down design. It refers
to a style of programming, in which, an application is constructed with existing
primitives of the programming language and then gradually more and more
complicated features are added till applications are written. In other words, initiating
the design with simple modules and then build them into more complex structures
ending at the top is bottom-up design.
Flowchart: The algorithm is represented in the form of a diagram with action boxes
linked by lines showing the order in which they are executed. This is known as the
flow of control. It is the diagrammatic representation of an algorithm.
Coding
The process of translating the algorithm into syntax of a given language is known as
Coding. Since algorithm cannot be executed directly by the computer, it has to be
translated into a programming language.
a = 100
while a < 10:
a=a+1
print a
In the above example, the while loop will not execute even a single time, because the
initial value of a is 100.
Runtime error: A runtime error is an error that causes abnormal termination of
program during running time. In general, the dividend is not a constant but might be a
number typed by you at runtime. In this case, division by zero is illogical. Computers
check for a "division by zero" error during program execution, so that you can get a
"division by zero" error message at runtime, which will stop your program abnormally.
This type of error is called runtime error.
Example
(a)
A=10
B=0
print A/B
(b)
During running time, if we try to open a file that does not exist in the hard disk,
then it will create runtime error.
Documentation
The documentation includes the problem definition, design documents, a description of
the test perform, a history of the program development and its different versions and a
users manual. Such a manual is designed for a naive user and illustrates the
preparation of input data, running the program and obtaining & interpreting the
results.
Program maintenance
It is not directly part of the original implementation process, but needs special
emphasis. All activities that occur after a program operation are part of the program
maintenance. Many large programs have long life span that often exceed the lifetime of
91
the hardware they run on. Usually, the expenditure for the program maintenance will
be more than the developmental cost of the program. The program maintenance
includes the following:
Finding and eliminating previously undetected program errors;
Modifying the current program, often to improve its performance, or to adapt to
new laws or government regulations, or to adapt to a new hardware, or to a new
operating system;
Adding new features or a better user interface, or new capabilities to the program;
and
Updating the documentation.
Maintenance is an important part of the life cycle of a program. It is also important as
far as documentation is concerned, since any change pertaining to a program will
require updating of internal as well as external documentation. Maintenance
documentation will include results of the program development steps, design
documents, program code and test information.
92
EXERCISE
Multiple choice questions:
1.
2.
3.
4.
5.
6.
Identifier
(b) constant
(c)
syntax
(d) expression
Runtime error
(b)
Syntax error
(c)
logical error
Testing
(b)
Syntax error
(c)
Runtime error
(d) Debugging
Testing
(b)
Debugging
(c)
logical error
(d) Algorithm
Flowchart
(b)
Identifier
(c)
Code
(d) Debugging
The program must be able to handle unexpected situation like wrong input or no
input.
7.
(a)
Error
(b)
Expression
(c)
Portability
(d) Reliability
Testing
(b)
Indentation
(c)
Debugging
8.
9.
10.
(a)
Error
(b)
Flexibility
(c)
Portability
(d) Reliability
Identifier
(b)
Expression
(c)
Syntax
(d) Task
Each module should also be divided into sub modules according to software
engineering and programming style.
(a)
(b)
Bottom up method
(c)
Coding
2.
What is an identifier?
3.
4.
5.
6.
7.
Define documentation.
8.
9.
10.
11.
12.
94
13.
14.
15.
16.
17.
18.
19.
20.
95