Chapter Sixteen
Putting It All Together

This is our last time together in this series, so by now (hopefully) this program should have been just a little bit easier. If you were smart, you might have even taken some bits and pieces of previous programs and glued them in! Enough chat, let's develop our multiple-sort program.

First, we will seed the random number generator and clear the screen:

```

RANDOMIZE TIMER / 3

CLS```

Now we will ask for the number of digits to generate, and check that the response was within 5 and 20:

```

getcount:

PRINT "Enter Number of Random Digits to Generate, between 5 and 20 ";

INPUT NUMS

IF NUMS < 5 OR NUMS > 20 THEN GOTO getcount```

Next, we will allocate the storage space needed for both the numeric and string versions of the random numbers:

```

DIM NUMBER(NUMS), NUMBER\$(NUMS)```

Let's clear up the screen and print out our first column heading:

```

CLS

LOCATE 1, 2

PRINT "Unsorted List"```

Now we will generate our array of random numbers, and print them out as we generate them. We can do this all within the same loop. We will then beep the speaker when we are done, just like we were asked:

```

FOR N = 1 TO NUMS

NUMBER(N) = INT(50 * RND) + 1

LOCATE N + 2, 2

PRINT NUMBER(N)

NEXT N

BEEP```

First, we will sort the numeric array (as numbers):

```

FOR OUTER = 1 TO NUMS - 1

FOR INNER = OUTER TO NUMS

IF NUMBER(INNER) < NUMBER(OUTER) THEN SWAP NUMBER(INNER), NUMBER(OUTER)

NEXT INNER

NEXT OUTER```

Next. we will print out our second column heading and the sorted list, then beep again:

```

LOCATE 1, 26

PRINT "Sorted As Numbers"

FOR N = 1 TO NUMS

LOCATE N + 2, 26

PRINT NUMBER(N)

NEXT N

BEEP```

Now to sort them as strings, we first need to convert them to strings:

```

FOR N = 1 TO NUMS

NUMBER\$(N) = STR\$(NUMBER(N))

NEXT N```

Now we sort the string array, the same as the numeric array:

```

FOR OUTER = 1 TO NUMS - 1

FOR INNER = OUTER TO NUMS

IF NUMBER\$(INNER) < NUMBER\$(OUTER) THEN SWAP NUMBER\$(INNER), NUMBER\$(OUTER)

NEXT INNER

NEXT OUTER```

And, just like the others, we will display the column title, and the sorted string array, then beep:

```

LOCATE 1, 52

PRINT "Sorted as Characters"

FOR N = 1 TO NUMS

LOCATE N + 2, 52

PRINT NUMBER\$(N)

NEXT N

BEEP```

We're done:

```

END```

For those of you who don't like all of that typing, you can download the final program.

See how simple it can be when you break it down into smaller sections? That leads us to the next point. We are only using a small amount of the power of QBASIC. QBASIC allows us to write small programs like this and incorporate it into much larger programs! Not only is the program more organized, but it is also easier to debug (find and fix mistakes), and has the added benefit that if you use a certain function (like sorting) many times, you only need to write the sort routine once!

All of the programming we have done in this series is referred to as top-down programming - In other words, the program starts at the top, and just goes on down, doing what it needs to do, until it hits the end at the bottom. There is very little jumping around in the program. The type of programming technique described in the previous paragraph is called structured programming, and is the preferred way to write programs. In fact, in many of the more advanced languages, it is the only way that can be used!

QBASIC also has a very robust set of graphics commands and functions that we haven't even touch upon (or hinted at)! It is great for creating your own graphs, or you can use it for making your own graphics-based games. In fact, my train simulator program was originally developed in QBASIC! I had to move it over to QUICKBASIC for technical reasons to make it work (I usually write programs using multiple source files, which QBASIC does not allow, among other things), but it only uses regular QBASIC graphics functions! (Some of the other functions use assembly language to make them work).

Happy Programming!