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

**Step 4.** If you use a GOSUB statement instead of GOTO, the program will jump to the line number specified; a RETURN statement returns it to the line after the GOSUB. The code between the lines referred to in the GOSUB and the RETURN is called a subroutine. These make code easier to read and allow you to use it in different parts of your program.

**Step 5.** We’ll use data in a separate file to draw a pattern. Using Notepad, save a file called DATA.TXT in the same folder as your SmallBasic programs. Enter the following values:

250, 0

500, 200

375, 200

375, 500

125, 400

125, 200

0, 200

250, 0

-999, -999

**Step 6.** Alter the previous walkthrough's Step 11 program. Add OPEN “DATA.TXT” FOR INPUT AS #1 to the beginning. Change the two READ statements to INPUT statements. These usually prompt you for input data, but the following variant reads data from the file number specified in the OPEN statement: ‘INPUT #1, X, Y’.

**Step 7.** SmallBasic features many functions that work with numeric values. The program shown below uses SIN and COS, but for trigonometric functions the angle must be in radians rather than degrees; D2R is a conversion factor. Note the rounding errors in the results – sin(90) is really 0.

**Step 8.** Enter PSET SCREENX, SCREENY COLOR 1 inside a FOR/NEXT loop that increments SCREENX from 0 to 599. Put this inside a FOR/NEXT loop that increments SCREENY from 0 to 399. You’ll see a black block. Temporarily put ‘DELAY 10’ after the PSET to see that it draws one dot at a time. Save this program.

**Step 9.** Start a new program. INPUT the real and imaginary parts, call a squaring subroutine (starting at line 500), and PRINT the answer. The real part is real squared minus imaginary squared; the imaginary part is two times real times imaginary. Test with real -= 0.1, imaginary = 1; the result should be real = -0.99, imaginary = 0.2.

**Step 10.** Next, INPUT two complex numbers. Alter the subroutine to add the second complex number to the result of squaring the first. The real part of the sum of two complex numbers is the sum of the real parts. The imaginary part is the sum of the two imaginary parts. Verify the results with an online complex calculator.

**Step 11.** Modify the previous program to INPUT a complex number and PRINT a message as to whether it’s in the Mandelbrot Set. To get the modulus square the real and imaginary parts, add them together and square root the sum. You should find that real = -1, imaginary = 0 is in the set, and real = -2, imaginary = 1 is not.

**Step 12.** Use Step 11’s logic inside the nested loops of Step 8’s program to test points on the complex plane, drawing a dot with the PSET statement only for points in the Mandelbrot Set. Test points from real = -2 and complex = 1 to real = 1, complex = -1. Derive these from SCREENX and SCREENY (for example Real = SCREENX/200-2).

**Step 13.** The Mandelbrot Set is often shown in colour, with points outside the Set displayed as contours. Modify the previous program to plot points that aren’t in the Set, using a colour derived from the count of how many iterations it required to exceed a modulus of two. Colour = 16 – Count / 8 works well.

**Step 14.** The Mandelbrot Set is a fractal figure. The more you zoom, the more detail you see. To fully appreciate it, modify the software to show an area for which you specify the real and imaginary parts of the complex number and a zoom factor. The screenshot below shows what you’ll see if real = -1.15, imaginary = 0.4, and zoom = 5.5.