You are on page 1of 9

CHAPTER - 2

Programming Logic

2.1 Computer software


Computer software is the product that software professionals build and then support over the
long term. A textbook description of software might take the following form:
Software is:
1. Instructions (computer programs) that when executed provide desired features,
function, and performance;
2. Data structures that enable the programs to adequately manipulate information, and
3. Descriptive information in both hard copy and virtual forms that describes the
operation and use of the programs.

Software has characteristics that are considerably different than those of hardware:
1. Software is developed or engineered; it is not manufactured in the classical sense.
2. Software doesn’t wear out, but it does deteriorate.
3. Although the industry is moving toward component-based construction, most software
continues to be custom built.

2.2 Software Engineering


Software engineering encompasses a process, a collection of methods (practice) and an array of
tools that allow professionals to build high quality computer software in a timely manner. Thus,
the software engineering includes overall activities performed during the production of
software such as requirement analysis, planning, designing, estimation, scheduling and module
development, combination of modules for overall product, testing the deliverables, delivery to
the owner, and finally feedback from the customers. Now, the same cycle repeats again in
response to the feedback for making the software product more energetic and more prefect as
the user requirement. The main remarkable features of software engineering are listed as:
• Highly innovative and rapidly
changing field
• Few results are supported by
empirical or comparative studies;
• Work within the field older than 3–
4 years is rarely acknowledged or
referenced;
• Old problems are given new names
and old solutions overlooked;
• There is a need for interdisciplinary
work comprising e.g. mathematics,
psychology, business or
management science, etc.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 1

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


2.3 Software Development Cycle
Program development is a process of preparing software programs as per the requirement of the
user or the client and should be carried out following proper guidelines. The reliability and
usability of the program depends on how well the program development was organized and
carried out. While preparing programs factors like reliability, speed, time and cost involved, ease
of use, error tolerance,
extendibility etc. should be
well considered and justified
for. To cope with all these
requirements, the structured
approach to software
development follows the
program development cycle.
Basically the program
development cycle can be
divided into following steps:

Step 1: Defining and Understanding the Problem: This process mainly deals with the analysis
of the problems. During this step, the developer carries out a number of tasks aimed at producing
a document which spells out the input, process and output requirements. The tasks involve:
 Collecting information from the client, to identify and verify the user’s needs
 Determine the scope of the problems to be solved and define its boundaries clearly
 Examining feasibility issues
 Identifying and considering the social and ethical requirements.
 Analyze the input, output and processing requirement.

Step 2: Planning and Designing Software Solutions: Developers take the information
presented in Stage 1 of the Program Development Cycle and use it to design a solution.
Designing a Solution involves:
 Documenting the names and data types of all the data elements used in the form of a
Data Dictionary.
 Using Abstraction and Enhancement techniques to develop Modularized Solutions.
 Developing Algorithm Solutions with flowcharts or pseudocode.
 Using Structure Charts, Data Flow Diagrams, or System Flow Charts to show the
relationship between Modules or processes.
 Designing Test Data to test the solution.

Step 3: Implementation of Software Solutions: Implementation of Software Solutions involves


converting paper based algorithm solutions into solutions that work on a computer system. This
involves: Implementing screen designs
 Coding algorithms using an appropriate language (Sequential or Event driven)
 Translation of the code (Compiler or Translators)
 Detecting and correcting errors using appropriate techniques (breakpoints, traces,
stepping, and output debugging statements).

Step 4: Testing and Evaluation of Software Solutions: Testing is the process of making sure
that the software solution meets all the defined requirements. Evaluation has more to do with
Quality Assurance and examining what people think of the solution measuring its functionality,
user friendliness, flexibility, probability etc. Testing can take place on a number of different
levels including: The module level, program level, system level, user level etc.

Step 5: Documenting Software: After fully development of software, it needs to be documented


so that it can be understood easily by any user, developer. Documentation helps any user can

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 2

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


easily handle the software. Similarly, any other developer can easily update the software if the
source code is available. It should contain as follows:
 Introduction of software
 Hardware & software requirement to operate it
 Algorithm with Pseudocode, flowchart, Data flow diagrams
 Coding
 Limitation, appendix, glossary

