SlideShare a Scribd company logo
1 of 35
PMAS – Arid Agriculture
University Rawalpindi
Programming Fundamentals using C++ (CS-323)
Lecture # 6
Monday, October 23, 2013
Quote of the day

“You don't have to be great
to start, but you have to
start to be great.” by Zig
Ziglar
Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

3
Quick recap
• Algorithm Development
• Stepwise refinement of Algorithm

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

4
Today’s Topics
• Representing algorithms
• Types of Algorithmic operations
• Examples of algorithmic problem solving

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

5
Pseudocode
 English language constructs modeled to look like statements available
in most programming languages
 Example: ADD X + Y
 No fixed syntax for most operations is required
 Everyone knows what you are talking about

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

6
Pseudocode (continued)
• Less ambiguous and more readable than natural language
• Emphasis is on the process, not the specific notation
• Can be easily translated into a programming language

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

7
Operations
• Types of algorithmic operations
• Sequential – input / output, assignment, printing, etc.
• Conditional – doing one thing or another based on a certain condition

• Iterative – looping – doing something more than once

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

8
Sequential Operations (continued)
• Computation operations
• Example
• Set the value of “variable” to “arithmetic expression”
• X = 10 (set x equal to 10)

• Variable
• Named storage location that can hold a data value
• X in the above example
Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

9
Sequential Operations (continued)
• Input operations
• To receive data values from the outside world
• Get a value for r, the radius of the circle

• Output operations
• To send results to the outside world for display
• Print the value of Area

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

10
Figure 2.3
Algorithm for Computing Average Miles per Gallon
Programming Fundamentals using C++(CS-323)

11
Conditional and Iterative Operations
• Sequential algorithm
• Executes its instructions in a straight line from top to bottom and then stops

• Control operations – allow us to get more complex
• Conditional operations
• IF X=10 THEN … do some stuff

• Iterative operations
• WHILE X < 100 … do some other stuff

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

12
Conditional and Iterative Operations
(continued)
• Conditional operations
• Ask questions and choose alternative actions based on the answers
• Example

• if x is greater than 25 then
print x
else

print x times 100

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

13
Conditional and Iterative Operations
(continued)
• Iterative operations
• Perform “looping” behavior; repeating actions until a
continuation condition becomes false

• Loop
• The repetition of a block of instructions

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

14
Conditional and Iterative Operations
(continued)
• Examples
• while j > 0 do
set s to s + aj
set j to j - 1
• repeat
print ak
set k to k + 1
until k > n

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

15
Figure 2.4
Second Version of the Average Miles per Gallon Algorithm
Programming Fundamentals using C++(CS-323)

16
Conditional and Iterative Operations
(continued)
• Components of a loop
• Continuation condition – when do we stop?
• Loop body – what we want to repeat

• Infinite loop
• The continuation condition never becomes false and the
loop runs forever

• This is usually an error
Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

17
Figure 2.5
Third Version of the Average Miles per Gallon Algorithm
Programming Fundamentals using C++(CS-323)

18
Conditional and Iterative Operations
• Pretest loop
• Continuation condition tested at the beginning of each pass through the loop
• It is possible for the loop body to never be executed
• Ex: While loop

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

19
Conditional and Iterative Operations
(continued)
• Post-test loop
• Continuation condition tested at the end of loop body
• Loop body must be executed at least once
• Ex: Do - While loop

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

20
Figure 2.6
Summary of
Pseudocode
Language
Instructions

Programming Fundamentals using C++(CS-323)

21
Example 1: Looking, Looking, Looking
• Examples of algorithmic problem solving
• Sequential search: find a particular value in an unordered collection
• Find maximum: find the largest value in a collection of data
• Pattern matching: determine if and where a particular pattern occurs in a
piece of text

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

22
Example 1: Looking, Looking, Looking
(continued)
• Task
• Find a particular person’s name from an unordered list of
telephone subscribers

• Algorithm outline
• Start with the first entry and check its name, then repeat
the process for all entries

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

23
Example 1: Looking, Looking, Looking
(continued)
• Correct sequential search algorithm
• Uses iteration (loops) to simplify the task
• Handles special cases (like a name not found in the
collection)

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

24
Uses the variable Found to exit the iteration as soon as a match is found

