E N D
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.
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.
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.
Pseudocode Pseudocode A way of expressing algorithms using English phrases and indention to make the steps in the solution explicit
More About Pseudocode We need to express more complicated algorithms Like Washing our Hair
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
Pseudocode With Pseudocode we can express… Individual steps (Sequential Operations) As Well as Structure… Repetition (Iterations) Selection (Control Operations)
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
IN-CLASS LAB Your turn: Find the average of 4 numbers and display it.
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
Counting upRead number n and print the integerscounting up to n. In-Class Lab
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.
Summing consecutive integersRead number n and print the sum of theintegers up to n,1 + 2 + ……+n In-Class Lab
Summing consecutive integersRead number n and print the sum of theintegers up to n,1 + 2 + ……+n In-Class Lab
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.
Power of twoRead number n and print 2n. In-Class Lab
Power of twoRead number n and print 2n. Read n . Initialize power to 1. Repeat n times: Double power. End repeat Write power Stop.
MultiplicationRead number m and n and print m x n In-Class Lab
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
Summing upSumming consecutive integers from 1 to 100 In-Class Lab
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