SlideShare ist ein Scribd-Unternehmen logo
1 von 37
ALGORITHMS AND FLOWCHARTS
Why Algorithm is needed?
2
Computer Program ?
Set of instructions to perform some specific task
Is Program itself a Software ?
NO, Program is small part of software.
Software merely comprises of Group of Programs (Source code),
Code: refers to statements that are written in any programming
language as machine code or C code.
• Code is some time interchangeably used with program.
Why Algorithm is needed?
3
• Programming is both tedious and exciting.
Tedious because like spoken languages programming languages
also have so many demanding rules.
Exciting because writing program provides the programmer with
the chance to create something new also gives challenges of
solving a problem.
It is very difficult to write direct programs in any language, just
like you can not start constructing building without the design of
building.
For constructing a building you need design of building, similarly
for writing a large or good program you need algorithm.
Program design
4
Program Design Process has 2 phases:
Problem Solving Phase
Creates an algorithm that solves the problem
Implementation (Coding) Phase
Translates the algorithm into a programming language
Algorithm
Problem Program
Algorithms
5
An algorithm is a finite set of steps defining the solution of a
particular problem.
Need not to belong one particular language
Sequence of English statements can also be algorithm
It is not a computer program
An algorithm can be expressed in English like language,called
pseudocode, in a programming language or in the form of
flowchart.
Algorithm Vs Program
6
What is the difference between an algorithm and a program?
 a program is an implementation of an algorithm to be run on a
specific computer and operating system.
 an algorithm is more abstract – it does not deal with machine
specific details – think of it as a method to solve a problem.
• What is good algorithm?
Efficient algorithms are good, we generally measure efficiency of
an algorithm on the basis of:
1. Time: algorithm should take minimum time to execute.
2. Space: algorithm should use less memory.
Algorithm Specification
7
Every algorithm must satisfy the
following criteria:
Input. Zero or more quantities are externally supplied.
Output. At least one quantity is produced.
Definiteness. Each instruction must be clear and
unambiguous(Unique meaning).
Finiteness. An algorithm terminates in a finite number of
steps.
Effectiveness. Every instruction must be basic enough to be
carried out than, means not so complex.
Pseudo code
8
Pseudocode is one of the methods that could be used to represent an
algorithm.
 It is not written in a specific syntax that is used by a programming
language and therefore cannot be executed in a computer.
There are lots of formats used for writing pseudocodes and most of
them borrow some of the structures from popular programming
languages such as C, Lisp, FORTRAN, etc.
 Also, natural language is used in Pseudocode when presenting
details that are not important.
Most of the algorithms are presented using pseudocode since they
can be read and understood using programmers who are familiar
with different programming languages.
Some programming constructs used for Pseudo Code –
READ, PRINT, SET, INITIALISE, INCREMENT, IF – THEN – ENDIF, IF –
THEN – ELSE – ENDIF,REPEAT – UNTIL, DO – WHILE etc.
What is the difference between
Algorithm and Pseudocode?
9
An algorithm is a well defined sequence of steps that
provides a solution for a given problem, while a pseudocode
is one of the methods that can be used to represent an
algorithm.
 While algorithms can be written in natural language,
pseudocode is written in a format that is closely related to
high level programming language structures.
 But pseudocode does not use specific programming language
syntax and therefore could be understood by programmers
who are familiar with different programming languages.
Additionally, transforming an algorithm presented in
pseudocode to programming code could be much easier than
converting an algorithm written in natural language.
10
Informal definition of an algorithm
11
Finding the largest integer
among five integers
12
Defining actions in Find Largest algorithm
13
ALGORITHMALGORITHM
REPRESENTATIONREPRESENTATION
14
Example 1Example 1
Write an algorithm that finds the average of
two numbers
Solution:
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2 2
End
15
Example 2Example 2
Write an algorithm to change a numeric
grade to a pass/fail grade.
Solution:
Pass/FailGrade
Input: One number
1. if (the number is greater than or equal to 40)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “fail”
End if
2. Return the grade
End
16
Example 3Example 3
Write an algorithm for grading System of
JUET.
Marks range Grade
>=80 A
>=70 & <80 B
>=60 & <70 C
>=50 & <60 D
<50 F
17
AlgoForGrade
Input: One number
1. if (the number is between 80 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 70 and 79, inclusive)
then
2.1 Set the grade to “B”
End if
Algorithm for GradingAlgorithm for Grading
Continues on the next slide
SolutionSolution
Dr. Vishnu Sharma18
3. if (the number is between 60 and 69, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 50 and 59, inclusive)
then
4.1 Set the grade to “D”
End if
5. If (the number is less than 50)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
19
Example 5Example 5
Write an algorithm to find the largest of 1000 numbers.
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
SolutionSolution
Flowchart
20
A graphical representation of an algorithm, often used
in the design phase of programming to work out the
logical flow of a program.
Visual way to represent the information flow
Make our logic more clear
Help during writing of program
Make testing and debugging easy
21
Flowchart or program constructs
 Sequence: The order of execution, this typically refers to the
