Welcome to the fourth episode of the video course on Basic language. Today we introduce the vectors, also called array, which are data structures. We also see the concept of average, minimum and maximum value of a sequence of numbers.
Last but not least, the program is useful for monitoring your children's academic performance and… automatically insulting them if their grades are too low! Of course, you can change the insults with others to your liking!
The program we create can be run on the QB64 and also on many different Commodore: on the Commodore 16, on the Commodore Vic20, on the Commodore 64, on the Commodore 128, but also on PET, even on emulators (such as VICE and CCS64) and you can run it also on the old GWBASIC that was in the PC DOS.
The QB64 (QuickBasic) allows you to compile the program in Basic and to create executable files under Windows, Linux and MacOS.
Arrays
So let's start the arguments of the fourth episode of the video course on Basic language: let's study vectors (array). You can think of them as a sequence of elements (variables), which can be called up from an integer index.
In the video, we do a test with the Commodore 64. We must first define the array. The command used is DIM. The arrayss can be of different types, let's start by defining an array of 3 strings.
We enter the first string at index 0, the second string at index 1 and, finally, the third string at index 2.
Then, let's try to recall the individual elements of the array.
It is not possible to recall elements with an index higher than what we have declared with the DIM command: the computer displays an error.
The index can also be a variable. By assigning, for example, the value 1 to the variable I, we can recall the corresponding element of the array. The same happens by assigning the value 0 and the value 2.
DIM A$(2) A$(0)="FORCHETTA" A$(1)="COLTELLO" A$(2)="CUCCHIAIO" I=1: PRINT A$(I)
When declaring the array with the DIM command, it is not possible to declare it a second time. Other Basic types support resizing or deleting arrays, but the Commodore 64 doesn't have these commands.
In the example program of the video course on Basic language, which we will study shortly, we do not use string type arrays, but floating point numeric type.
Video course on Basic language, the example program
At the bottom of this page, you can find the sample program for this lesson. If you don't have interfaces like SD2IEC, Kung Fu Flash, you can type it.
In the first three lines of the program (10, 20 and 30) we find education REM, useful for including comments. We have already analyzed this instruction in the first episode of the course.
Lines 50 and 60 contain the instructions PRINT and INPUT, we have already talked about it.
Regarding INPUT, already allows you to write the question to ask the user on the monitor. Unfortunately, if the demand exceeds the width of the monitor, education INPUT fails to enter data correctly.
This is why I divide the question with an instruction PRINT and then there INPUT. Obviously, I'm based on the width of the smallest monitor, that of the Commodore Vic20, which is only 22 characters.
The computer asks the user to indicate the number of votes to calculate the average, maximum and minimum value. The number of votes is entered in the numeric variable NV.
Education 70 controls, through IF… THEN, that the variable is within the limits set by the program. In fact, in this program, we can process a maximum of 20 votes. Furthermore, it is not possible to indicate less than 1 vote.
When the number of votes is out of bounds, the question on line 50 is reformulated, adding “RI” before the request. The computer writes "RIDIGITA" instead of "ENTER" to the monitor.
We talked about the instructions IF… THEN in the second episode of the course.
Line 80 specifies on the monitor that the program requires the insertion of the pupil's grades.
Array dimensioning
We finally get to the heart of this episode of the video course on Basic language. On line 90, we define the array V of numeric variables!
A vector of 20 elements + 1 (element 0) is created, but we do not use it.
From line 100 to line 150 there is a cycle FOR CV … NEXT CV already seen in episode 3 of the course.
The computer asks, in sequence, all the grades of the pupil. Each vote is included in the index CV vector V. We have already entered, previously, in the variable NV, the number of votes that the program asks you to type.
Here too there is control over the votes cast. If a grade entered is less than 0 or greater than 10, the program asks you to type it again.
Furthermore, in line 140, line 400 is recalled, which allows you to write, after each vote, a comment or an insult, if the vote were negative.
We have already talked about the instructions GOSUB, RETURN, which allow you to temporarily move the execution of the program.
The vote typed, and inserted first in the array V index CV, is copied to the variable G.
On the variable G, which represents the grade entered, the program carries out evaluations. The evaluations are printed on the monitor.
It is possible to customize these evaluations by modifying the strings present in lines 410 – 450.
Through education RETURN, present after each evaluation (at lines 410 – 460) the program returns to the cycle FOR CV … NEXT CV, to request the entry of the next vote.
Once all the NV votes, the array V it is filled from index 1 to index NV.
Video course on Basic language, calculations on arrays: average value
From lines 200 to 270 the computer performs all the calculations.
To calculate the mean, we first need to define a variable sum, which we call SM.
All the NV votes of the array, through a second cycle FOR CV … NEXT CV.
After adding up all the marks, the average ME it is calculated by dividing the sum SM of votes by the number of votes themselves, NV.
The divider NV it must obviously be greater than 0 and we checked this at the beginning of the program, on line 70.
Maximum and minimum value of a sequence of numbers
Regarding the maximum value, it is calculated in this way.
First, the variable is defined MA, which represents the maximum value, and the value of the first grade entered is assigned.
The computer compares each vote, present in the array, with the variable MA. Only if the value of the vote present in the index CV of the array is greater than the value of the variable MA, the latter takes its value.
At the end of the loop, the variable MA will contain the maximum value present in the array of votes V.
A similar reasoning is used to calculate the minimum value.
The variable is defined ME who is initially assigned the first vote.
Scanning all elements of the vote vector V, from first to last NV, when a grade is less than the value present in the variable ME, this is overwritten with the lowest grade.
At the end of the loop, the variable ME will contain the minimum value present in the array of votes V.
After calculating the data that interest us, that is, the average ME of votes, the highest grade MA and the minimum grade ME, the program proceeds to execute on line 300.
Monitor printing of the results of the array processing
The part of the program between lines 300 and 370 writes the processing results to the monitor.
The computer first writes the average of the marks. After writing the grade point average, the value of the average ME is assigned to the variable G, to allow the computer to issue judgment when line 400 is invoked.
Then he writes on the monitor the maximum grade, and the relative judgment.
Finally he writes the minimum grade, together with his judgment.
With line 380 you get to education END, which ends the program.
In the video, we try the program with the Commodore 16.
Video course on Basic language # 4, the listing
And here is the listing, the program is available in two formats:
- CORSO-BASIC-4. TXT, in text format, to analyze it on a PC or to transcribe it on the various Commodore, on the DOS GwBasic and on the QB64;
- CORSO-BASIC-4. ZIP, ZIP compressed folder to unpack. It includes the program in PRG format, loadable and bootable from the Commodore 64 and a virtual disk D64 containing the program loadable from the C16, Vic20, C64 and C128, as well as emulators (CCS64, VICE or others). It also contains the BAS file to be loaded with the QB64.
Credits: in the video course, SID music is Super Hang-On by Steve Barrett.
The topics of the course are still many!
To be notified when other tutorials, reviews and subsequent installments of the BASIC course will come out, I suggest you subscribe to the YouTube channel and activate the notification bell!