Test algorithms using trace tables and test data

Resources | Subject Notes | Computer Science

IGCSE Computer Science - Algorithm Design and Problem Solving

Algorithm Design and Problem-Solving: Trace Tables and Test Data

This section focuses on techniques for testing algorithms, specifically using trace tables and test data. Understanding how to systematically test your code is crucial for ensuring it functions correctly and produces the expected results. We will cover the purpose of trace tables, how to create them, and how to use them to verify the correctness of algorithms.

Trace Tables: A Systematic Approach to Testing

A trace table is a table used to record the state of variables during the execution of an algorithm. It helps to step through the algorithm line by line, observing how the values of variables change. This allows you to identify errors in your code and ensure that it behaves as intended.

Creating a Trace Table

  1. Identify the Variables: List all the variables used in the algorithm.
  2. Determine the States: For each variable, determine its initial state and how it will change during execution.
  3. Create Columns: Create columns in the table for:
    • Iteration Number: Indicates the current step in the algorithm.
    • Variable 1: The value of the first variable at the current iteration.
    • Variable 2: The value of the second variable at the current iteration.
    • ... (Continue for all variables)
    • Result: The output or the final state of the algorithm at the current iteration.
  4. Fill the Table: Execute the algorithm step by step, recording the values of the variables in the corresponding rows.

Example: Testing an Algorithm to Calculate the Sum of Numbers in a List

Let's consider a simple algorithm that calculates the sum of numbers in a list. The algorithm takes a list of numbers as input and returns their sum.

Algorithm:

  1. Initialize a variable `sum` to 0.
  2. For each number in the list:
  3. Add the number to `sum`.
  4. Return `sum`.

Now, let's create a trace table to test this algorithm with a sample list.

Iteration Number Sum
1 10 0
2 20 10
3 30 30
4 40 60
5 50 110

In this trace table:

  • The 'Iteration' column represents the number of times the loop executes.
  • The 'Number' column shows the current number being processed from the list.
  • The 'Sum' column shows the accumulated sum up to that point.

The final 'Sum' value (110) is the correct result, indicating that the algorithm is working correctly.

Test Data: Designing Input to Cover Different Scenarios

Test data refers to the input values used to test an algorithm. Good test data should cover a range of scenarios, including:

  • Normal Cases: Typical inputs that the algorithm is designed to handle.
  • Boundary Cases: Inputs that are at the edges of the valid range (e.g., minimum and maximum values).
  • Error Cases: Invalid or unexpected inputs that the algorithm should handle gracefully (e.g., negative numbers when only positive numbers are expected).

For the sum of numbers algorithm, here are some test data examples:

  • Test Case 1: List = {10, 20, 30, 40, 50} (Normal Case)
  • Test Case 2: List = {} (Empty List - Boundary Case)
  • Test Case 3: List = {-10, 20, 30} (Error Case - if the algorithm only expects positive numbers)
  • Test Case 4: List = {0, 0, 0} (Boundary Case)
  • Test Case 5: List = {1, 1, 1, 1, 1} (Normal Case)

By using a variety of test data, you can increase your confidence that your algorithm is robust and will work correctly in different situations.

Using Trace Tables and Test Data Together

The most effective way to test an algorithm is to combine trace tables and test data. Create a trace table for each test case, and compare the actual output of the algorithm with the expected output. If the outputs match, the algorithm is likely correct for that test case. If they don't match, you need to examine the trace table to identify the source of the error and fix the algorithm.

This iterative process of testing and debugging is a fundamental part of algorithm design and problem-solving.