software development techniques - topic 02
DESCRIPTION
TRANSCRIPT
Topic 2 : Desk-Checking
Er. Pradip Kharbuja
Er. Pradip Kharbuja
What Makes a Good Algorithm?
• A good algorithm has the following qualities:
1. Complete
2. Efficient
3. Readable
4. Maintainable
5. Documented
6. Robust
Er. Pradip Kharbuja
Syntax
• The words and symbols that make up a programming language are
known as its syntax.
• Java, C, Visual Basic and so on all have their own particular
syntax.
• Example of declaring variable as integer
In Java int a;
In Visual Basic Dim a as Integer
Er. Pradip Kharbuja
Why Pseudocode?
• No need to worry about programming language
• We can focus on the logic of an algorithm rather than the programming language specific features
• Pseudo code can be useful if you're unfamiliar with the programming language
Er. Pradip Kharbuja
Pseudocode Syntax
• There are many different styles of writing pseudocode.
• There is no ‘correct’ form of pseudo code.
• But we will use the following syntax :
1. When we need to hold some value, we use the word data to set up a container for it:
syntax : Data <name> as <type>
e.g. Data myAge as whole number
• Acceptable types for now are ‘whole number’, ‘real number’, ‘string’, ‘character’, ‘boolean'
Er. Pradip Kharbuja
Pseudocode Syntax
2. When we need to get information from the user, we use the word input :
syntax : Input <name>e.g. : Input myAge
3. When we need to display anything to the user, we use output. Enclose in quotation marks.
e.g. : Output “Hello there!”• If we need to output the contents of some data, we also use
output but omit the quotation marks:e.g. : Output myAge
Er. Pradip Kharbuja
Task
Take following inputs and display them:
1. First Name
2. Last Name
3. Age
4. Salary
Pseudocode
Er. Pradip Kharbuja
Pseudocode
Er. Pradip Kharbuja
Arithmetic Operators
Er. Pradip Kharbuja
% gives remainder
e.g. 10 % 3 gives 1 20 % 4 gives 0 1 % 3 gives 1
Pseudocode Syntax
• We can put information/value in a data container like:
• When we wish to perform arithmetic calculation on numerical data, we need a container for the answer and then we use the arithmetic symbols:
Er. Pradip Kharbuja
Pseudocode Example - 1
Er. Pradip Kharbuja
Pseudocode Example - 2
• Write a pseudocode to multiply two numbers.
Er. Pradip Kharbuja
Desk-Check
Desk checking is a manual (non computerised) technique for checking the logic of an algorithm or pseudo code, ensuring there are no bugs or mistakes present.
This process is done manually without computer assistance because pseudo code is not a real programming language.
Desk checks are useful to check an algorithm (before coding) thereby confirming that the algorithm works as expected.
Er. Pradip Kharbuja
What is in a Desk-Check?
Desk-check results are documented on a table for easy reference.
The table typically has columns for
the line number
variables
a condition column, which is either true or false
an input column
an output column, which shows the end results.
and remarks column
Er. Pradip Kharbuja
Er. Pradip Kharbuja
Again, there are no specific rules for pseudo code and desk-check
Pseudocode Example - 1(with line number)
Er. Pradip Kharbuja
Desk-Checking Pseudocode Example - 1
Line No. myAge myNewAge Input Output Remarks
1 0
2 0 0
3 0 0 "Please enter your age" Output in Screen
4 21 0 21 User enters 21
5 21 22 myNewAge =
myAge + 1
6 21 22 "In next year you will be" Output
7 21 22 22 Output
Er. Pradip Kharbuja
Pseudocode Example - 2
• Write a pseudocode to multiply two numbers.
Er. Pradip Kharbuja
Desk-Checking Pseudocode Example – 2
Line Num. firstNumber secondNumber answer Input Output Remarks
1 0
2 0 0
3 0 0 0
4 0 0 0 "Please enter a
number"
Output
5 20 0 0 20 Input
6 20 0 0 "Please enter a
second number"
7 20 5 0 5
8 20 5 100 Process
9 20 5 100 "The answer is "
10 20 5 100 100Er. Pradip Kharbuja
Pseudocode Example - 3
Er. Pradip Kharbuja
Desk-Checking Pseudocode Example – 3
Line Num. income taxRate myTax myNetPay Input Output Remarks
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 20000 User inputs 20000
7 20000 10.00 0 0
8 20000 10.00 200000 0
9 20000 10.00 200000 -180000
10-13 20000 10.00 200000 -180000 -180000 OutputEr. Pradip Kharbuja
Desk-Checking
• Desk-checks help to find the errors in your logic.
• 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.
• In this case, our tax calculation is incorrect. Tax should be 10%.
Er. Pradip Kharbuja
Correction
Before After
7 taxRate = 10.00
8 myTax = income * taxRate
9 myNetPay = income - myTax
7 taxRate = 10.00
8 myTax = (income / 100) * taxRate
9 myNetPay = income - myTax
Er. Pradip Kharbuja
• Mistakes are going to happen – that’s unavoidable.
• Pseudocode lets us ensure that we fix it at the earliest and
easiest point - before we have written a single line of code.
Again,Desk-Checking Pseudocode Example – 3
Line Num. income taxRate myTax myNetPay Input Output Remarks
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 20000 User inputs 20000
7 20000 10.00 0 0
8 20000 10.00 2000 0
9 20000 10.00 2000 18000
10-13 20000 10.00 2000 18000 18000 OutputEr. Pradip Kharbuja
Desk-Checking
With a desk-check we can see where data starts going wrong
Sometimes, errors won’t be uncovered in a single desk-check
When picking user input, be awkward. What happens if you enter a negative number in our previous examples?
Er. Pradip Kharbuja
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.
Er. Pradip Kharbuja
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.
• The two main commenting styles in most languages are
1. line-by-line //this is a comment
2. block comments. /* This is block
comment */Er. Pradip Kharbuja
Commenting - 2
• Add comments when:
• You are doing something unusual
• You are doing something that requires some assumptions.
• Comments should detail your intention, not simply describe what the code does.
Er. Pradip Kharbuja
Commenting - 3
/*
The following algorithm calculates the
length of the hypotenuse of a right
angled triangle.
*/
/*
The following algorithm takes one value
and squares it and then takes another
value and squares it. It then gives
the square root of the sum of those
squares.
*/
Good Comment Bad Comment
Er. Pradip Kharbuja
• A comment should explain to people what you were doing, not
how you were doing it.
Terminologies
• Qualities of Good Algorithm
• Syntax
• Arithmetic Operators
• Desk-check
• Flow of execution
• Commenting
Er. Pradip Kharbuja
Pseudocode
Er. Pradip Kharbuja
Desk-Check with Null Values
Line num1 num2 sum usertext Output Remarks
1 0
2 0 0
3 0 0 0
4 0 0 0 Null
5 10 0 0 Null
6 10 20 0 Null
7 10 20 30 Null
8 10 20 30 “The answer is”
9 10 20 30 “The answer is” “The answer is” Output
10 10 20 30 “The answer is” 30 Output
Desk Checking
1. Take 3 user inputs and find the sum.
2. Take length and breadth of the rectangle and find the area.
Er. Pradip Kharbuja
Any QuestionsEnd of Topic - 02
Er. Pradip Kharbuja