Figure 2.9
The Sequential Search Algorithm
Programming Fundamentals using C++(CS-323)

25
Example 1: Looking, Looking, Looking
(continued)
• The selection of an algorithm to solve a problem is
greatly influenced by the way the data for that
problem are organized

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

26
Example 2: Big, Bigger, Biggest
• Task
• Find the largest value from a list of values

• Algorithm outline
• Keep track of the largest value seen so far (initialized to be the first in the list)

• Compare each value to the largest seen so far, and keep the larger as the new
largest

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

27
Figure 2.10
Algorithm to Find the Largest Value in a List
Programming Fundamentals using C++(CS-323)

28
Example 3: Meeting Your Match
• Task
• Find if and where a pattern string occurs within a longer
piece of text

• Algorithm outline
• Try each possible location of pattern string in turn
• At each location, compare pattern characters against string
characters

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

29
Example 3: Meeting Your Match
(continued)
• Pattern-matching algorithm
• Contains a loop within a loop
• External loop iterates through possible locations of
matches to pattern
• Internal loop iterates through corresponding characters
of pattern and string to evaluate match

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

30
Figure 2.12
Final Draft of the Pattern-Matching Algorithm
Programming Fundamentals using C++(CS-323)

31
Summary
• Algorithm design is a first step in developing an algorithm
• Must also:
• Ensure the algorithm is correct
• Ensure the algorithm is sufficiently efficient

• Pseudocode is used to design and represent algorithms
• Pseudocode is readable, unambiguous, and analyzable
• Algorithm design is a creative process; uses multiple drafts and top-down
design to develop the best solution
Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

32
Reading Material
• http://courses.cs.vt.edu/~cs1104/Algorithms/TOC.html
• http://faculty.otterbein.edu/PSanderson/csc150/notes/chapter2.html
• http://books.google.com.pk/books?id=BEhlUfdMNucC&printsec=fron
tcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=true
• eBook : Invitation to Computer Science 5ed, Chapter # 2

Thursday, October 24, 2013

Programming Fundamentals using C++(CS-323)

33
Assignment of pseudo code
Assignment of pseudo code

More Related Content

What's hot

Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
myrajendra
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 

What's hot (20)

Working principle of dda and bresenham line drawing explaination with example
Working principle of dda and bresenham line drawing explaination with exampleWorking principle of dda and bresenham line drawing explaination with example
Working principle of dda and bresenham line drawing explaination with example
 
Application of Kruskals algorithm
Application of Kruskals algorithmApplication of Kruskals algorithm
Application of Kruskals algorithm
 
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
Tkinter Python Tutorial | Python GUI Programming Using Tkinter Tutorial | Pyt...
 
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
Qnx os
Qnx os Qnx os
Qnx os
 
PRIM'S ALGORITHM
PRIM'S ALGORITHMPRIM'S ALGORITHM
PRIM'S ALGORITHM
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
 
5.3 dynamic programming
5.3 dynamic programming5.3 dynamic programming
5.3 dynamic programming
 
Gstreamer Basics
Gstreamer BasicsGstreamer Basics
Gstreamer Basics
 
品質管理グループ Linux 勉強会
品質管理グループ Linux 勉強会品質管理グループ Linux 勉強会
品質管理グループ Linux 勉強会
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
zkStudyClub - cqlin: Efficient linear operations on KZG commitments
zkStudyClub - cqlin: Efficient linear operations on KZG commitments zkStudyClub - cqlin: Efficient linear operations on KZG commitments
zkStudyClub - cqlin: Efficient linear operations on KZG commitments
 
Jquery ui
Jquery uiJquery ui
Jquery ui
 
Clique problem step_by_step
Clique problem step_by_stepClique problem step_by_step
Clique problem step_by_step
 
Pygame presentation
Pygame presentationPygame presentation
Pygame presentation
 

Viewers also liked

서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집
서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집
서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집
한국디자인진흥원 공공서비스디자인PD
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
hccit
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
Manoj Patil
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
hermiraguilar
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
Damian T. Gordon
 
Programming The Arm Microprocessor For Embedded Systems
Programming The Arm Microprocessor For Embedded SystemsProgramming The Arm Microprocessor For Embedded Systems
Programming The Arm Microprocessor For Embedded Systems
joshparrish13
 

Viewers also liked (10)