Step 6: Maintenance of Software Solutions: Maintenance is the process of modifying a


solution for an important reason. Modifications may be undertaken to:
 Remove a bug from the program
 Improve the efficiency of the code
 add new function to fulfill new user requirement
 Improve the user interface
 Furnish for new hardware

2.4 ALGORITHM, FLOWCHART and PROGRAMS


The second step in the software development cycle is to design algorithm and flowchart. This
step is an important step in the program development cycle. A programmer writes a program
after developing an algorithm and drawing a flowchart as

ALGORITHM FLOWCHART PROGRAM

Once the algorithm is ready, it becomes ready and easy to code program. In fact, algorithm and
flowchart is a roadmap to solution of the program. Flowchart is descriptive solution of the
problem where as flowchart is the pictorial solution of the problem. Let us analyze algorithm and
flowchart separately.

A. Algorithm
Algorithm is a set of instruction which will produce a solution to a given problem is executed
in a specified sequence. An algorithm tells a person what to do next. Thus, an algorithm is a
roadmap for solving a problem. The word ‘algorithm’ is named after a famous ninth century
Arabic author and mathematician Abu Jafar Mohammed Ibn Musa Al Khowarizmi.

Some of popular definition of algorithm:


 An algorithm consists of a set of explicit and unambiguous finite steps which, when
carried out for a given set of initial conditions, produce the corresponding output and
terminate in finite time.
 Algorithm is a step-by-step procedure for solving a problem; programming languages
are essentially a way of expressing algorithms.
 An algorithm is a set of instructions to be carried out step by step in order to produce
the desired correct solution to the problem. Programming languages are the tools used
to implement the algorithm.

In any solution of problem, a programmer would usually have a choice of algorithms and
each algorithm has its own advantages and limitations but important job is that to decide
which algorithm is best. On solving the problem we must write the instruction in such a way
that the computer can easily execute it.

Let us understand the concept of algorithm by taking a simple example. You must have heard
a recipe for making an omelet. The recipe tells the stepwise instructions to make omelet as:

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 3

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Example 1:
Step 1: be ready.
Step 2: burn stove.
Step 3: put the pan on the stove.
Step 4: mix the liquid of an egg, salt, onion.
Step 5: pour the mixture in the pan.
Step 6: fry it properly.
Step 7: stop

You have an omelet. Yes the sequence of stepwise instructions to make omelet is an
algorithm. It would be better to understand the concept of an algorithm with a proper
example.

Example 2:
Write an algorithm to add any two numbers.
Step 1: start
Step 2: read two numbers from user
Step 3: assign these numbers in two variables x and y.
Step 4: add the value of x and y put the result in third
variable z.
Step 5: display the value of z.
Step 6: stop.

This is the set of stepwise instructions (algorithm) to add any two numbers. But honestly
speaking, the above example-2 is an informal algorithm. It is not necessary to write an
instruction in a language if arithmetic expression is possible, i.e. instead of “read two
numbers from user----assign these numbers in two variables x and y”, it would be better to
write “read x and y”. Similarly instead of writing “add the value of x and y and put the result
in z” we can write its equivalent arithmetic expression as “z= x + y” as shown in following
example.
Step 1: start
Step 2: read x and y
Step 3: z=x + y
Step 4: print “z”
Step 5: stop.

Example 3:
Write an algorithm to find area and perimeter of a rectangle and area and circumference of
the circle.

Step 1: start
Step 2: read Length (L), Breadth (B), radius (R)
Step 3: A1=L*B
P=2*(L+ B)
A2=3.14*R*R
C=2*3.14*R
Step 4: display the value of A1, P, A2 and C
Step 5: stop

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 4

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Example 4:
Consider the following algorithm to calculate the simple interest and compound interest for
the amount deposited for some years and for the rate of interest.

Algorithm: Simple and Compound interest


Step 1: Start of Program
Step 2: Read the value of P, T, R
Step 3: Compute the simple interest
SI = (P * T* R)/100
Step 4: Compute the compound interest
CI = P * (1+R/100) ^T – P
Step 5: Display the Simple interest and compound interest
Step 6: End

