Topic 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Desk Checking

Scope and Coverage

 This topic will cover:


1. A formal Pseudo-code syntax
2. Desk-checking of algorithms
What Makes a Good Algorithm?

 A good algorithm has the following qualities


 It is complete
 It is robust
 It is efficient
 It is readable
 It is maintainable
 It is documented
Pseudocode

 Pseudocode is an English-like way to state an algorithm.


 There is no ‘correct’ form of pseudocode.
 There are many different styles, and none are the ‘right’ way to do it.
 In this module, we’re going to make use of a specific form of pseudocode.
 This makes sure everyone is writing the same kinds of things in the same
level of detail.
Syntax

 The words and symbols that make up a programming language are known as its
syntax.
 C#, Java, C , Visual Basic and so on all have their own particular syntax.
 In exactly the same way, the pseudocode we develop during this module will have
its own syntax.
 It’s just that we won’t be able to compile it into real computer code.
Why Pseudocode?

 There are several reasons why we use pseudocode in programming.


 It means we are not tied to a specific programming language.
 It means we can focus on the logic of an algorithm rather than the
language specific features.
 It means we can formally check the correctness on paper.
Pseudocode Syntax

 In this module, we will use the following syntax:


 When we need to hold some data , we use the word data to set up a container for it:
 data <name> as <type>
 E.g. data myAge as Whole Number
 Acceptable types for now are
 Whole Number ( 0,1,2,3,…… )
 Real Number (…-2,-1,0,1,2,…..)
 String (“Hello World”)
Pseudocode Syntax

 When we need to display anything to the user, we use output. Enclose what’s
seen by the user in quotation marks.
 Output “Hello there!”
 If we need to output the contents of some data, we also use output but omit the
quotation marks:
 Output myAge
 We can put information in a data container like so:
 myAge = 20
Pseudocode Syntax

 When we wish to perform arithmetic on numerical data, we need a container for


the answer and then we use the arithmetic symbols:
 data doubleAge as whole number
 doubleAge = myAge * 2
Pseudocode Syntax

 Arithmetic Operators
Naming Convention (Camel case)

 It is the practice of writing compound words or phrases such that each word or
abbreviation in the middle of the phrase begins with a capital letter , with no spaces or
hyphens.
 Camel case may start with a capital letter or with a lowercase letter.
 Examples
 iPhone
 FedEx
 HarperCollins
Pseudocode Example

1. data myAge as whole number


2. data myNewAge as whole number

3. myAge=10
4. myNewAge = myAge + 1

5. output "In a year you will be"


6. output myNewAge
Pseudocode Example
 Taking Input from User

1. data myAge as whole number


2. data myNewAge as whole number

3. output "Please enter your age"


4. input myAge

5. myNewAge = myAge + 1

6. output "In a year you will be"


7. output myNewAge
Multiply two number

1. data firstNumber as whole number


2. data secondNumber as whole number
3. data answer as whole number

4. output "Please enter a number"


5. input firstNumber
6. output "Please enter a second number"
7. input secondNumber

8. answer = firstNumber * secondNumber;


9. Output "The answer is "
10.Output answer
Class Work

 Write a pseudocode for calculating Simple Interest.


 Write a pseudocode for calculating area of rectangle
 Area = length * breadth
 Write a pseudocode for calculating area of Circle
 Area = π * radius * radius
Pseudocode Checking

 An important aspect of having pseudocode is that it is correct.


 It does what we expect it to do, and it does it correctly.
 In later lectures, we will talk about ways in which you can test your algorithms
properly.
 So you are reasonably sure they work for all sets of data.
 For now, we are going to introduce an informal mechanism for working through
an algorithm.
 It is called a desk-check.
Desk-Check

 Because pseudocode is not a real programming language, it means we can run


it by hand.
 We can’t do that as easily with real programming code because much of
what a language does is hidden from us.

 A desk-check is a formal way of stepping through every line in a pseudocode


representation of an algorithm, highlighting what happens at each line.
What is in a Desk-Check?

 For a desk-check, you must define:


 Any starting values.
 What input is provided by a user
 The contents of every data container you created
 The line of code you are currently checking

 It is best to do this in a table form.


Pseudocode Example
1. data myAge as whole number
2. data myNewAge as whole number

3. output "Please enter your age"


4. input myAge

5. myNewAge = myAge + 1

6. output "In a year you will be"


