We've previously looked at the basic principles behind computer programming using SmallBasic. We covered a dozen or so instructions, but to write more complicated programs you’ll require further knowledge of SmallBasic commands and concepts.

In the previous tutorial, we provided short programs to illustrate each instruction; only in the final step did we leave you on your own to write a program that would achieve a particular task. It’s often true that you learn better by doing things by yourself, so we’ll continue to follow that theory in the second part of our programming workshop.

Bear in mind that there’s more than one way to write each program, and our examples are exactly that. You may come up with a different but equally correct solution to the same task.

We won’t set you afloat without a paddle from the outset, though. You’ll first need to get to grips with some new statements and concepts. We’ll introduce these by supplying you with the code that you’ll need to input in order to run the programs.

Having completed this two-part tutorial, you’ll have the necessary knowledge to start writing your own programs. An expert programmer you won’t be, but you will understand the basic principles required to continue learning the SmallBasic language.

Practice makes perfect: keep at it.

## SmallBasic: Complex numbers

To understand the Mandelbrot Set, the subject of our final program, you need a working knowledge of complex numbers and the way they’re displayed on the complex plain.

‘Real’ numbers – positive and negative, whole and fractional – can be represented as points on a line. Negative numbers (such as -125 and -19.6) are on the left, zero is in the middle and positive numbers (such as 0.01 and 765) are on the right. Some numbers don’t appear: for example, you won’t find the square root of minus 1.

Mathematicians refer to the square root of minus 1 as i. It’s just one of an infinite number of imaginary numbers. Like real numbers, the imaginary numbers can be represented as points on a line. It has negative imaginary numbers (such as -10i and -5.5i) on the left, zero in the middle, and positive imaginary numbers (such as 3.4i and 250i) on the right. Some problems, such as 2-7 and 9/71, have answers on the real number line; others, such as √-3 and 99xi have answers on the imaginary number line. But some don’t have answers on either line.

To provide answers to any possible mathematical problem, we have to use so-called complex numbers that have a real and an imaginary component – for example, 5-1.9i and -6.6+101i. These are represented as points on a plane, in which the real number line forms the horizontal axis and the imaginary number line forms the vertical axis.

## SmallBasic: The Mandelbrot Set

Our final program generates the Mandelbrot Set. This has been described as the most astonishing figure in mathematics: it’s infinitely complicated, yet generated using a simple operation.

The Mandelbrot Set is plotted on the complex plain. To draw the Mandelbrot Set, we test points on the complex plain. Start with the value zero (where both real and imaginary parts are zero), square it, then add the complex number you wish to test. If the modulus of the complex result (this is the distance from the complex plain’s origin) is two or more, the point isn’t in the Mandelbrot Set.

If the modulus is less than two, perform the same operation again. Square the previous result and add the value being tested. Again, if the modulus of the result is two or more, the point you’re testing isn’t in the Mandelbrot Set.

For some points, the modulus will never exceed two – no matter how many times this operation is carried out. These points belong to the Mandelbrot Set. It isn’t possible to carry out this operation an infinite number of times, so set a limit of 200 on the number of iterations. Points are considered to be in the Mandelbrot Set if the result of the operation hasn’t yielded a result with a modulus greater than two by the time this limit is reached.

## SmallBasic: Beyond the basics

**Step 1.** SmallBasic supports variables called arrays. These are lists of numbers. Use the DIM statement to tell SmallBasic how many values an array can hold, then assign it a name. The values can be accessed using a number in brackets after it. Try the program shown above to generate Fibonacci numbers.

**Step 2.** Variables can also contain text strings. Basic requires these to end with a $; SmallBasic does not. The + sign that joins string variables doesn’t allow you to perform arithmetic, but you can manipulate them in other ways. See SmallBasic’s Help file for details. The program shown below hints at what’s possible.

**Step 3.** Next, a couple of useful extras. You can put more than one statement on a line, separating them with a colon. If you terminate a PRINT statement with a comma (or a semicolon), the next PRINT will continue on the same line rather than starting a new one. The program shown below uses both these tricks.

>> Continues