Sr. Jon Ander, Internet de las Cosas y Big Data: ¿hacia dónde va la Industria?
Sr. Jon Ander, Internet de las Cosas y Big Data: ¿hacia dónde va la Industria? Sr. Jon Ander, Internet de las Cosas y Big Data: ¿hacia dónde va la Industria?
Sr. Jon Ander, Internet de las Cosas y Big Data: ¿hacia dónde va la Industria?
 
서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집
서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집
서비스디자인, 경험 경제를 디자인하다 - K디자인 2014년 가을호 서비스디자인 특집
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
 
ARM lab programs
ARM  lab programs  ARM  lab programs
ARM lab programs
 
System Programing Unit 1
System Programing Unit 1System Programing Unit 1
System Programing Unit 1
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Introduction to Pseudocode
Introduction to PseudocodeIntroduction to Pseudocode
Introduction to Pseudocode
 
Arm7 Interfacing examples
Arm7   Interfacing examples Arm7   Interfacing examples
Arm7 Interfacing examples
 
Programming The Arm Microprocessor For Embedded Systems
Programming The Arm Microprocessor For Embedded SystemsProgramming The Arm Microprocessor For Embedded Systems
Programming The Arm Microprocessor For Embedded Systems
 
Assembly Language Basics
Assembly Language BasicsAssembly Language Basics
Assembly Language Basics
 

Similar to Assignment of pseudo code

CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
Arumugam90
 

Similar to Assignment of pseudo code (20)

1_Introduction.pptx
1_Introduction.pptx1_Introduction.pptx
1_Introduction.pptx
 
C++ Notes PPT.ppt
C++ Notes PPT.pptC++ Notes PPT.ppt
C++ Notes PPT.ppt
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
Using Git, Pointers in Rust
Using Git, Pointers in RustUsing Git, Pointers in Rust
Using Git, Pointers in Rust
 
Intro_2.ppt
Intro_2.pptIntro_2.ppt
Intro_2.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
Scientific Software Development
Scientific Software DevelopmentScientific Software Development
Scientific Software Development
 
Mathemetics module
Mathemetics moduleMathemetics module
Mathemetics module
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
Performance Tuning and Optimization
Performance Tuning and OptimizationPerformance Tuning and Optimization
Performance Tuning and Optimization
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
A Workshop on R
A Workshop on RA Workshop on R
A Workshop on R
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Paris Data Geeks
Paris Data GeeksParis Data Geeks
Paris Data Geeks
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 