7. output myNewAge
Desk-Checking Pseudocode
Code myAge myNewAge Notes
Data myAge as whole number 0 Variable
Declaration
Data myNewAge as whole number 0 0 Variable
Declaration

Output “please enter your age” 0 0 User output


Input myAge 21 0 User enters 21
myNewAge = myAge+1 21 22
Output “In a year you will be” 21 22 User output
Output myNewAge 21 22 User Output
Desk-Checking

 The code (or pseudocode) that we write defines the flow of execution through a
program.
 This is the order in which code statements are executed when the program is
running.
 So far, we have looked only at sequential flow of execution.
 Each line of code is executed after the last.
 In later weeks, we will look at ways of representing loops and choices.
 These make desk-checking more challenging.
Desk-Checking
 The full line of code is shown on the desk-check for easy reference.
 An alternate, and easier, system is to provide line numbers.

 For this, you will need to ensure that your pseudo-code programs contain line
numbers.
 Number only lines that contain pseudocode statements.
 Do not number blank lines
Pseudocode Example
1 data income as whole number
2 data taxRate as real number
3 data myTax as real number
4 data myNetPay as real number

5 output "What is your annual salary?"


6 input income

7 taxRate = 10.00
8 myTax = income * taxRate
9 myNetPay = income - taxRate

10 output "You pay the following tax:"


11 output myTax
12 output "You have the following net annual pay:"
13 output myNetPay
Desk-Checking Pseudocode
Code Line Income taxRate myNetPay myTax Notes
1 0
2 0 0
3 0 0 0
4 0 0 0 0
5 0 0 0 0 Output
6 20000 0 0 0 User inputs
20000
7 20000 10.00 0 0
8 20000 10.00 0 200000
9 20000 10.00 -180000 200000
10-13 20000 10.00 -180000 200000 Output
Desk-Checking
 Desk-checks are there to uncover errors in your logic.
 Pseudocode by itself does not always show that.
 When you have made an error, you can go back to the pseudocode and
correct it.
 By desk-checking complex algorithms, we can be sure the problem is in our
logic.
 Not in the computer code that we write.
 In this case, our tax calculation is incorrect.
Desk-Checking

7 taxRate = 10.00 7 taxRate = 10.00


8 myTax = income * taxRate 8 myTax = (income / 100) * taxRate
9 myNetPay = income - taxRate 9 myNetPay = income - taxRate

Before After

• Mistakes are going to happen – that’s inevitable.


• Pseudocode lets us ensure that we fix it at the earliest and easiest point -
before we have written a single line of code.
Desk-Check
Code Line Income taxRate myNetPay myTax Notes
1 0
2 0 0
3 0 0 0
4 0 0 0 0
5 0 0 0 0 Output
6 20000 0 0 0 User inputs
20000
7 20000 10.00 0 0
8 20000 10.00 0 2000
9 20000 10.00 18000 2000
10-13 20000 10.00 18000 2000 Output
Commenting

 The final thing to talk about in this topic is commenting.


 All programs should contain comments. These are human readable notes in
the program that are ignored when it is compiled.
 They make programs much more readable, and you should get into the habit
of doing it sooner rather than later.
Commenting

 The two main commenting styles in most languages are line-by-line or block comments. You can
use either of these for your pseudocode.

// This is a comment /*
And so is this!
*/

Line Block
Commenting

 Add comments when:


 You are doing something unusual
 This ensures that people who read your code know what it is you’re
trying to do.
 You are doing something that requires some assumptions.
 This ensures that people who read your code know what assumptions
you’ve made in writing it.
 Comments should detail your intention, not simply describe what the code
does.
Commenting

/* /*
The following algorithm calculates the The following algorithm takes one value
length of the hypotenuse of a right and squares it and then takes another
angled triangle. value and squares it. It then gives
*/ the square root of the sum of those
squares.
*/

Good Comment Bad Comment

A comment should explain to people what you were doing, not how you were doing it. The
code itself will do the latter if it’s well written (more on that later).
Commenting
1 data income as whole number
2 data taxRate as real number
3 data myTax as real number
4 data myNetPay as real number
// Taking input from User
5 output "What is your annual salary?"
6 input income
/* Calculating Tax rate
and myNetPay */

7 taxRate = 10.00
8 myTax = income * taxRate
9 myNetPay = income - taxRate

10 output "You pay the following tax:"


11 output myTax
12 output "You have the following net annual pay:"
13 output myNetPay

You might also like