Simple Basic program

CPC and PCW technology
Post Reply
User avatar
PanzerGeneral
Posts: 37
Joined: Wed Oct 14, 2020 7:10 am

Simple Basic program

Post by PanzerGeneral »

I wanted to show my wife that the object-oriented programming task she got is so trivial that she can be done on the old Amstrad in Basic. The idea is to create a two-dimensional array, populate it with random numbers, and then write it to the screen. Then when I started to drill down, it turned out that I can't write it in Basic. You forgot how it works. But maybe one of you has Locomotive Basic and its commands mastered enough to be able to suggest something like that?
User avatar
Laudguy
Posts: 27
Joined: Fri Sep 24, 2021 12:52 pm
Location: just chilling

Simple Basic program

Post by Laudguy »


If this table is to be larger than 10 by 10, then its size must first be declared with the DIM command, e.g .:
DIM array (100,200) Otherwise, referencing an object in a larger array generates an error.

We draw the numbers with the RND function, multiplying it by the maximum value we want to be randomized. The result will be a fractional number between zero and this value. e.g. x = RND * 100
If we only want integers, add% to the end of the variable name or earlier declare that variables starting with a letter would be an integer with the DEFINT b, t-z command
In the example above, all variables starting with b or with letters from t to z will be integers unless we add them! for fractions or $ for text variables.

So: the array (x, y) = RND * 200 is put into a double FOR NEXT loop with the variables x and y as parameters that grow in them and we already have the numbers drawn in the array. And this is the same in every Basic. :)

To display them in even columns, it is enough to add a comma after the name of the variable / array in PRINT, and to change the width of spaces, use the ZONE command with the number of characters. (the default is 8).
The MODE 2 command will increase the number of characters available per line to 80.
You can also complicate this display with the LOCATE command to move the cursor to the desired place used before PRINT. How someone likes to use more unnecessary variables. Although I think that by appropriately selecting the size of the randomly selected numbers and their number in relation to the available characters on the screen, the comma in PRINT and ZONE will suffice.

Oh, to make the numbers align to the right margin and not the left one in these columns, let's use:
PRINT USING "
Just chillin all day and night
User avatar
Wariorr
Posts: 27
Joined: Fri Sep 24, 2021 12:10 pm
Location: My training hall

Simple Basic program

Post by Wariorr »

"; variable, These hashes are the number of characters we allocate to the number, and the comma after the variable will make the cursor jump to the next column after it is displayed.

In fact, the entire program can be done on one line. I guess that's it. Good luck. :)


PS .: The complications would start if the table was larger than it will fit on the screen and we would like it to be able to be scrolled to see another part of it, also adding frames to make it look like a spreadsheet. But after such a complication, it would be enough to add enter, edit, write and read data and some formulas for sum or average from row or column and we already have the office program in 10 lines. Oki in the 20th if it still displayed 3D bars.
I'm a great warrior of old lore.
Post Reply