Assignment of pseudo code

  • 1.
  • 2. PMAS – Arid Agriculture University Rawalpindi Programming Fundamentals using C++ (CS-323) Lecture # 6 Monday, October 23, 2013
  • 3. Quote of the day “You don't have to be great to start, but you have to start to be great.” by Zig Ziglar Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 3
  • 4. Quick recap • Algorithm Development • Stepwise refinement of Algorithm Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 4
  • 5. Today’s Topics • Representing algorithms • Types of Algorithmic operations • Examples of algorithmic problem solving Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 5
  • 6. Pseudocode  English language constructs modeled to look like statements available in most programming languages  Example: ADD X + Y  No fixed syntax for most operations is required  Everyone knows what you are talking about Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 6
  • 7. Pseudocode (continued) • Less ambiguous and more readable than natural language • Emphasis is on the process, not the specific notation • Can be easily translated into a programming language Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 7
  • 8. Operations • Types of algorithmic operations • Sequential – input / output, assignment, printing, etc. • Conditional – doing one thing or another based on a certain condition • Iterative – looping – doing something more than once Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 8
  • 9. Sequential Operations (continued) • Computation operations • Example • Set the value of “variable” to “arithmetic expression” • X = 10 (set x equal to 10) • Variable • Named storage location that can hold a data value • X in the above example Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 9
  • 10. Sequential Operations (continued) • Input operations • To receive data values from the outside world • Get a value for r, the radius of the circle • Output operations • To send results to the outside world for display • Print the value of Area Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 10
  • 11. Figure 2.3 Algorithm for Computing Average Miles per Gallon Programming Fundamentals using C++(CS-323) 11
  • 12. Conditional and Iterative Operations • Sequential algorithm • Executes its instructions in a straight line from top to bottom and then stops • Control operations – allow us to get more complex • Conditional operations • IF X=10 THEN … do some stuff • Iterative operations • WHILE X < 100 … do some other stuff Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 12
  • 13. Conditional and Iterative Operations (continued) • Conditional operations • Ask questions and choose alternative actions based on the answers • Example • if x is greater than 25 then print x else print x times 100 Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 13
  • 14. Conditional and Iterative Operations (continued) • Iterative operations • Perform “looping” behavior; repeating actions until a continuation condition becomes false • Loop • The repetition of a block of instructions Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 14
  • 15. Conditional and Iterative Operations (continued) • Examples • while j > 0 do set s to s + aj set j to j - 1 • repeat print ak set k to k + 1 until k > n Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 15
  • 16. Figure 2.4 Second Version of the Average Miles per Gallon Algorithm Programming Fundamentals using C++(CS-323) 16
  • 17. Conditional and Iterative Operations (continued) • Components of a loop • Continuation condition – when do we stop? • Loop body – what we want to repeat • Infinite loop • The continuation condition never becomes false and the loop runs forever • This is usually an error Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 17
  • 18. Figure 2.5 Third Version of the Average Miles per Gallon Algorithm Programming Fundamentals using C++(CS-323) 18
  • 19. Conditional and Iterative Operations • Pretest loop • Continuation condition tested at the beginning of each pass through the loop • It is possible for the loop body to never be executed • Ex: While loop Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 19
  • 20. Conditional and Iterative Operations (continued) • Post-test loop • Continuation condition tested at the end of loop body • Loop body must be executed at least once • Ex: Do - While loop Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 20
  • 22. Example 1: Looking, Looking, Looking • Examples of algorithmic problem solving • Sequential search: find a particular value in an unordered collection • Find maximum: find the largest value in a collection of data • Pattern matching: determine if and where a particular pattern occurs in a piece of text Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 22
  • 23. Example 1: Looking, Looking, Looking (continued) • Task • Find a particular person’s name from an unordered list of telephone subscribers • Algorithm outline • Start with the first entry and check its name, then repeat the process for all entries Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 23
  • 24. Example 1: Looking, Looking, Looking (continued) • Correct sequential search algorithm • Uses iteration (loops) to simplify the task • Handles special cases (like a name not found in the collection) Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 24
  • 25. Uses the variable Found to exit the iteration as soon as a match is found Figure 2.9 The Sequential Search Algorithm Programming Fundamentals using C++(CS-323) 25
  • 26. Example 1: Looking, Looking, Looking (continued) • The selection of an algorithm to solve a problem is greatly influenced by the way the data for that problem are organized Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 26
  • 27. Example 2: Big, Bigger, Biggest • Task • Find the largest value from a list of values • Algorithm outline • Keep track of the largest value seen so far (initialized to be the first in the list) • Compare each value to the largest seen so far, and keep the larger as the new largest Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 27
  • 28. Figure 2.10 Algorithm to Find the Largest Value in a List Programming Fundamentals using C++(CS-323) 28
  • 29. Example 3: Meeting Your Match • Task • Find if and where a pattern string occurs within a longer piece of text • Algorithm outline • Try each possible location of pattern string in turn • At each location, compare pattern characters against string characters Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 29
  • 30. Example 3: Meeting Your Match (continued) • Pattern-matching algorithm • Contains a loop within a loop • External loop iterates through possible locations of matches to pattern • Internal loop iterates through corresponding characters of pattern and string to evaluate match Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 30
  • 31. Figure 2.12 Final Draft of the Pattern-Matching Algorithm Programming Fundamentals using C++(CS-323) 31
  • 32. Summary • Algorithm design is a first step in developing an algorithm • Must also: • Ensure the algorithm is correct • Ensure the algorithm is sufficiently efficient • Pseudocode is used to design and represent algorithms • Pseudocode is readable, unambiguous, and analyzable • Algorithm design is a creative process; uses multiple drafts and top-down design to develop the best solution Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 32
  • 33. Reading Material • http://courses.cs.vt.edu/~cs1104/Algorithms/TOC.html • http://faculty.otterbein.edu/PSanderson/csc150/notes/chapter2.html • http://books.google.com.pk/books?id=BEhlUfdMNucC&printsec=fron tcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=true • eBook : Invitation to Computer Science 5ed, Chapter # 2 Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 33