assignment of pseudo code
Post on 25-Jun-2015
902 Views
Preview:
TRANSCRIPT
PMAS – Arid Agriculture University Rawalpindi
Programming Fundamentals using C++ (CS-323)Lecture # 6
Monday, October 23, 2013
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323) 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, April 13, 2023 Programming Fundamentals using C++(CS-323) 4
Quick recap
• Algorithm Development• Stepwise refinement of Algorithm
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323) 5
Today’s Topics
• Representing algorithms
• Types of Algorithmic operations
• Examples of algorithmic problem solving
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
6
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
7
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
8
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
9
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
10
Figure 2.3Algorithm for Computing Average Miles per Gallon
11Programming Fundamentals using C++(CS-323)
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
12
Thursday, April 13, 2023
Conditional and Iterative Operations (continued)• Conditional operations
• Ask questions and choose alternative actions based on the answers• Example
• if x is greater than 25 thenprint x
elseprint x times 100
Programming Fundamentals using C++(CS-323) 13
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
14
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
Conditional and Iterative Operations (continued)
• Examples• while j > 0 do
set s to s + aj
set j to j - 1
• repeatprint ak
set k to k + 1until k > n
15
Figure 2.4Second Version of the Average Miles per Gallon Algorithm
16Programming Fundamentals using C++(CS-323)
Thursday, April 13, 2023
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
Programming Fundamentals using C++(CS-323) 17
Figure 2.5Third Version of the Average Miles per Gallon AlgorithmProgramming Fundamentals using C++(CS-323) 18
Thursday, April 13, 2023
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
Programming Fundamentals using C++(CS-323) 19
Thursday, April 13, 2023
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
Programming Fundamentals using C++(CS-323) 20
Figure 2.6Summary of Pseudocode
Language Instructions
21Programming Fundamentals using C++(CS-323)
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
22
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
23
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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)
24
Figure 2.9The Sequential Search Algorithm
25
Uses the variable Found to exit the iteration as soon as a match is found
Programming Fundamentals using C++(CS-323)
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
26
Thursday, April 13, 2023
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
Programming Fundamentals using C++(CS-323) 27
Figure 2.10Algorithm to Find the Largest Value in a List
28Programming Fundamentals using C++(CS-323)
Thursday, April 13, 2023
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
Programming Fundamentals using C++(CS-323) 29
Thursday, April 13, 2023
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
Programming Fundamentals using C++(CS-323) 30
Figure 2.12Final Draft of the Pattern-Matching Algorithm
Programming Fundamentals using C++(CS-323) 31
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323)
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
32
Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323) 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=frontcover&source=gbs_ge_summary_r&cad=0#v=onepage&q&f=true
• eBook : Invitation to Computer Science 5ed, Chapter # 2
top related