Problem Solving (Pseudocode, Algorithm & Techniques)

Pseudocode is an informal language that helps programmers describe the steps of a program’s solution without using any programming language syntax.

previous post Introduction to Problem-Solving

EXAMPLE 2 Draw a flowchart to add two numbers, along with an algorithm in simple English.

SOLUTION

Algorithm

  • Step 1. Start
  • Step 2. Get two numbers N1 and N2
  • Step 3. SN1+ N2
  • Step 4. Print the result S
  • Step 5. Stop
Problem Solving (Pseudocode, Algorithm & Techniques)
Algorithm

EXAMPLE 3 Draw a Flowchart to find the Area and Perimeter of the Rectangle, along with the algorithm in simple English.

L: Length of Rectangle, B: Breadth Rectangle
AREA Area of Rectangle, PERIMETER: Perimeter of Rectangle

SOLUTION

Algorithm

  • Step 1. Start
  • Step 2. Input Side-Length & input value say L, B
  • Step 3. AREA <– L x B
  • Step 4. PERIMETER <– 2x (L + B)
  • Step 5. Print AREA, PERIMETER
  • Step 6. Stop
Problem Solving (Pseudocode, Algorithm & Techniques)

EXAMPLE 4 Draw a flowchart to determine the larger of two numbers, along with an algorithm in simple English.

SOLUTION

Algorithm

Step 1. Start

Step 2 Get two numbers a and b

Step 3 If a>b then print an else print b

Step 4 Stop

Problem Solving (Pseudocode, Algorithm & Techniques)

Pseudocode

Pseudocode is a “text-based” detail (algorithmic) design tool. Pseudocode creates an outline or a rough draft of a program that gives the idea of how the algorithm works and how the control flows from one step to another. For example, consider the following pseudocode:

If student's marks is greater than or equal to 50
     display "passed"
 else
     display "failed"
NOTE

Pseudocode is an informal way of describing the steps of a program’s solution without using any strict programming language syntax or underlying technology considerations.

The above pseudocode gives you an idea of how a program determines whether a student has passed or failed to compare the marks of the student.

Consider the same algorithm that we talked about in example 4 (determine if the first number is greater than the second number or not). The pseudocode of this algorithm can be like :

Input first number in variable firstnum.
Input second number in variable secondnum
if the firstnum is > the secondnum
display 'the first number is greater than second number'.
else
display 'the first number IS greater than second number'.
NOTE

In pseudocode, usually, the instructions are written in upper case, variables in lowercase, and messages in sentence case.

Please note there are no standard rules to writing pseudocode as it is totally informal way of representing an algorithm.

Advantages and Disadvantages of Pseudocode

The pseudocode is a very useful and helpful tool in algorithm development. It offers many advantages and it has some disadvantages too.

Pseudo-code Advantages

  • It uses a language similar to everyday English, thus is easy to understand.
  • It highlights the sequence of instructions.
  • The structure of an algorithm is clearly visible through pseudocode, e.g., selection and repetition blocks.
  • Pseudocode can be easily modified without worrying about any dependencies.

Pseudo-code Disadvantages

  • It is not a visual tool like flowcharts.
  • Since there is no accepted standard for pseudocode, so it varies from programmer to programmer.
  • It can only be tried on paper, there is no program available to try it.
  • It is an informal representation of an algorithm.

Verifying an Algorithm

Verification of an algorithm means ensuring that the algorithm is working as intended. For example, if you have written a program to add two numbers but the algorithm is giving you the product of the two input numbers. In this case, the algorithm is not working as intended.

What is an algorithm?

An Algorithm refers to a sequence of instructions, a finite set of commands, or a method of working. It can be represented as a sequence of instructions to be carried out until an endpoint is reached as per the rules, conditions, or sequence by which the computer or people tackle a problem or situation.

Discuss two common tools for developing an algorithm.

