Chapter 2
Chapter 2
Chapter 2
Pseudocode
Outline
By the end of this chapter, you will be able to:
Define algorithm Differentiate between pseudocode and flowchart Solve a given problem by applying pseudocode technique Perform desk-checking
Algorithm
An algorithm is a step-by-step procedure to solve a given problem procedure consisting of a finite set of unambiguous rules (instructions) which specify a finite sequence of operations that provides the solution to a problem, or to a specific class of problems for any allowable set of input quantities (if there are inputs). The steps are normally "sequence, "selection, "iteration" and a case-type statement. Uses human native language.
3
..Algorithm
Can be implemented by using
Pseudocode
OR
Flowchart
Problem Solving
Understand and analyze the problem 3 separate components:
Input: List of things that is required by the problem Output: Outcome desired Process: Actions that need to be done
Meaningful names
To represent variables in our pseudocode number1 vs n1 Underscore (_) is used if there are more than one word in the variable. Eg. total_sales Another option: use capital letters to separate words. Eg. totalSales Majority of programming languages does not allow gap (space) in
Example
To add two numbers together
Input number_ 1 number_ 2 Process Read two numbers Add the numbers together Display the total Outpu t total
Pseudocode
After identifying Input, Output and Process, we can write pseudocode. Pseudocode: See translation to a specific programming language as it includes some basic computer operations
Keywords - input
Prompt and Get: use to receive input from the keyboard.
Keywords - output
Display, Output or Put: used we want to output written to the computer screen. Print: enables output is to be sent to a printer.
10
Arithmetic Symbols
Symbols that you can use to write pseudocodes:
Symbol + * / () Purpose Addition Subtraction Multiplication Division Parentheses
11
Pseudocode
Pseudocode = Similar to how we write essays. Beginning, body content and ending of an essay. Begin with Start End with End Body content are you processes show sequence of task in the program
12
Example
Problem: Adding two numbers together 1. Start 2. Declare variables, number_1, number_2, total 3. Get number_1, number_2 4. total = number_1 + number_2 5. Display total 6. End.
13
Pseudocodes
Pseudocode corresponds closely with the input, processes and output identified earlier
14
Desk checking
Desk checking: process of tracing through the pseudocode with some test data Mimic what the computer would do based on the pseudocode written Help eliminate errors but will not be able to prove that your algorithm is correct
15
Desk checking
Should have at least 2 sets of input data for testing. Write the desired outcome from each set
number_1 number_2 total First set Second set 5 7 18 33 23 40
16
Desk checking
Create a table with the variables that are involved in the program and pass each test data set through the pseudocode. Statement number_1 number_2 total
number First pass 1, 2 3 4 Second pass 1, 2 3 4 5 18 23 print 7 33 40 print
17
Desk checking
Complete the desk check for calculating land price: 1.Start 2.Declare variables, width, length, size, price 3.Prompt user for width, length 4.Get width, length 5.size = width * length 6.price = size * 5.40 7.Display price 8.End
18
Case Study
19
Summary
Algorithm = a finite step by step of instructions to solve a problem. Algorithm can be implemented by using pseudocode or flowchart. Desk-checking = a process of tracing through the algorithm with several sample data.
20