6.1. Algorithms#

An algorithm is a set of precise instructions that when carried out will solve a task. Any time you are solving a problem you are usually applying some approach, which could be written up as an algorithm. There are many different types of algorithms that can be applied to many different types of problems. The effectiveness of the algorithm can often be measured by how efficient the algorithm (how quickly it reaches a solution) and the quality of the solution it yields.

Key features of algorithms include sequence, selection and iteration.

6.1.1. Sequence#

../../_images/1_example1.png

Instructions that are carried out one after the other are sequential. This means the instructions are carried out in the exact order they are written.

6.1.2. Selection#

../../_images/1_example2.png

Some algorithms can select different pathways to take. In this case the algorithm will check a condition (or sometimes multiple condition) and the evaluation of the condition (i.e. whether the condition is true or false) will control which pathway is taken.

6.1.3. Iteration#

../../_images/1_example5.png

Iteration allows sections of an algorithm to be repeated a fixed number of times or until a certain condition has been met.

6.1.4. Subroutines#

../../_images/1_example4.png

Subroutines are algorithms that can be used in the main algorithm. They can be very useful particularly when there are aspects of the algorithm that are repetitive. They can also make it easier to write algorithms as they can be used to solve subtasks. Use of subroutines often help to make the overall algorithm easier to understand as the main algorithm can be written more concisely.

Question 1

Which of the following could be used to perform iteration? Select all that apply.

  1. if statements

  2. if-elif-else statements

  3. print statements

  4. the time module

  5. while loops

  6. for loops

  7. def ining a function

Solution

E. F.

Question 2

Which of the following could be used to perform selection? Select all that apply.

  1. if statements

  2. if-elif-else statements

  3. print statements

  4. the time module

  5. while loops

  6. for loops

  7. def ining a function

Solution

Solution is locked

Question 3

Which of the following could be used to perform subroutine? Select all that apply.

  1. if statements

  2. if-elif-else statements

  3. print statements

  4. the time module

  5. while loops

  6. for loops

  7. def ining a function

Solution

Solution is locked

Question 4

Consider the following flowchart. Which of the following are used in this algorithm? Select all that apply.

../../_images/1_question3.png
  1. Sequence

  2. Selection

  3. Iteration

  4. Subroutine

Solution

Solution is locked

Question 5

Consider the following flowchart. Which of the following are used in this algorithm? Select all that apply.

Get x
Get y
Get operation
IF operation == 'add' THEN
    Display x + y
ELSEIF operation == 'subtract' THEN
    Display x - y
ENDIF
  1. Sequence

  2. Selection

  3. Iteration

  4. Subroutine

Solution

Solution is locked

Question 6

Consider the following flowchart. Which of the following are used in this algorithm? Select all that apply.

BEGIN
    FOR i = 0 TO 2 STEP 1
        doubled = add(i. i)
        Display doubled
    NEXT i
END


BEGIN add (x, y)
    RETURN x + y
END add (x, y)
  1. Sequence

  2. Selection

  3. Iteration

  4. Subroutine

Solution

Solution is locked