Example 5:
Write an algorithm to find the area of the triangle when three sides are given.

Algorithm: Area of the triangle when side is given


Step 1: Start of program.
Step 2: Read the sides value a, b and c
Step 3: Find the semi-perimeter by s = (a + b+ c)/2
3.1. Find area = ) ^0.5
Step 4: Display the area
Step 5: End of program

Characteristics of a good algorithm


Each and every algorithm should possess some of the properties. All algorithms must have
some characteristics. They are explained below:
 Input: An algorithm may take a zero or any number of inputs. The number of inputs
taken by an algorithm depends upon the nature of the problem.
 Output: An algorithm must produce one or more outputs. To put in other word, every
algorithm must give solution to the problem in the form of output.
 Termination or finiteness: An algorithm must terminate after a finite number of steps.
It should end after certain time. It should not enter into to an infinite loop.
 Generality: It indicates that the algorithm for a problem should be general. It means
the algorithm should work for all the values. For example, if you wrote algorithm for
adding two numbers, the algorithm should add integer as well as float values.
 Effectiveness: It says that operation must be simple and should be done in a finite time
to produce effective result.
 Definiteness: It denotes that each instruction written in algorithm must be clear, well-
defined and precise. It should not be ambiguous.

Advantages and disadvantages of algorithm


The term algorithm is now applied to many kinds of problem solving that employ a sequence
of steps. The sequence may be displayed in the form of a flowchart in order to make it easier
to follow. The algorithms are used basically to solve the problem complexity in simplified
manner such that the problem can easily manipulate on software program. Many different
applications algorithms are now available, and highly advanced systems such as artificial
intelligence algorithms may become common in the future. However, it is really time
consuming and causes waste of resource to write algorithm.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 5

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


B. Flowchart
A flow chart is a graphical/ diagrammatic/ pictorial representation of series of instructions to
be executed to perform a given task. It is a diagram or chart that shows the connections
between different stages of a process. As the name says, it is a chart that shows the flows of
instructions or actions in pictorial form. A flowchart clearly describes what to do next.

A flowchart uses different graphical symbols to denote different types of instructions. The
instructions are written inside these special symbols. The graphical symbols are connected by
solid lines having arrow marks to indicate the flow of operation.

Symbol Explanation
Start and end of the program is denoted by the oval
symbol.
Processing or mathematical operations are represented
by rectangle.
The parallelogram symbol indicates the input and output
operation.
This symbol indicates the decision making. It allows the
program for branching an looping.

This symbol represents the reference to another point in


the flowchart where it continues.
These arrows indicate the sequence of operation.

This symbol indicates a comments or remarks made


…………. about the program codes.

Rules to be considered
While drawing the flowchart, we should consider the following rules or guidelines:
 The default flow of direction is from top to bottom and left to right.
 Arrow head should be used to indicate the direction of flow or the sequence of
operation.
 Only one flow line can enter a processing symbol, decision symbol and termination
symbol.
 Do not intersect the flow lines as far as possible.
 Make your flowchart clear, logical and neat.
 Test the validity of the flowchart by taking some sample data.

Example 1: Write a flowchart to find the area of the


triangle when three sides are given.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 6

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Example 2: Draw an algorithm and flowchart
to reverse a given integer numbers.

Algorithm: To reverse an integer


Step 1: Start of program.
Step 2: Read any integer number
Step 3: Initialize temp = N
Step 4: [Repeat until N is greater
than zero and compute the reverse]
4.1 Reverse = 0
4.2 digit = N mod 10
4.3 Reverse = Reverse * 10 +digit
4.4 N= N/10
Step 5: Print Reverse
Step 6: End the program

Example 3: Draw a flowchart to


find the largest number among
three input numbers.

Example 4: Write an algorithm and


corresponding flow chart to find the
factorial of an inputted number.