order in which the code will execute. Normally code executes
line by line, so line 1 then 2 then 3 and so on. 
 Selection: Selection, like branching, is a method of controlling the
execution sequence, you can create large control blocks, using if
statements testing a condition, or switch statements
evaluating a variable etc to control and change the execution of
the program depending on this environment and changing
variables. 
 Iteration (Repetition): Iteration is typically used to refer to
collections and arrays of variables and data. Repeating set of
instruction. Counting from 1 to 10, you are iterating over the first
10 numbers. for, while, do-while loops will be implemented for
iteration.
22
Flowchart Constructs
23
Flowchart Constructs (cont..)
24
Flowchart Constructs (cont..)
25
Example-1
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Algorithm
Step 1: Take Width and Length as input
Step 2: Calculate Area by Width* Length
Step 3: Print Area.
Example-1
Pseudocode
Step 1: Input W,L
Step 2: A ← L x W
Step 3: Print A
START
Input
W, L
A ←L x W
STOP
Print
A
Example-2
Write an Pseudocode and draw a flowchart that will
take marks of four subjects and calculate the
average.Then if average marks are greater than 50 then
print PASS otherwise print FAIL.
28
Example-2
Step 1: Input M1,M2,M3,M4
Step 2: AVG ← (M1+M2+M3+M4)/4
Step 3: if (AVG <50) then
Print “FAIL”
else
Print “PASS”
endif
START
Input
M1,M2,M3,M4
AVG←(M1+M2+M3+M4)/4
IS
AVG<50
STOP
YN
Print
“PASS”
Print
“FAIL”
Example 3
Write an algorithm and draw a flowchart to convert the
length in feet to centimeter.
Algorithm:
 Input the length in feet (Lft)
Calculate the length in cm (Lcm) by multiplying LFT with 30
Print length in cm (LCM)
Example 3
Pseudocode
Step 1: Input Lft
Step 2: Lcm ← Lft x 30
Step 3: Print Lcm
START
Input
Lft
Lcm ←Lft x 30
Print
Lcm
STOP
Flowchart
Example 4
Write an algorithm and draw a flowchart that will
calculate the roots of a quadratic equation
 Hint: d = sqrt ( ), and the roots are: x1 = (–b
+ d)/2a and x2 = (–b – d)/2a
2
0ax bx c+ + =
2
4b ac−
Example 4
Algorithm:
Input the coefficients (a, b, c) of the quadratic equation
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
Example 4
Pseudocode:
 Step 1: Input a, b, c
 Step 2: d ← sqrt ( )
 Step 3: x1 ← (–b + d) / (2 x a)
 Step 4: x2 ← (–b – d) / (2 x a)
 Step 5: Print x1, x2
START
Input
a, b, c
d ← sqrt(b x b – 4 x a x c)
Print
x1 ,x2
STOP
x1 ←(–b + d) / (2 x a)
X2 ←(–b – d) / (2 x a)
4b b a c× − × ×
Example 5
Write an algorithm that reads three numbers and prints the
value of the largest number.
Example 5
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX ← N1 //[N1>N2, N1>N3]
else
MAX ← N3 //[N3>N1>N2]
endif
else
if (N2>N3) then
MAX ← N2 //[N2>N1, N2>N3]
else
MAX ← N3 //[N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX
Flow Charts Limitation
 For very large program, flow chart goes for many pages
 Costly to draw flow charts for large program
 Difficult to modify
37

Weitere ähnliche Inhalte

Was ist angesagt?

Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
ankitamakin
 

