Flowcharts and Pseudocode
Lesson Objective
In this lesson we will be learning about flowcharts and pseudocode in detail by drawing and writing them for some example problems. As you already know, both of these are techniques to represent algorithms and programming logic. They are important because they help us to explain our programming logic better and in a clear way to someone else and also it enables us to debug any error in our logic.
Flowcharts
You are already aware that flowcharts are diagrammatic representations of algorithms. You know which symbols are used to draw flowcharts and the basic guidelines to draw them. You also know about their importance and usage.
Guidelines for developing flowcharts
Flowchart should have only one start and one stop symbol
General flow of processes is top to bottom or left to right
Arrows should not cross each other
A step by step guide how to create and use flowcharts and different symbols used in them:
Follow-up question
Q. ___ is a diagrammatic representation of ordered sequences of steps to carry out an operation.
A. Algorithm
B. Flowchart
C. Computer program
D. None of the above
Check the Answer!
Ans: B) Flowchart
Tools to draw flowcharts
Now, let’s discover some tools you can use to draw flowcharts.
1. Diagrams.net
You can go to website and download the diagram drawing tool to draw flowcharts. It is easy to use and involves simple drag and drop to draw the symbols in the flowchart.
This is a fun way to draw flowcharts as opposed to the conventional pen and paper drawing.
2. Flowgorithm
A more amazing tool is Flowgorithm. Flowgorithm is a free beginner's programming language that is based on simple graphical flowcharts. It is closer to how you actually code to implement your logic in a programming language. In fact, it allows you to convert the flowcharts you create directly into the code of any programming language of your choice. You can convert your flowcharts into pseudo code as well. You can also run your programs directly in Flowgorithm.
For beginners, many programming languages can be frustratingly difficult. Many of them require you to write lines of confusing code just to display the text "Hello, world!". By using flowcharts, you can concentrate on programming concepts rather than all the nuances of a typical programming language.
But, if you don’t have experience with any programming language, it’s better to use diagrams.net for now and just focus on the problem solving aspect. When you’re more familiar with programming languages, Flowgorithm can be a great tool to use.
For windows user the flowgorithm software is directly accessible on website, however for the Mac users please follow the instructions given in the video below:
You can learn the basics of how to make a flowchart using Flowgorithm from this tutorial:
Here is a video tutorial as well.
Well!! We are all set to draw a flowchart. Now go out and try a flowchart for the sample problem statement given.
Guidelines to submit your Flowchart
Choose your method of drawing the flowchart.
Once you are done, export it as an image file and save to your local files.
Upload the image to the link attached next to the Problem statement.
Problem Statement: You have to add two numbers entered by the user.
Step 1: Marking the start of the algorithm (with an oval shaped symbol)
Step 2: Declaring variables n1,n2 and sum to be used (in a rectangular box that is used for processing and writing instructions)
📌 Note: Declaring variables is a stylistic choice and variables can be used directly as well. You will see in future lessons that some programming languages require variable declaration before use and some do not.
Step 3: Taking input of n1 and n2 from the user (in parallelogram shaped box)
Step 4: Performing sum operation for n1 and n2 (in rectangular shaped box)
Step 5: Giving sum as output (in parallelogram shaped box)
Step 6: Marking the end of the algorithm (with an oval shaped symbol)
Now, you can check your answer with the solution.
Pseudocode
Pseudocode is defined as a method of describing a process or writing programming code and algorithms using a natural language such as English. It is not the code itself, but rather a description of what the code should do. In other words, it is used as a detailed yet understandable step-by-step plan or blueprint from which a program can be written. It is like a rough draft of a program or an algorithm before it is implemented in a programming language. It can also be referred to as 'false code' or 'representation of code'.
Guidelines for writing pseudocode
You are already aware of the main constructs used in pseudocode. Now, you will learn about the general guidelines to follow while writing pseudo code.
When writing pseudocode, everyone often has their own style of presenting things out since it’s read by humans and not by a computer; its rules are less rigorous than that of a programming language. However, there are some simple rules that help make pseudocode more universally understood.
Always capitalise the initial word (often one of the main 6 constructs).
Maintain the habit of keeping only one statement per line.
Indent to show hierarchy, improve readability, and show nested constructs.
Always end multi line sections using any of the END keywords (ENDIF, ENDWHILE, etc.).
Keep your statements programming language independent.
Use the naming domain of the problem, not that of the implementation. E.g., "Append the last name to the first name" instead of "name = first or last."
Keep it simple, concise, and readable.
Following these rules help you generate readable pseudocode and give you the ability to recognize an ill-written code.
Tool to write Pseudocode
You can use any text editor for writing pseudocode because it is only written for representation of algorithms and program logic. But, if you want to be able to run the logic you write, even though that might reduce some flexibility of how you write the algorithm, you can do so with an awesome online tool called pseudoeditor.com.
Guidelines to submit your Pseudocode
Choose your method of editor.
Once you are done, save it as a .txt file on your local file.
Upload the txt file to the link attached next to the Problem statement.
Let's try a sample problem now.
Problem Statement: You have to add two numbers entered by the user.
Check the solution below, once you are done.
Check the Pseudocode!
BEGIN
NUMBER n1, n2, sum
INPUT n1, n2
SUM = n1 + n2
PRINT sum
END
Let's try more such Flowchart and pseudocode based problems.
Remember, this is the first step to problem solving!!
Problem Statement: Find the largest among three different numbers entered by the user.
(You can skip to check these steps and try on your own)
Step 1: Mark the start of the algorithm
Step 2: Declare a,b,c the three numbers
Step 3: Take a,b,c as input
Step 4: Write a condition to check if a>b or not (inside a diamond shaped decision box); if above condition is true goto step 5 else goto step 6
Step 5: We come to this step if a>b and now we check if a>c or not. If this condition is true then we will output "a" else we will output "c" according to our logic.
Step 6: We come to this step if a is lesser than b and now we check whether b is greater than c or not. If this condition is true we output "b" else we output "c".
Step 7: We mark the end of algorithm
Flowchart
Now, you can check your answer with the solution.
Pseudocode
Check the Pseudocode!
START
INPUT a, b, c
IF a > b
IF a > c
PRINT a
ELSE
PRINT b
END IF
ELSE
IF b > c
PRINT b
ELSE
PRINT c
END IF
END IF
STOP
Problem Statement: Find the sum of series 1+2+3+…..+N.
Step 1: Mark the start of the algorithm
Step 2: Declare i and sum with values 1 and 0 respectively.
Step 3: Take N as input
Step 4: Write a condition to check if i <= N or not (inside a diamond shaped decision box); if the condition is true goto step 5 else goto step 6.
Step 5: We come to this step if i is still less than or equal to N. Now, increase the value of sum to sum+i. Then, increment i by 1. Then go to step 4 (this is a loop)
Step 6: We come to this step when i is greater than N. Now, print the value of sum.
Step 7: We mark the end of algorithm
Flowchart
Now, you can check your answer with the solution.
Pseudocode
Check the Pseudocode!
START
sum = 0, i = 1
INPUT n
WHILE i <= n
sum = sum + i
i = i + 1
END WHILE
PRINT sum
STOP
Now, it's time to think steps on your own. 🔥
Problem Statement: Find the roots of a quadratic equation aX^2 + bX + c = 0
Flowchart
Now, you can check your answer with the solution.
Pseudocode
Check the Pseudocode!
START
INPUT a,b,c
d = bb - 4a*c
IF (d equals 0)
r1 = r2 = (-b) / 2*a
ELSE IF (d > 0)
r1 = ((-b)+sqrt(d) / 2*a
r2 = ((-b)-sqrt(d) / 2*a
ELSE
r_part = (-b) / 2*a
i_part = sqrt(d) / 2*a
r1 = r_part + i.i_part
r2 = r_part - i.i_part
ENDIF
PRINT r1, r2
STOP
Problem Statement: Find The LCM (Lowest Common Multiple) of two numbers taken from the user as input.
📌 Note: LCM is the smallest positive integer that is perfectly divisible by both numbers
Flowchart
Now, you can check your answer with the solution.
Pseudocode
Check the Pseudocode!
START
INPUT n1
INPUT n2
IF n1 > n2
lcm = n1
ELSE
lcm = n2
rem1 = lcm % n1
rem2 = lcm % n2
WHILE (rem1 > 0 OR rem2 > 0)
lcm = lcm + 1
rem1 = lcm % n1
rem2 = lcm % n2
ENDWHILE
PRINT lcm
STOP
📌 Note: As you may already know, the remainder is being calculated by the modulus operator(%) which gives us the remainder that is left on dividing the first operand by the second operand.
Problem Statement: Print the Fibonacci Series upto n terms. A series of numbers in which each number is the sum of the two preceding or previous numbers is called Fibonacci Series. The first few terms are as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, … , n.
📌 Note: The first two terms are by default 0 and 1. And their sum forms the third term and similarly the series propagates.
Flowchart
Now, you can check your answer with the solution.
Pseudocode
Check the Pseudocode!
START
INPUT n
a = 0
b = 1
PRINT a, b on separate lines
i = 2
WHILE i <= n
sum = a + b
a = b
b = sum
i = i + 1
PRINT sum on next line
END WHILE
STOP
Useful resources for you:
Flowchart examples: https://visme.co/blog/flowchart-examples/