Top Banner
35
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Assignment of pseudo code
Page 2: Assignment of pseudo code

PMAS – Arid Agriculture University Rawalpindi

Programming Fundamentals using C++ (CS-323)Lecture # 6

Monday, October 23, 2013

Page 3: Assignment of pseudo code

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

Page 4: Assignment of pseudo code

Thursday, April 13, 2023 Programming Fundamentals using C++(CS-323) 4

Quick recap

• Algorithm Development• Stepwise refinement of Algorithm

Page 5: Assignment of pseudo code

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

Page 6: Assignment of pseudo code

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

Page 7: Assignment of pseudo code

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

Page 8: Assignment of pseudo code

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

Page 9: Assignment of pseudo code

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

Page 10: Assignment of pseudo code

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

Page 11: Assignment of pseudo code

Figure 2.3Algorithm for Computing Average Miles per Gallon

11Programming Fundamentals using C++(CS-323)

Page 12: Assignment of pseudo code

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

Page 13: Assignment of pseudo code

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

Page 14: Assignment of pseudo code

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

Page 15: Assignment of pseudo code

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

Page 16: Assignment of pseudo code

Figure 2.4Second Version of the Average Miles per Gallon Algorithm

16Programming Fundamentals using C++(CS-323)

Page 17: Assignment of pseudo code

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

Page 18: Assignment of pseudo code

Figure 2.5Third Version of the Average Miles per Gallon AlgorithmProgramming Fundamentals using C++(CS-323) 18

Page 19: Assignment of pseudo code

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

Page 20: Assignment of pseudo code

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

Page 21: Assignment of pseudo code

Figure 2.6Summary of Pseudocode

Language Instructions

21Programming Fundamentals using C++(CS-323)

Page 22: Assignment of pseudo code

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

Page 23: Assignment of pseudo code

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

Page 24: Assignment of pseudo code

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

Page 25: Assignment of pseudo code

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)

Page 26: Assignment of pseudo code

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

Page 27: Assignment of pseudo code

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

Page 28: Assignment of pseudo code

Figure 2.10Algorithm to Find the Largest Value in a List

28Programming Fundamentals using C++(CS-323)

Page 29: Assignment of pseudo code

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

Page 30: Assignment of pseudo code

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

Page 31: Assignment of pseudo code

Figure 2.12Final Draft of the Pattern-Matching Algorithm

Programming Fundamentals using C++(CS-323) 31

Page 32: Assignment of pseudo code

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

Page 34: Assignment of pseudo code
Page 35: Assignment of pseudo code