Algorithmic Problem Solving and Program Development Life Cycle
Algorithmic Problem Solving and Program Development Life Cycle
Computer is an electronic device that accepts data processes it and generates the
relevant output. A computer cannot perform any task on its own, but need to be instructed
about how the task is to be performed. The set of instructions that instruct the computer about
the way the task is to be performed is called a program. A program is required for
processing all kind of tasks – simple tasks like addition of 2 numbers and complex tasks like
gaming. The instructions in a program have 3 essential parts:
1. Instructions to accept the input data from the user.
2. Instructions that will act upon the input data and process it.
3. Instructions to provide the output to the user.
The person who writes the program is called computer programmer. The programmer
has to follow the program Development life cycle for writing a program. The program
development life cycle has the following steps:
1. Problem Analysis
2. Program Design
3. Program Development
4. Program Documentation & Maintenance
Problem Analysis:
The programmer should first understand the problem to be solved. There are various
ways in which the problem can be solved. The programmer has to choose a single way to
solve the problem.
Program Design:
The selected solution is represented is
1. Algorithm – An algorithm is an English-like explanation of the solution.
2. Flow Chart – Flow chart is a diagrammatic representation of the solution, for easy
understanding & clarity
3. Pseudo code – Pseudo code is a shorthand way of describing a program.
Program Development:
The computer programming languages are of various kinds Low-level languages like
C, C++ and java. The process of translating the pseudo code into syntax of a suitable
programming language is called as Coding. The coded pseudo code or program is compiled
for syntax errors. Syntax errors come due to the incorrect use of the programming language.
During compilation, the syntax errors are removed. The compiled program is then executed
to generate the output result. The program is tested with various inputs to see that it generates
the desired results. If incorrect results are obtained, then the program has semantic or logical
errors which have to be removed to get correct result.
Program Documentation & Maintenance:
The program has to be properly documented so that later on, anyone can use it and
understand its working. The maintenance phase maintains the program for updating,
removing errors, changing requirements, etc.
ALGORITHM:
Algorithm refers to the logic of the program. It is defined as a sequence of instructions and if
the instructions are executed in the specified sequence, then the desired result will be
obtained. It is defined as any problem whose solution can be expressed in a list of executable
instruction.
Characteristics:
Instructions should be precise (exact and accurate) and unambiguous (expressed in a
clear way)
Instructions should not be repeated infinitely.
Algorithm should ultimately terminate.
Instructions should be written in sequence
It should look like normal English
The desired result should be obtained only after the algorithm terminates.
Qualities of a good algorithm:
There are many methods or logics available to solve the problem. Therefore, for a given
problem, there are so many algorithms. The following are used to judge the quality of the
algorithm.
Time: The computer takes some amount of time for the execution of a program. The
lesser is the time required, the better is the algorithm
Memory: The computer takes some amount of memory storage to execute a program.
The lesser is the memory requires, the better is the algorithm
Accuracy: Multiple algorithms provide solutions to a given problem, but some
provide more accurate results than others, such algorithms may be suitable.
Sequence: The instructions of an algorithm must form in a sequence or repeated a no.
Of times until a particular condition is met.
Generability: The designed algorithm must handle a range of input data. So the
algorithms must be generalised.
Representation of Algorithms:
The algorithm can be represented in any one of the following ways:
Normal English: The algorithm is represented step by step in normal English. Such
algorithms are easy to understand, read and write.
Flow chart: The flow chart is a pictorial representation of an algorithm. The steps are
represented using the standard symbols in flow chart. It is a formal design tool.
Pseudo code: It is also a formal design tool and utilized very well with rules of
structured design and programming.
Decision table: It provides an easy look at a complex, nested selection to help clarify
the conditions to be tested and how those conditions should be nested to arrive at the
proper actions.
Program: The algorithms can be represented using any high-level language, called as
program.
BUILDING BLOCKS OF AN ALGORITHM:
An algorithm is a sequence of simple steps that can be followed to solve a problem.
Algorithms can be designed using the following basic methods.
Statements
State
Sequence
Selection / Conditional
Control flow
Functions
An algorithm is an effective procedure for solving a problem in a finite number of
steps. An algorithm is a series of specific steps which solve a particular problem.
Series – The steps must be in particular order and each of the steps must be used.
Specific – A step must not be replaced by similar step.
Steps – Each action to be done is a step.
Solve – An algorithm produces final result or output which is the solution to the problem.
Particular problem – The algorithm for one problem will not usually solve a different
problem.
Statements:
Most algorithms have these basic statements
Description of the problem
Setup
Parameters
Execution
Conclusion
Example: To find the sum of 2 numbers
1. Description of the problem: To find the sum of two numbers.
2. Setup: Two numbers are required for addition and one variable to store the result
3. Parameters: read the first number. Read the second number
4. Execution: Calculate the sum of two numbers read.
5. Conclusion: The desired output is given.
Sequential Control:
Steps of an algorithm are carried out in a sequential manner, which each step is
executed exactly once.
Example: Temperature Conversion – To convert temperature in Fahrenheit degree to
Celsius degree.
Algorithm:
1. Start
2. Read the temperature in Fahrenheit
3. Apply the conversion formula
Celsius_degree = (5/9)*(Fahrenheit_degree-32)
4. Display the result
5. Stop
Selection / Conditional Control:
Based on the condition, only one of a number of alternative steps is executed.
Example 1: To find the greatest of two numbers.
Algorithm:
1. Start
2. Read two numbers A, B
3. Compare A and B. If A is greater perform step 4 else perform step 5.
4. Output A is greatest
5. Output B is greatest
6. Stop
Example 2: To find the greatest among three numbers.
1. Start
2. Read three numbers A, B and C
3. Compare A and B. If A is greater perform step 4 else perform step 5
4. Compare A and C. If A is greater, Output A is greatest else output C is greatest.
5. Compare B and C. If B is greater output B is greatest else output C is greatest.
6. Stop
Repetition or Control flow:
One or more steps are performed repeatedly. One or more instructions may be
executed several times depending on some conditions.
Example: To compute and print the sum and average of ten numbers.
1. Start
2. Initialize variables sum and avg to be 0
3. Repeat step 4 and step 5 for 10 times
4. Read the value of number
5. Add the value of the number to the sum
6. Calculate the average using avg=sum/10
7. Print the sum and avg value.
8. Stop
NOTATIONS
A notation is a system of characters, expressions, graphics or symbols, designs used in
problem solving to represent technical facts. Notations can be of
• 1. Pseudocode 2. Flowchart
PSEUDOCODE
It is a formal design tool and it is a programming analysis tool. ‘pseudo’ - initiation; ‘code’ –
statements. Pesudocode is called as Program Design Language (PDL). The programmer
concentrates on the logic of the problem. Pseudocode is the statements that cannot be
understood by the compiler. It is a shortened way of describing program which can be
understand by the programmer. This pseudocode is easier for a non-programmer to
understand the working of the program. This set of instructions cannot be compiled or
executed. There is no standard for the syntax of Pseudocode and it is written in a structured
English
Notations used in writing Pseudocode
• To input the data – INPUT, GET, READ are the terms used
• To output the data – OUTPUT, PRINT, DISPLAY
• For calculations – COMPUTE, CALCULATE
• For addition – ADD
• For subtraction – SUB
• For incrementation – INCREMENT
• For initialization – INITIALIZE
The following control structures are also used while writing the Pseudocode
1. Sequence
2. Selection / condition
3. Iteration
Sequence:
The sequence structure is simply a sequence of steps to be executed in a linear order.
Example: To find the product of any two numbers
READ A, B
COMPUTE C by multiplying A with B
PRINT the result C
STOP
Selection or Conditional:
In selection, the step to be executed next is based on a decision taken. There are 2
main selection constructs – if-statement and case statement. In the if-statement, if the
condition is true then the THEN part is executed otherwise the ELSE part is executed. The
case statement is used when there are number of conditions to be checked. Depending on the
value of expression, one of the conditions is true, and the corresponding statements are
executed. If no match, then the OTHERS option which is also the default option is executed.
IF-statement
IF(condition) THEN
Statement
ENDIF
IF-ELSE statement
IF(condition) THEN
Statement 1
ELSE
Statement 2
ENDIF
Example: To find the maximum of three numbers
Pseudocode:
READ values of A, B, C
IF A is greater than B THEN
ASSIGN A to MAX
ELSE
ASSIGN B to MAX
IF MAX is greater than C THEN
PRINT MAX is greatest
ELSE
PRINT C is greatest
STOP
Case statement
CASE expression
Condition 1: Statement 1
Condition 2: Statement 2
..
..
..
..
..
Condition N: Statement N
OTHERS: default statement
Example: To print student’s grade based on the mark.
INPUT mark
CASE based on mark
CASE mark>=90
PRINT Grade S
CASE mark>=80
PRINT Grade A
CASE mark>=70
PRINT Grade B
CASE mark>=60
PRINT Grade C
CASE mark>=55
PRINT Grade D
CASE mark>=50
PRINT Grade E
OTHERS
PRINT Fail
Iteration
Iteration is used in the place where repetition of several lines of code is necessary.
For, while and do-while are the iterative statements.
For loop, repeats the execution of the statements for the specified number of times.
For count=min to max
Statements
Next count
Example: To add first 10 numbers
INITIALIZE Total=0
For count=1 to 10
READ number
Total=Total + number
NEXT count
PRINT Total
WHILE loop and the DO-WHILE loop both execute the statements while the condition is
true. In a WHILE loop the condition is checked at the start of the loop whereas in a DO-
WHILE loop the condition is checked at the end of the loop. So the DO-WHILE loop
executes at least once even if the condition is false when the loop is entered.
WHILE(Condition)
Statement 1
Statement 2
..
..
..
END
DO
Statement 1
Statement 2
..
..
..
WHILE(Condition)
Example: To add from 1 to 100
INITIALIZE SUM to ZERO
INTIALIZE I to ZERO
WHILE(I less than equal to 100)
INCREMENT I
ADD I to SUM and store in SUM
END
PRINT SUM
STOP
FLOW CHART
A flowchart is a diagrammatic representation of the logic for solving a task. A
flowchart is drawn using boxes of different shapes with lines connecting them to show the
flow of control. A flowchart represents the logic of the program clearer in a much better way
using a flowchart.
Flowchart Symbols:
A flowchart is drawn using different kinds of symbols. A symbol used in the
flowchart is for a specific purpose. The flowchart symbols are available in most word
processors facilitating the programmer to draw a flowchart on the computer using word
processor.
The different symbols of the flow chart are given in the below table.
START
STOP
Example: To find product of any 2 numbers:
START
Read A, B
C=A*B
Print C
STOP
Selection:
In a selection operation, the step to be
executed next is based on a decision taken. If the
condition is true (yes), a path is followed and if the
condition is false (no), then a different path is
followed.
while and do-while loop executes the statement while the condition is true
while loop: do-while loop:
Example: To find the sum of first 100 integers.
PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer to
perform specific tasks. The programmers have to follow all the specified rules before writing
program using programming language. The user has to communicate with the computer using
language which it can understand.
Types of programming language
• Machine language
• Assembly language
• High level language
Machine language:
The computer can understand only machine language which uses 0’s and 1’s. In
machine language the different instructions are formed by taking different combinations of
0’s and 1’s.
Advantages:
• Translation free:
Machine language is the only language which the computer understands. For executing any
program written in any programming language, the conversion to machine language is
necessary. The program written in machine language can be executed directly on computer.
In this case any conversion process is not required.
• High speed
The machine language program is translation free. Since the conversion time is saved, the
execution of machine language program is extremely fast.
Disadvantages:
• It is hard to find errors in a program written in the machine language.
• Writing program in machine language is a time consuming process.
• Machine dependent: According to architecture used, the computer differs from each
other. So machine language differs from computer to computer. So a program
developed for a particular type of computer may not run on other type of computer.
Assembly language:
Assembly language is symbolic representation of machine language. Assembly languages are
symbolic programming language that uses symbolic notation to represent machine language
instructions. They are called low level language because they are so closely related to the
machines.
Ex: ADD a, b
Assembler:
Assembler is the program which translates assembly language instruction in to a machine
language.
Advantages:
• Easy to understand and use.
• It is easy to locate and correct errors.
Disadvantages:
• Machine dependent
The assembly language program which can be executed on the machine depends on the
architecture of that computer.
• Hard to learn
It is machine dependent, so the programmer should have the hardware knowledge to create
applications using assembly language.
• Less efficient
Execution time of assembly language program is more than machine language program.
Because assembler is needed to convert from assembly language to machine language.
High level language:
High level language contains English words and symbols. The specified rules are to be
followed while writing program in high level language. The interpreter or compilers are used
for converting these programs in to machine readable form.
Translating high level language to machine language
Compiler:
A compiler is a program which translates the source code written in a high level language in
to object code which is in machine language program. Compiler reads the whole program
written in high level language and translates it to machine language. If any error is found it
display error message on the screen.
Interpreter:
Interpreter translates the high level language program in line by line manner. The interpreter
translates a high level language statement in a source program to a machine code and
executes it immediately before translating the next statement. When an error is found the
execution of the program is halted and error message is displayed on the screen.
Advantages:
• Readability
High level language is closer to natural language so they are easier to learn and understand
• Machine independent
High level language program have the advantage of being portable between machines.
• Easy debugging
Easy to find and correct error in high level language
Disadvantages:
• Less efficient
The translation process increases the execution time of the program. Programs in high level
language require more memory and take more execution time to execute.
Categories of high level languages:
1. Interpreted programming languages
2. Functional programming languages
3. Compiled programming languages
4. Procedural programming languages
5. Scripting programming language
6. Mark up programming language
7. Concurrent programming language
8. Object oriented programming language
Interpreted programming languages:
An interpreted language is a programming language for which most of its implementation
executes instructions directly, without previously compiling a program into machine
language instructions. The interpreter executes the program directly translating each
statement into a sequence of one or more subroutines already compiled into machine code.
Examples:
• Pascal
• Python
Functional programming language:
Functional programming language defines every computation as a mathematical evaluation.
They focus on the programming languages are bound to mathematical calculations
Examples:
• Clean
• Haskell
Compiled Programming language:
A compiled programming is a programming language whose implementation are typically
compilers and not interpreters. It will produce a machine code from source code.
Examples:
• C
• C++
Procedural programming language:
Procedural (imperative) programming implies specifying the steps that the programs should
take to reach to an intended state. A procedure is a group of statements that can be referred
through a procedure call. Procedures help in the reuse of code. Procedural programming
makes the programs structured and easily traceable for program flow.
Examples:
• Hyper talk
• MATLAB
Scripting language:
Scripting language are programming languages that control an application. Scripts can
execute independent of any other application. They are mostly embedded in the application
that they control and are used to automate frequently executed tasks like communicating with
external program.
Examples:
• Apple script
• VB script
Markup languages:
A markup language is an artificial language that uses annotations to text that define how the
text is to be displayed.
Examples:
• HTML
• XML
Concurrent programming language:
Concurrent programming is a computer programming technique that provides for the
execution of operation concurrently, either with in a single computer or across a number of
systems.
Examples:
• Joule
• Limbo
Object oriented programming language:
Object oriented programming is a programming paradigm based on the concept of objects
which may contain data in the form of procedures often known as methods.
Examples:
• C++
• Java
Algorithm:
1. Start
2. Read the basic of the salary
3. Calculate Dearness Allowance (DA) which is 32% of basic
4. Calculate House Rent Allowance (HRA) which is 15% of basic
5. City compensatory Allowance (CCA) is allocated to all which is 325
6. Calculate net salary by adding basic, DA, HRA and CCA
7. Stop
Pseudocode:
READ basic
INITIALIZE CCA as 325
COMPUTE DA = 0.32 * basic
COMPUTE HRA = 0.15 * basic
COMPUTE Net salary = basic +DA + HRA + CCA
PRINT Net salary
Flowchart:
Algorithm:
1. Start
2. Read the value of n
3. Assign i value to be 2.
4. Compare i and n-1. If i is less than or equal to n-1, perform step 5,6 and step 7. Else
goto step 8
5. Divide n by i and check whether remainder is 0. If so, perform step 6 else goto step 7
6. Print not prime and goto step 9
7. Increment i by 1 and repeat from step 4
8. If i is equal to n then print Prime
9. Stop
Pseudocode: Flowchart:
READ the value of n
INITIALIZE i to 2
WHILE (i <=n-1)
IF (n mod i == 0)
WRITE Not Prime
EXIT
END IF
i=i+1
END WHILE
IF (i == n)
PRINT Prime
END IF
ILLUSTRATIVE PROBLEM
GUESS AN INTEGER IN A RANGE
Algorithm:
1. Start
2. Declare hidden, guess, range = 1 to 100
3. Compute hidden = Choose a random value in a range
4. Read guess
5. If guess = hidden, then print “Guess is hit” else print “Guess not hit”
6. Stop
Pseudocode:
INITIALIZE hidden = random number
READ guess
IF (guess == hidden) THEN
PRINT Guess is hit
ELSE
PRINT Guess not hit
END IF
Flowchart:
Pseudocode:
READ n
FOR i=0 to n, then
READ a[i]
INCREMENT i
END FOR
COMPUTE min=a[0]
FOR i=1 to n, then
IF a[i]<min, THEN
CALCULATE min=a[i]
INCREMENT i
ELSE
INCREMENT i
END IF
END FOR
PRINT min
Flowchart:
INSERT A CARD IN A LIST OF SORTED CARDS
Algorithm:
1. Start
2. Read n
3. Initialize i=0
4. If i<n, then goto step 5, 6 else goto step 7
5. Read a[i]
6. i=i+1 goto step 4
7. Read item
8. Calculate i=n-1
9. If i>=0 and item<a[i], then go to step 10, 11 else goto step 12
10. a[i+1]=a[i]
11. i=i-1 goto step 9
12. Compute a[i+1]=item
13. Compute n=n+1
14. If i<n, then goto step 15, 16 else goto step 16
15. Print a[i] and increment i and goto step 14
16. Stop
Pseudocode:
READ n
FOR i=0 to n, THEN
READ a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], THEN
CALCULATE a[i+1]=a[i]
DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n, THEN
PRINT a[i]
INCREMENT i
END FOR
Flowchart:
Recursion
• A function that calls itself is known as recursion.
• Recursion is a process by which a function calls itself repeatedly until some specified
condition has been satisfied
Example 10: To find the Factorial of given number using Recursion
• Algorithm:
1: Start
2: Read number n
3: Call factorial(n) and store the result in f
4: Print factorial f
5: Stop