Was ist angesagt? (20)

Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
Flowcharts
FlowchartsFlowcharts
Flowcharts
 
The Loops
The LoopsThe Loops
The Loops
 
structured programming
structured programmingstructured programming
structured programming
 
Finite automata examples
Finite automata examplesFinite automata examples
Finite automata examples
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
10. switch case
10. switch case10. switch case
10. switch case
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Loops c++
Loops c++Loops c++
Loops c++
 
Principles of programming
Principles of programmingPrinciples of programming
Principles of programming
 
Programming flowcharts for C Language
Programming flowcharts for C LanguageProgramming flowcharts for C Language
Programming flowcharts for C Language
 
Data Type in C Programming
Data Type in C ProgrammingData Type in C Programming
Data Type in C Programming
 
Programming Fundamental Slide No.1
Programming Fundamental Slide No.1Programming Fundamental Slide No.1
Programming Fundamental Slide No.1
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 

Ähnlich wie 3 algorithm-and-flowchart

01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
ssuser586772
 
Fundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxFundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptx
Eyasu46
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
khair20
 
Ch1 principles of software development
Ch1 principles of software developmentCh1 principles of software development
Ch1 principles of software development
Hattori Sidek
 

Ähnlich wie 3 algorithm-and-flowchart (20)

ALGORITHM PPT GUIDE.pdf
ALGORITHM PPT GUIDE.pdfALGORITHM PPT GUIDE.pdf
ALGORITHM PPT GUIDE.pdf
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
 
lecture 5
 lecture 5 lecture 5
lecture 5
 
Introduction to computer science
Introduction to computer scienceIntroduction to computer science
Introduction to computer science
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
Lec 2 -algorithms-flowchart-and-pseudocode1.pptx
Lec 2 -algorithms-flowchart-and-pseudocode1.pptxLec 2 -algorithms-flowchart-and-pseudocode1.pptx
Lec 2 -algorithms-flowchart-and-pseudocode1.pptx
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Problem solving methodology
Problem solving methodologyProblem solving methodology
Problem solving methodology
 
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.pptBCE L-2 Algorithms-and-Flowchart-ppt.ppt
BCE L-2 Algorithms-and-Flowchart-ppt.ppt
 
Fundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptxFundamentals of Programming Lecture #1.pptx
Fundamentals of Programming Lecture #1.pptx
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
PCCF UNIT 1.pptx
PCCF UNIT 1.pptxPCCF UNIT 1.pptx
PCCF UNIT 1.pptx
 
Fundamentals of programming with C++
Fundamentals of programming with C++Fundamentals of programming with C++
Fundamentals of programming with C++
 
ICP - Lecture 6
ICP - Lecture 6ICP - Lecture 6
ICP - Lecture 6
 
Algorithm.pdf
Algorithm.pdfAlgorithm.pdf
Algorithm.pdf
 
Basic Slides on Algorithms and Flowcharts
Basic Slides on Algorithms and FlowchartsBasic Slides on Algorithms and Flowcharts
Basic Slides on Algorithms and Flowcharts
 
Lecture1-Algorithms-and-Flowcharts-ppt.ppt
Lecture1-Algorithms-and-Flowcharts-ppt.pptLecture1-Algorithms-and-Flowcharts-ppt.ppt
Lecture1-Algorithms-and-Flowcharts-ppt.ppt
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Ch1 principles of software development
Ch1 principles of software developmentCh1 principles of software development
Ch1 principles of software development
 

Mehr von Rohit Shrivastava (13)

1 introduction-to-computer
1 introduction-to-computer1 introduction-to-computer
1 introduction-to-computer
 
17 structure-and-union
17 structure-and-union17 structure-and-union
17 structure-and-union
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
14 strings
14 strings14 strings
14 strings
 
11 functions
11 functions11 functions
11 functions
 
10 array
10 array10 array
10 array
 
8 number-system
8 number-system8 number-system
8 number-system
 
7 decision-control
7 decision-control7 decision-control
7 decision-control
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
5 introduction-to-c
5 introduction-to-c5 introduction-to-c
5 introduction-to-c
 
2 memory-and-io-devices
2 memory-and-io-devices2 memory-and-io-devices
2 memory-and-io-devices
 
4 evolution-of-programming-languages
4 evolution-of-programming-languages4 evolution-of-programming-languages
4 evolution-of-programming-languages
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 

