Home / Notes / Class 11 / Computer Science / Algorithms and flowcharts

Algorithms and flowcharts: Class 11 Computer Science

Written by Usman Tariq (Computer Science)

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.

Key points

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:

  • Finiteness: it ends after a limited number of steps.
  • Definiteness: every step is clear and has one meaning.
  • Input: it takes zero or more values.
  • Output: it gives at least one result.
  • Effectiveness: each step is simple enough to be carried out exactly.

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:

  • Oval (terminator): Start and Stop.
  • Parallelogram: input and output.
  • Rectangle: a process such as a calculation or assignment.
  • Diamond: a decision with a yes or no (true or false) question and two exits.
  • Arrows: show the direction of flow.
  • Small circle: a connector to join parts of a chart.

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:

  • Sequence: steps are carried out one after another.
  • Selection: a condition chooses between paths (IF, IF-ELSE, nested IF).
  • Iteration (loop): a group of steps repeats while a condition holds (WHILE, FOR, REPEAT UNTIL).

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.

Definitions and flashcards

Say the meaning first, then open the card to check.

Algorithm

A finite set of clear steps that solves a problem.

Pseudocode

An informal, English-like way to write an algorithm.

Flowchart

A diagram that shows an algorithm using symbols and arrows.

Selection

A control structure that chooses a path using a condition.

Iteration

Repeating a group of steps while a condition is true.

Infinite loop

A loop whose condition never becomes false.

Dry run

Tracing an algorithm by hand with sample values.

Variable

A named place that holds a value which can change.

Debugging

Finding and removing errors from an algorithm or program.

Short questions with model answers

These are the kind of questions asked for 2, 3 and 5 marks. Try each one before opening the answer.

Define an algorithm and give two of its properties. (2 marks)

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.

What is the use of a decision symbol in a flowchart? (2 marks)

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.

Differentiate between an algorithm and a flowchart. (3 marks)

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.

Name and explain the three control structures. (3 marks)

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.

Write an algorithm and draw a flowchart in words to find whether a number is even or odd. (5 marks)

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.

Why is a dry run useful? (2 marks)

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.

Numericals with worked steps

Trace the algorithm: S = 0, I = 1; while I ≤ 5: S = S + I; I = I + 1. Find S.
  1. Start: S = 0, I = 1
  2. I = 1: S = 1, I = 2
  3. I = 2: S = 3, I = 3
  4. I = 3: S = 6, I = 4
  5. I = 4: S = 10, I = 5
  6. I = 5: S = 15, I = 6
  7. I = 6 fails the test, loop ends

Answer: S = 15

Trace the algorithm: F = 1, I = 1; while I ≤ 4: F = F × I; I = I + 1. Find F.
  1. Start: F = 1, I = 1
  2. I = 1: F = 1, I = 2
  3. I = 2: F = 2, I = 3
  4. I = 3: F = 6, I = 4
  5. I = 4: F = 24, I = 5
  6. I = 5 fails the test

Answer: F = 24 (which is 4!)

A loop runs for I = 2, 4, 6, ... up to 20 and prints I. How many numbers are printed?
  1. The values are 2 × 1, 2 × 2, up to 2 × 10 = 20
  2. Count = 20 ÷ 2 = 10

Answer: 10 numbers

Common mistakes

  • Leaving out the Start or Stop oval, or drawing more than one Start.
  • Using a rectangle for a decision, or a diamond with only one exit.
  • Forgetting to change the loop variable, which gives an infinite loop.
  • Starting the sum at a wrong value instead of 0, or the product at 0 instead of 1.
  • Writing steps that are vague, such as 'do the calculation', instead of the exact formula.

Practise this chapter

Practice questions and timed test Printable worksheet

Previous: Number systems and logic gates Next: Programming in C