1 / 21

Pseudocode Examples

Pseudocode Examples.

ron
Download Presentation

Pseudocode Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pseudocode Examples An algorithm is a procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed. An algorithm is merely the sequence of steps taken to solve a problem. The steps are normally "sequence," "selection, " "iteration," and a case-type statement.

  2. Pseudocode Examples For example, In C, "sequence statements" are imperatives. The "selection" is the "if then else" statement, and the iteration is satisfied by a number of statements, such as the "while," " do," and the "for," while the case-type statement is satisfied by the "switch" statement.

  3. Pseudocode Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool. The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch.

  4. Pseudocode Pseudocode A way of expressing algorithms using English phrases and indention to make the steps in the solution explicit

  5. More About Pseudocode We need to express more complicated algorithms Like Washing our Hair

  6. A Algorithm for Hair Step 0: Wet Hair Step 1: Apply shampoo Step 2: Lather Step 3: Rinse Step 4: If (Hair not clean enough) goto Step 1 Step 5: Done

  7. Pseudocode With Pseudocode we can express… Individual steps (Sequential Operations) As Well as Structure… Repetition (Iterations) Selection (Control Operations)

  8. Pseudocode for Adding 3 numbers Step 1) load the sum (0) to the accumulator Step 2) Read first number from the keyboard Step 3) Add first number to the accumulator Step 4) Read second number from the keyboard Step 5) Add second number to the accumulator Step 6) Read third number from the keyboard Step 7) Add third number to the accumulator Step 8) Store accumulator back to the sum Step 9) Display the sum Step 10) Stop

  9. IN-CLASS LAB Your turn: Find the average of 4 numbers and display it.

  10. Your turn:Find the average of 4 numbers and display it. Answer : There are many ways to answer this correctly as long as it is clear you got it right. Ask your friend to check it. input 4 numbers sum = add numbers together avg = sum / 4 print avg

  11. Counting upRead number n and print the integerscounting up to n. In-Class Lab

  12. Counting upRead number n and print the integerscounting up to n. Read n. Initialize i to 1. while i<=n , do: Write i. (Print/Display i) increment i. (i=i+1) end while Stop.

  13. Summing consecutive integersRead number n and print the sum of theintegers up to n,1 + 2 + ……+n In-Class Lab

  14. Summing consecutive integersRead number n and print the sum of theintegers up to n,1 + 2 + ……+n In-Class Lab

  15. Summing consecutive integersRead number n and print the sum of theintegers up to n,1 + 2 + ……+n Read n. Initialize i to 1. Initialize sum to 0. while i<=n, do: Increase sum by i. (sum=sum+i) Increment i. end while Write sum. Stop.

  16. Power of twoRead number n and print 2n. In-Class Lab

  17. Power of twoRead number n and print 2n. Read n . Initialize power to 1. Repeat n times: Double power. End repeat Write power Stop.

  18. MultiplicationRead number m and n and print m x n In-Class Lab

  19. MultiplicationRead number m and n and print m x n Read m. Read n. Initialize multi to 0. Set multi to m * n Print multi. Stop

  20. Summing upSumming consecutive integers from 1 to 100 In-Class Lab

  21. Summing upSumming consecutive integers from 1 to 100 Set the value of sum to 0 Set the value of x to 1 While x is less than or equal to 100 Add x to sum (sum=sum+x) Add 1 to the value of x (x=x+1) Print the value of sum Stop

More Related