Algorithm
1. Start
2. Initialize variable i and Fact
to 1
3. Input any positive number
and read on N
4. Multiply Fact and i, the
result store on Fact
5. Increase i by 1
6. If i <= N then go to step 4
7. Otherwise, print the value of
Fact as output
8. Stop

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 7

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Advantages of Flowcharts
It is easier to explain the logic used to a non-technical person as well. The flowchart contains
instructions written in natural language. (e.g. English). So a non technical professional can
easily understand the logic behind the problem. The flow of instruction to be executed can be
interpreted easily. It is better to draw a flowchart before writing a program because
programmers find it very easy to write corresponding program, because flowchart is a
roadmap for program. A flowchart guides a programmer to go from starting point of the
program to the final point. By the help of a flowchart one can write an error free program at a
high speed.
 Communication: Flowcharts are better way of communicating the logic of a system to
all concerned.
 Effective analysis: With the help of flowchart, problem can be analyzed in more
effective way.
 Proper documentation: Program flowcharts serve as a good program documentation,
which is needed for various purposes.
 Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.
 Testing and Debugging: The flowchart helps in testing and debugging effectively.
 Efficient Program Maintenance: The maintenance of operating program becomes
easy with the help of flowchart.

Limitations of flowcharts:
A professional programmer thinks that drawing a flowchart is a time consuming process.
Drawing a flow chart is wastage of time for a professional programmer because without
drawing a flowchart they can think the logic solve a problem. Any change (even a small
change) in already existing flowchart requires a completely new flowchart. Oops, redrawing a
complex flowchart is tedious, time consuming, and boring task.
 One can misunderstand the flowchart if the proper symbol is not used and hence results
a wrong program.
 Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
 Alterations and Modifications: If alterations are required the flowchart may require
re-drawing completely.
 All the logic of vary large program cannot be represented by flowchart.

C. PSEUDOCODE
Pseudocode is a famous programming analysis tool which is very close to the program.
Pseudo means false and code means command or instruction. Therefore pseudo code means a
false code. Here false does not mean wrong. The code which is exactly not real code in a
particular programming language but looks like a program is the real meaning of pseudo
code. Pseudo code consists of code which is not exactly the instructions of a particular
programming language. Pseudo code consists of codes that look like a programming
language instructions. Pseudo codes are phrases written in ordinary natural language (eg
English, Japanese etc). The pseudo code is not Set sum to zero
recognized by a computer because they are not Set I to 1
written in a computer language. The main Level 1
advantage of writing a pseudo code is that a Add I to sum
programmer can replace the written pseudocodes Increase I
in language independent way. Figure shows a IF I<=10 THEN GOTO LEVEL 1
pseudo code to find the summation of natural Print the value of sum
number 1, 2, 3, 4……..10. End

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 8

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)


Advantages of Pseudocode
 Pseudo code is the best programming tool for beginners. A beginner can develop the
logic to be used to solve a particular problem without knowing the syntax of any
computer language.
 Pseudo code does not need any symbols and syntax so it is easy task to write a pseudo
code.
 A pseudo code can easily be converted in to a program because pseudo code is much
closer to the program than that of a flowchart.
 As compared to flowchart or any other program analysis tool, pseudo code is easier to
modify.
 Since, pseudo code does not include any graphical symbols. Therefore, one has to
invest less time to write a pseudo code.
 As compared to program the pseudo code does not have any hard and fast syntax rule.
Therefore a pseudo code is easier to write than a program.
 A non technical professional can easily understand the pseudo code.

Limitations of Pseudocode
 Since pseudo code is not recognized by computers. Therefore it is not for computers. It
means a pseudo code cannot be submitted to computers. Computer cannot execute a
pseudo code.
 There are no any fixed and standard rules to write a pseudo code. Different
programmers use their own style of writing pseudo code. Hence one can misunderstand
the pseudo code written by the other programmer.
 Most of the professional, programmer can develop the logic of the program without
going through pseudo code. So they think writing a pseudo code is a useless step.

Programming in C - Compiled by Yagya Raj Pandeya, NAST, Dhangadhi ©yagyapandeya@gmail.com Page 9

Print to PDF without this message by purchasing novaPDF (http://www.novapdf.com/)

You might also like