1. Algorithm
Algorithm is a step by step procedure that explains the logic of problem solving
Advantages
• Neat representation of logic
• Representation makes easy to understand the solution
• Can be used to predict performance of the computation
• Ease in debugging for errors
Types of Algorithms
• Brute force - Steps start at some point and examines all the possible ways of
solving the problem until the solution is reached
• Divide and Conquer – Divides a problem into sub problems till the sub problem is very
small . Solution to main problem is combination of all solutions
• Greedy Algorithm - Tires to find the best solution but does not guarantee it.
• Dynamic programming algorithm - Remember solutions from previous results and
therefore guarantee best solutions
• Back tracking algorithm - This technique considers searching every possible
combination to solve an optimization problem
• Serial / Linear - Serial executes logic step by step
• Parallel - Multiple steps of same algorithm are executed simultaneous
2. Generalized Algorithm
• Generalized solutions can be applied to variety of inputs in a for all situations problems
• Specialized solutions are applicable to specific inputs for a particular problem
• Generalized algorithms check for all types of error conditions
Advantage
• Consider able possible states in execution and handles special cases
• Avoids using many special algorithms
Disadvantages
• Can become complicated in some cases
• Difficult to maintain if changes occur often
How to make algorithm generalized
1. List all classes of inputs to the algorithm and the expected solutions
2. Compare methods for different solutions to find common procedure
3. List different error conditions and include check for all
4. Generalized algo can only be known if specialized solutions are known
3. Infinite loop
A condition when some steps are repeated in an algorithm without limit to the number of
repetition
How to avoid
• By limiting the repetitions
• By counting - limit to a fixed count - once the count is reached
repetition stops
• By using sentinel value ( Guard value) - a special value is used to terminate
the loop - this makes loop more generalized and independent of the count
and also avoids infinite loop
• example - reading records a file
• error with fixed count for small file - invalid read
• error with fixed count for large file - partial read
• so read a file till it reaches ( end of file character )
4. Ways of representing algorithms
• Flowchart
• Pseudo code
• Program
( A) Flowcharts
• Chart or diagram representing flow of the program
• Different type of steps are represented by different shapes
• Diagrams help to visualize the steps in the logic or procedure
(B) Pseudo code
• Easy step by step explanation of logic having words and syntax borrowed from
programming as well as natural language
• example pseudo code of an algorithm
1. scan two integers a & b from user
2. c=a+b
3. print c
(C) Program
• Directly executes on a computer
• This representation complex
• Need to know the programming language
5. Program planning
• Coding stage
• Before coding we need to plan the design of the program
• The plan gives the overview of the complete code
• It becomes a guideline for the developer
• Errors can be detected early
Program planning tools
(1) Flowcharts (2) Peudo code (3) Structure charts
Flowcharts
Represents the flow of the algorithm
start or end computation input output decision connector
6. Flow charts start
stop
Store 0 in sum
Store 1 to count
Sum = sum + count
Increment count
Count
> 100
False
True
Advantages
• Makes understanding easy
• Effective way for detailing
• Infinite loops can be detected easily
Limitations
• complex programs can not be
represented
• drawing becomes cumbersome
without tools and consumes more
time
7. Pseudo code
• Is a mix of programming language and natural language
• example
1. scan two integers a and b from user
2. if a > b display a
3. else display b
Advantage
• easy was to represent logic of the solution
• used for generic representation of the program
Disadvantage
• Complex programs can become confusing
8. Structure charts
• It shows the structure of the program
• Used for large programs which are divided into modules
main
add subtract multiple divide
Advantage
• shows different parts of the program as part of a big system
• useful in planning the development work
Disadvantage
• less details are mentioned and is at abstract level
9. Use of indenting in programming
What is indentation ?
• It is arrangement of text of programming code to increase the readability
• It is the number of blank spaces left from left or right margin used to separate
different blocks of the code
• Code that is differently indented can be read easily As a separate block
#include<stdio.h>
void main(){ int x,y , sum; printf(“Enter x :
“); scanf(‘%d”,&x); printf(“Enter y :”);
Scanf(“%d”; &y); sum=x+y; printf(“Sum =
%d”, sum);
}
#include<stdio.h>
void main()
{
int x,y , sum;
printf(“Enter x : “);
scanf(‘%d”,&x);
printf(“Enter y :”);
scanf(“%d”; &y);
sum=x+y;
printf(“Sum = %d”, sum);
}
• indentation shows the logical structure of the source code e.g. Loops , control;
structures etc
10. Structured programming concepts
• Structured programming emerged in 1960 is a paradigm to improve clarity , quality and
development time of a program by modularizing the programs into subroutines functions
, block structures and loop structures
• It enforces a logical structure on the program to make it efficent e.g. FORTRAN, PASCAL ,
Ada , ALGOL
• It has three basic logical elements
(a) sequence logic - order of execution
(b) selection logic - group of statements executed depending of program state
(c ) iteration logic - group of statements executed until a program state is reached
•
11. Structured Programming
Sequential Program Control
• Used for actual data processing or computation
• Easy to construct the code
• All instruction to be executed are put in sequence from start to end
• Writing a correct statement or code is important but its place in the entire flow
or sequence is equally important
• Sequential co0ntrol is the default control
• Real world problems have decision making events and repetitions that can not be
solved just by sequential control
Selection Control
• Decision control makes a decision to alter the next program flow depending on
the current state the data of the program
• It is also called branching where program flow is transferred to another block of
statements depending on whether condition is met or not
Iteration Control
• A loop statement allows a certain set of statements to be repeated for a
given finite number of times until some condition is True or False
• The number of times that the loop is executed depends on the condition
• checked before or after each cycle
13. Need of careful us of Goto statement
• Structured programming avoids use of goto statement
• Use of goto statement is a bad programming practice
• Its excessive use may lead to
• creation of a spaghetti code
• create a bad/ unreadable/ complicated code
• increase the complexity of debugging and analysis of the code
•
What is a spaghetti code ?
It is a code which has a complex and tangled control structure using many goto
statements , exceptions , threads and unstructured branching instructions
It can be reduces by keeping program organized , indented , well commented
using functions and breaking code into sections.
How to use goto statement
• always use goto for forward jump
• use goto only when you feel that using alternate structure will slowdown
execution or increase complexity
• verify whether goto/break/continue/return should not create any unreachable
code