Kürzlich hochgeladen

Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
nirzagarg
 
9352852248 Call Girls Gota Escort Service Available 24×7 In Gota
9352852248 Call Girls  Gota Escort Service Available 24×7 In Gota9352852248 Call Girls  Gota Escort Service Available 24×7 In Gota
9352852248 Call Girls Gota Escort Service Available 24×7 In Gota
gargpaaro
 
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
opyff
 
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
ezgenuh
 
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
gajnagarg
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
wsppdmt
 
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
62qaf0hi
 

Kürzlich hochgeladen (20)

Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Thrissur [ 7014168258 ] Call Me For Genuine Models ...
 
9352852248 Call Girls Gota Escort Service Available 24×7 In Gota
9352852248 Call Girls  Gota Escort Service Available 24×7 In Gota9352852248 Call Girls  Gota Escort Service Available 24×7 In Gota
9352852248 Call Girls Gota Escort Service Available 24×7 In Gota
 
Vip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime Begusarai
Vip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime BegusaraiVip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime Begusarai
Vip Begusarai Escorts Service Girl ^ 9332606886, WhatsApp Anytime Begusarai
 
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
What Does The Engine Malfunction Reduced Power Message Mean For Your BMW X5
 
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
如何办理多伦多大学毕业证(UofT毕业证书)成绩单原版一比一
 
Bhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime Bhilai
Bhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime BhilaiBhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime Bhilai
Bhilai Escorts Service Girl ^ 8250092165, WhatsApp Anytime Bhilai
 
Nangloi Jat Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nangloi Jat
Nangloi Jat Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nangloi JatNangloi Jat Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nangloi Jat
Nangloi Jat Escorts Service Girl ^ 9332606886, WhatsApp Anytime Nangloi Jat
 
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
一比一原版(PU学位证书)普渡大学毕业证学历认证加急办理
 
Effortless Driving Experience Premier Mercedes Sprinter Suspension Service
Effortless Driving Experience Premier Mercedes Sprinter Suspension ServiceEffortless Driving Experience Premier Mercedes Sprinter Suspension Service
Effortless Driving Experience Premier Mercedes Sprinter Suspension Service
 
Is Your BMW PDC Malfunctioning Discover How to Easily Reset It
Is Your BMW PDC Malfunctioning Discover How to Easily Reset ItIs Your BMW PDC Malfunctioning Discover How to Easily Reset It
Is Your BMW PDC Malfunctioning Discover How to Easily Reset It
 
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be WrongIs Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
Is Your Mercedes Benz Trunk Refusing To Close Here's What Might Be Wrong
 
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's WhyIs Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
Is Your Volvo XC90 Displaying Anti-Skid Service Required Alert Here's Why
 
01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc
01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc
01552_14_01306_8.0_EPS_CMP_SW_VC2_Notebook.doc
 
Changodar Call Girls Book Now 7737669865 Top Class Escort Service Available
Changodar Call Girls Book Now 7737669865 Top Class Escort Service AvailableChangodar Call Girls Book Now 7737669865 Top Class Escort Service Available
Changodar Call Girls Book Now 7737669865 Top Class Escort Service Available
 
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In dewas [ 7014168258 ] Call Me For Genuine Models We ...
 
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
一比一原版西安大略大学毕业证(UWO毕业证)成绩单原件一模一样
 
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证一比一原版伯明翰城市大学毕业证成绩单留信学历认证
一比一原版伯明翰城市大学毕业证成绩单留信学历认证
 
Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...
Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...
Premium Call Girls Nagpur Call Girls (Adult Only) 💯Call Us 🔝 6378878445 🔝 💃 E...
 
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
Housewife Call Girl in Faridabad ₹7.5k Pick Up & Drop With Cash Payment #8168...
 
❤️Panchkula Enjoy 24/7 Escort Service sdf
❤️Panchkula Enjoy 24/7 Escort Service sdf❤️Panchkula Enjoy 24/7 Escort Service sdf
❤️Panchkula Enjoy 24/7 Escort Service sdf
 

