SlideShare a Scribd company logo
1 of 13
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
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
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 )
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
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
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
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
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
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
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
•
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
Start
End
S1
S2
S3
S4
Sequential Control Selection Control Iteration Control
Start
End
S2
S3
S4
S2
S3
S4
C
S2
S3
S4
Start
C
End
True
True
False False
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

More Related Content

What's hot

Programming process and flowchart
Programming process and flowchartProgramming process and flowchart
Programming process and flowchart
hermiraguilar
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
hermiraguilar
 

What's hot (20)

Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)
 
Programming process and flowchart
Programming process and flowchartProgramming process and flowchart
Programming process and flowchart
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer   Unit 1. Problem Solving with Computer
Unit 1. Problem Solving with Computer
 
phases of algorithm
phases of algorithmphases of algorithm
phases of algorithm
 
Logic Formulation 1
Logic Formulation 1Logic Formulation 1
Logic Formulation 1
 
Unit 1 psp
Unit 1 pspUnit 1 psp
Unit 1 psp
 
STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2STRUCTURED PROGRAMMING Chap2
STRUCTURED PROGRAMMING Chap2
 
Flowcharts
FlowchartsFlowcharts
Flowcharts
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
 
Program Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State UniversityProgram Logic Formulation - Ohio State University
Program Logic Formulation - Ohio State University
 
Algorithm Design & Implementation
Algorithm Design & ImplementationAlgorithm Design & Implementation
Algorithm Design & Implementation
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Introduction to problem solving in c++
Introduction to problem solving in c++Introduction to problem solving in c++
Introduction to problem solving in c++
 
Algorithm defination, design & Implementation
Algorithm defination, design & ImplementationAlgorithm defination, design & Implementation
Algorithm defination, design & Implementation
 
Problem solving (C++ Programming)
Problem solving (C++ Programming)Problem solving (C++ Programming)
Problem solving (C++ Programming)
 
Programming logic &practices
Programming logic &practices Programming logic &practices
Programming logic &practices
 
Presentación vhdl Peter Ashenden
Presentación vhdl Peter AshendenPresentación vhdl Peter Ashenden
Presentación vhdl Peter Ashenden
 

Similar to FPL -Part 2 ( Sem - I 2013)

Similar to FPL -Part 2 ( Sem - I 2013) (20)

Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Problem-solving and design 1.pptx
Problem-solving and design 1.pptxProblem-solving and design 1.pptx
Problem-solving and design 1.pptx
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptxAlgorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
Algorithms-Flowcharts-Data-Types-and-Pseudocodes.pptx
 
Algorithms and flow charts
Algorithms and flow chartsAlgorithms and flow charts
Algorithms and flow charts
 
Debbuging
DebbugingDebbuging
Debbuging
 
Introduction to computer programming
Introduction to computer programmingIntroduction to computer programming
Introduction to computer programming
 
DAA Unit 1.pdf
DAA Unit 1.pdfDAA Unit 1.pdf
DAA Unit 1.pdf
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Pseudo code.pptx
Pseudo code.pptxPseudo code.pptx
Pseudo code.pptx
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
 
Basic syntax : Algorithm,Flow chart
Basic syntax : Algorithm,Flow chartBasic syntax : Algorithm,Flow chart
Basic syntax : Algorithm,Flow chart
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
 
White box testing
White box testingWhite box testing
White box testing
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Coding
CodingCoding
Coding
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
lect 1-ds algo(final)_2.pdf
lect 1-ds  algo(final)_2.pdflect 1-ds  algo(final)_2.pdf
lect 1-ds algo(final)_2.pdf
 

Recently uploaded

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

FPL -Part 2 ( Sem - I 2013)

  • 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
  • 12. Start End S1 S2 S3 S4 Sequential Control Selection Control Iteration Control Start End S2 S3 S4 S2 S3 S4 C S2 S3 S4 Start C End True True False False
  • 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