Two common tools for developing an algorithm are flowchart and pseudocode. A flowchart is a diagrammatic or pictorial/graphical representation that illustrates the sequence of operations to be performed for the solution of a problem. Pseudocode is an idiomatic high-level description of a computer program or algorithm. It is written in symbolic code in English which must be translated into a proper code using a specific programming language.

NOTE

The word ‘algorithm’ comes from the ninth-century Arab mathematician, Al-Khwarizmi, who performed on ‘written processes to achieve some purpose.’ The term ‘algebra’ also comes from the term ‘al-jabr,’ which he presented.

Then how do you verify an algorithm?

Ohh, yeah you’re right – we should test it with a sample input for which we know the output; if the expected output matched with the output produced by the algorithm, the algorithm is verified.
This is really useful, but for larger algorithms, we may want to verify the medium results too of various steps of the algorithm. And for such conditions, a useful tool for verifying algorithms called Dry-Run is used.

Dry Run

A dry run is the process of a programmer manually working via their code to trace the importance of variables. There is no software involved in this method.

Traditionally, a dry run would involve a printout of the code. The programmer would sit down with a pen and paper and manually follow the importance of a variable to check that it was used and updated as expected.

If a programmer discovered that the value is not what it should be, they are able to identify the area of code that resulted in the error.

Characteristics of a dry run are:

  • It is carried out during design, implementation, testing, or maintenance.
  • It is used to identify the logic errors in a code.
  • It cannot find the execution errors in a code.
Trace Tables

Dry running an algorithm is carried out using trace tables where the impact of each line of the code is seen on the values of variables of the algorithm. As per the line of the code, the processing that takes place is applied to the variables’ values.

For example, we want to calculate the double of the numbers in sequences 1, 2, and 3 and print the value as 1 added to the double value:

1. FOR number 1 TO 3
2. Val number * 2
3. PRINT Val + 1

The trace table for the above code will record the code movement and its impact on the variables, line by line :

Problem Solving (Pseudocode, Algorithm & Techniques)
trace table

So the given code’s trace table will be as shown above when the code is dry-run. The above-given trace table has documented the impact of each line of the code separately.

However, you can skip this style and simply show the impact of code on variables too, e.g.,
shown below:

Problem Solving (Pseudocode, Algorithm & Techniques)
the impact of code on variables

You can choose to skip line numbers too if you want as we have done in the example below.

EXAMPLE 12 Dry-run the code given below
and show its trace table.

SOLUTION

num ← USER INPUT


num ← USER INPUT

count ← 0

WHILE num < 500 THEN

num ← num * 2

count ← count + 1

end while

PRINT num

PRINT count

SOLUTION
Dry run the code - Help Of Ai

Comparing Algorithms

To solve a problem, many a time, in fact, mostly multiple solutions are available. In other words, you may have multiple algorithms which work correctly and produce the correct, desired results. In such a case, which algorithms should you choose for coding?

In order to decide which algorithm to choose over another, their efficiency will be the deciding factor.

Major factors that govern the efficiency of an algorithm are:

  • the time it bears to find the solution and
  • the resources which are consumed in the method.

For instance, while buying a car, how do you choose a car from the available choices? Yup, you are absolutely right; you might consider both the speed and the fuel consumption. The factor that matters the most to you, will win

Similarly, for algorithms, the two deciding factors are :

Space-wise efficiency.

How much amount of (memory) space the algorithm will take up before it terminates? For this, we consider the amount of data the algorithm uses while running.

Time-wise efficiency.

How much time accomplishes the algorithm take to finish? This factor is dependent on so many issues like RAM available, the programming language is chosen, the hardware platform, and many others.

When you have a hardware platform fixed with a specific RAM size, then space efficiency becomes the deciding factor and the algorithms that take less space are chosen. With this, we have come to the end of this article. Let us quickly revise what we have learned so far in this chapter.

Picture of Hoa
Hoa

Leave a Comment