Home / Notes / Class 11 / Computer Science / Algorithms and flowcharts
Problem solving steps, definition and properties of an algorithm, pseudocode, flowchart symbols, control structures of sequence, selection and iteration, tracing with dry runs, and common examples.
Problem solving is a planned way to reach a solution. The usual steps are: understand and define the problem, find the inputs, processing and outputs, design an algorithm, write the program, test and debug it, and document it.
An algorithm is a finite, ordered set of clear steps that solves a problem. A recipe is a daily-life example. The same algorithm can be written as plain steps, as pseudocode or as a flowchart, and then coded in any language.
A good algorithm has these properties:
Pseudocode is a way of writing an algorithm in short English-like statements without the strict syntax of a language. Typical words are INPUT, OUTPUT, SET, IF, THEN, ELSE, WHILE and FOR. It is easy to read and easy to turn into code.
Flowchart symbols:
A flowchart should start at the top, flow downward and from left to right, use each symbol for its own purpose, and have only one Start. Decision diamonds must have labelled exits. Keep the text inside symbols short.
Every algorithm is built from three control structures:
In a loop, the condition is tested each time. A loop needs a starting value, a test and a change that eventually makes the test fail. If the change is missing, the loop never ends and is called an infinite loop.
Example, largest of three numbers A, B, C: set MAX to A. If B is greater than MAX, set MAX to B. If C is greater than MAX, set MAX to C. Output MAX. This uses sequence and selection.
Example, sum of the first N natural numbers: set SUM to 0 and I to 1. While I is less than or equal to N, add I to SUM and add 1 to I. When the loop stops, output SUM. For N = 4 the values added are 1, 2, 3 and 4, so SUM is 10.
A dry run (trace table) means following the algorithm by hand with sample data, writing the value of each variable after every step. It helps find logic errors before coding. Testing with normal, boundary and wrong data shows whether the algorithm is correct.
Algorithms are compared by how many steps they need and how much memory they use as the input grows. Searching a list one item at a time (linear search) is simple, while halving a sorted list again and again (binary search) is much faster for large lists.
Study tip: Always give a flowchart clear labels and arrows, and trace your algorithm once with a small sample like N = 3 before submitting. In long questions, state inputs, processing and outputs first, because marks are given for each.
Say the meaning first, then open the card to check.
A finite set of clear steps that solves a problem.
An informal, English-like way to write an algorithm.
A diagram that shows an algorithm using symbols and arrows.
A control structure that chooses a path using a condition.
Repeating a group of steps while a condition is true.
A loop whose condition never becomes false.
Tracing an algorithm by hand with sample values.
A named place that holds a value which can change.
Finding and removing errors from an algorithm or program.
These are the kind of questions asked for 2, 3 and 5 marks. Try each one before opening the answer.
An algorithm is a finite sequence of clear, ordered steps used to solve a problem. Two properties are finiteness, meaning it must end, and definiteness, meaning each step has one clear meaning.
The diamond symbol shows a question whose answer is yes or no. It has two exits, one for each answer, so the flow can take one of two paths. It is used for selection and for the test in a loop.
An algorithm is a list of steps written in words or pseudocode. A flowchart is a picture of the same steps made with symbols and arrows. A flowchart is easier to follow at a glance, while an algorithm is easier to write and change.
Sequence runs the steps in order, one after another. Selection tests a condition and picks one path, for example IF marks are 40 or more then PASS else FAIL. Iteration repeats steps while a condition is true, for example adding numbers in a loop. Any program can be built from these three.
Algorithm: Step 1 Start. Step 2 Input N. Step 3 Find R as the remainder of N divided by 2. Step 4 If R is 0, output Even, else output Odd. Step 5 Stop. Flowchart: an oval Start, a parallelogram for Input N, a rectangle R = N mod 2, a diamond asking R = 0, with the Yes exit going to a parallelogram Output Even and the No exit to a parallelogram Output Odd, both joining at an oval Stop.
A dry run lets us test the logic by hand with sample data before any code is written. By writing the value of each variable at every step we can see wrong results early and fix them. It saves time during programming.
Answer: S = 15
Answer: F = 24 (which is 4!)
Answer: 10 numbers
Previous: Number systems and logic gates Next: Programming in C