Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Assignment of pseudo code

1,867 views

Published on

  • Be the first to comment

Assignment of pseudo code

  1. 1. PMAS – Arid Agriculture University Rawalpindi Programming Fundamentals using C++ (CS-323) Lecture # 6 Monday, October 23, 2013
  2. 2. 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
  3. 3. Quick recap • Algorithm Development • Stepwise refinement of Algorithm Thursday, October 24, 2013 Programming Fundamentals using C++(CS-323) 4
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 10. Figure 2.3 Algorithm for Computing Average Miles per Gallon Programming Fundamentals using C++(CS-323) 11
  11. 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
  12. 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
  13. 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
  14. 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
  15. 15. Figure 2.4 Second Version of the Average Miles per Gallon Algorithm Programming Fundamentals using C++(CS-323) 16
  16. 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
  17. 17. Figure 2.5 Third Version of the Average Miles per Gallon Algorithm Programming Fundamentals using C++(CS-323) 18
  18. 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
  19. 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
  20. 20. Figure 2.6 Summary of Pseudocode Language Instructions Programming Fundamentals using C++(CS-323) 21
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 27. Figure 2.10 Algorithm to Find the Largest Value in a List Programming Fundamentals using C++(CS-323) 28
  28. 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
  29. 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
  30. 30. Figure 2.12 Final Draft of the Pattern-Matching Algorithm Programming Fundamentals using C++(CS-323) 31
  31. 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
  32. 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

×