3 algorithm-and-flowchart

  • 2. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software ? NO, Program is small part of software. Software merely comprises of Group of Programs (Source code), Code: refers to statements that are written in any programming language as machine code or C code. • Code is some time interchangeably used with program.
  • 3. Why Algorithm is needed? 3 • Programming is both tedious and exciting. Tedious because like spoken languages programming languages also have so many demanding rules. Exciting because writing program provides the programmer with the chance to create something new also gives challenges of solving a problem. It is very difficult to write direct programs in any language, just like you can not start constructing building without the design of building. For constructing a building you need design of building, similarly for writing a large or good program you need algorithm.
  • 4. Program design 4 Program Design Process has 2 phases: Problem Solving Phase Creates an algorithm that solves the problem Implementation (Coding) Phase Translates the algorithm into a programming language Algorithm Problem Program
  • 5. Algorithms 5 An algorithm is a finite set of steps defining the solution of a particular problem. Need not to belong one particular language Sequence of English statements can also be algorithm It is not a computer program An algorithm can be expressed in English like language,called pseudocode, in a programming language or in the form of flowchart.
  • 6. Algorithm Vs Program 6 What is the difference between an algorithm and a program?  a program is an implementation of an algorithm to be run on a specific computer and operating system.  an algorithm is more abstract – it does not deal with machine specific details – think of it as a method to solve a problem. • What is good algorithm? Efficient algorithms are good, we generally measure efficiency of an algorithm on the basis of: 1. Time: algorithm should take minimum time to execute. 2. Space: algorithm should use less memory.
  • 7. Algorithm Specification 7 Every algorithm must satisfy the following criteria: Input. Zero or more quantities are externally supplied. Output. At least one quantity is produced. Definiteness. Each instruction must be clear and unambiguous(Unique meaning). Finiteness. An algorithm terminates in a finite number of steps. Effectiveness. Every instruction must be basic enough to be carried out than, means not so complex.
  • 8. Pseudo code 8 Pseudocode is one of the methods that could be used to represent an algorithm.  It is not written in a specific syntax that is used by a programming language and therefore cannot be executed in a computer. There are lots of formats used for writing pseudocodes and most of them borrow some of the structures from popular programming languages such as C, Lisp, FORTRAN, etc.  Also, natural language is used in Pseudocode when presenting details that are not important. Most of the algorithms are presented using pseudocode since they can be read and understood using programmers who are familiar with different programming languages. Some programming constructs used for Pseudo Code – READ, PRINT, SET, INITIALISE, INCREMENT, IF – THEN – ENDIF, IF – THEN – ELSE – ENDIF,REPEAT – UNTIL, DO – WHILE etc.
  • 9. What is the difference between Algorithm and Pseudocode? 9 An algorithm is a well defined sequence of steps that provides a solution for a given problem, while a pseudocode is one of the methods that can be used to represent an algorithm.  While algorithms can be written in natural language, pseudocode is written in a format that is closely related to high level programming language structures.  But pseudocode does not use specific programming language syntax and therefore could be understood by programmers who are familiar with different programming languages. Additionally, transforming an algorithm presented in pseudocode to programming code could be much easier than converting an algorithm written in natural language.
  • 11. 11 Finding the largest integer among five integers
  • 12. 12 Defining actions in Find Largest algorithm
  • 14. 14 Example 1Example 1 Write an algorithm that finds the average of two numbers Solution: AverageOfTwo Input: Two numbers 1. Add the two numbers 2. Divide the result by 2 3. Return the result by step 2 2 End
  • 15. 15 Example 2Example 2 Write an algorithm to change a numeric grade to a pass/fail grade. Solution: Pass/FailGrade Input: One number 1. if (the number is greater than or equal to 40) then 1.1 Set the grade to “pass” else 1.2 Set the grade to “fail” End if 2. Return the grade End
  • 16. 16 Example 3Example 3 Write an algorithm for grading System of JUET. Marks range Grade >=80 A >=70 & <80 B >=60 & <70 C >=50 & <60 D <50 F
  • 17. 17 AlgoForGrade Input: One number 1. if (the number is between 80 and 100, inclusive) then 1.1 Set the grade to “A” End if 2. if (the number is between 70 and 79, inclusive) then 2.1 Set the grade to “B” End if Algorithm for GradingAlgorithm for Grading Continues on the next slide SolutionSolution
  • 18. Dr. Vishnu Sharma18 3. if (the number is between 60 and 69, inclusive) then 3.1 Set the grade to “C” End if 4. if (the number is between 50 and 59, inclusive) then 4.1 Set the grade to “D” End if 5. If (the number is less than 50) then 5.1 Set the grade to “F” End if 6. Return the grade End
  • 19. 19 Example 5Example 5 Write an algorithm to find the largest of 1000 numbers. FindLargest Input: 1000 positive integers 1. Set Largest to 0 2. Set Counter to 0 3. while (Counter less than 1000) 3.1 if (the integer is greater than Largest) then 3.1.1 Set Largest to the value of the integer End if 3.2 Increment Counter End while 4. Return Largest End SolutionSolution
  • 20. Flowchart 20 A graphical representation of an algorithm, often used in the design phase of programming to work out the logical flow of a program. Visual way to represent the information flow Make our logic more clear Help during writing of program Make testing and debugging easy
  • 21. 21
  • 22. Flowchart or program constructs  Sequence: The order of execution, this typically refers to the order in which the code will execute. Normally code executes line by line, so line 1 then 2 then 3 and so on.   Selection: Selection, like branching, is a method of controlling the execution sequence, you can create large control blocks, using if statements testing a condition, or switch statements evaluating a variable etc to control and change the execution of the program depending on this environment and changing variables.   Iteration (Repetition): Iteration is typically used to refer to collections and arrays of variables and data. Repeating set of instruction. Counting from 1 to 10, you are iterating over the first 10 numbers. for, while, do-while loops will be implemented for iteration. 22
  • 26. Example-1 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Algorithm Step 1: Take Width and Length as input Step 2: Calculate Area by Width* Length Step 3: Print Area.
  • 27. Example-1 Pseudocode Step 1: Input W,L Step 2: A ← L x W Step 3: Print A START Input W, L A ←L x W STOP Print A
  • 28. Example-2 Write an Pseudocode and draw a flowchart that will take marks of four subjects and calculate the average.Then if average marks are greater than 50 then print PASS otherwise print FAIL. 28
  • 29. Example-2 Step 1: Input M1,M2,M3,M4 Step 2: AVG ← (M1+M2+M3+M4)/4 Step 3: if (AVG <50) then Print “FAIL” else Print “PASS” endif START Input M1,M2,M3,M4 AVG←(M1+M2+M3+M4)/4 IS AVG<50 STOP YN Print “PASS” Print “FAIL”
  • 30. Example 3 Write an algorithm and draw a flowchart to convert the length in feet to centimeter. Algorithm:  Input the length in feet (Lft) Calculate the length in cm (Lcm) by multiplying LFT with 30 Print length in cm (LCM)
  • 31. Example 3 Pseudocode Step 1: Input Lft Step 2: Lcm ← Lft x 30 Step 3: Print Lcm START Input Lft Lcm ←Lft x 30 Print Lcm STOP Flowchart
  • 32. Example 4 Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation  Hint: d = sqrt ( ), and the roots are: x1 = (–b + d)/2a and x2 = (–b – d)/2a 2 0ax bx c+ + = 2 4b ac−
  • 33. Example 4 Algorithm: Input the coefficients (a, b, c) of the quadratic equation Calculate d Calculate x1 Calculate x2 Print x1 and x2
  • 34. Example 4 Pseudocode:  Step 1: Input a, b, c  Step 2: d ← sqrt ( )  Step 3: x1 ← (–b + d) / (2 x a)  Step 4: x2 ← (–b – d) / (2 x a)  Step 5: Print x1, x2 START Input a, b, c d ← sqrt(b x b – 4 x a x c) Print x1 ,x2 STOP x1 ←(–b + d) / (2 x a) X2 ←(–b – d) / (2 x a) 4b b a c× − × ×
  • 35. Example 5 Write an algorithm that reads three numbers and prints the value of the largest number.
  • 36. Example 5 Step 1: Input N1, N2, N3 Step 2: if (N1>N2) then if (N1>N3) then MAX ← N1 //[N1>N2, N1>N3] else MAX ← N3 //[N3>N1>N2] endif else if (N2>N3) then MAX ← N2 //[N2>N1, N2>N3] else MAX ← N3 //[N3>N2>N1] endif endif Step 3: Print “The largest number is”, MAX
  • 37. Flow Charts Limitation  For very large program, flow chart goes for many pages  Costly to draw flow charts for large program  Difficult to modify 37