Initialization Matrix Array on BBC SDL

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
User avatar
zachnoland
Posts: 17
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Initialization Matrix Array on BBC SDL

Post by zachnoland »

I know that in BBC BASIC arrays can be treated like single or double arrays or even triple arrays, this triple array is something unique from most programming languages. Okay, let's get straight to the topic of discussion.

If you use a single array, you can initialize it like this and declare it directly like this:

Code: Select all

10 DIM A%(2)
20 A%() = 1, 2, 3
But what about Matrix or double array?
As far as I know it has to be initialized manually

Code: Select all

10 DIM A(2, 2)
20 A(0, 0) = 1 : A(0, 1) = 2 : A(0, 2) = 3
30 REM ETC....
This is quite tiring, There is actually another way using for loops, but it is not so flexible(if the value in the array matches what i want).

when compared to modern programming languages.
C++ Example:

Code: Select all

int number[2][2] = {{1,2, 3}, {1, 2, 3}, {1, 2, 3}};
I know this is a bit illogical compared to modern programming languages.
zachnoland wrote: Thu 08 May 2025, 06:28

Code: Select all

20 A%() = 1, 2, 3
But is there a way to create a double array the same way as a single array?
Richard Russell
Posts: 366
Joined: Tue 18 Jun 2024, 09:32

Re: Initialization Matrix Array on BBC SDL

Post by Richard Russell »

zachnoland wrote: Thu 08 May 2025, 06:28 But is there a way to create a double array the same way as a single array?
There are two straightforward ways of initialising a 2-dimensional array, that I am aware of.

The first way:

Code: Select all

      DIM A(2,2)
      A() = 1,2,3,4,5,6,7,8,9
The second way:

Code: Select all

      DIM A(2,2)
      A(0, 0 TO 2) = 1,2,3
      A(1, 0 TO 2) = 4,5,6
      A(2, 0 TO 2) = 7,8,9
The first is easier, but doesn't make it obvious which initial value goes into which element (you have to know that BBC BASIC's arrays are row-major). The second is more long-winded, and relies on the array-slicing extension, but makes it obvious which value goes where.

Because you can omit the final index, the second example can also be written:

Code: Select all

      DIM A(2,2)
      A(0, 0 TO) = 1,2,3
      A(1, 0 TO) = 4,5,6
      A(2, 0 TO) = 7,8,9
User avatar
zachnoland
Posts: 17
Joined: Sat 07 Dec 2024, 15:22
Location: somewhere in Southeast Asia

Re: Initialization Matrix Array on BBC SDL

Post by zachnoland »

Richard Russell wrote: Thu 08 May 2025, 12:55 The second way:

Code: Select all

      DIM A(2,2)
      A(0, 0 TO 2) = 1,2,3
      A(1, 0 TO 2) = 4,5,6
      A(2, 0 TO 2) = 7,8,9
the second example can also be written:

Code: Select all

      DIM A(2,2)
      A(0, 0 TO) = 1,2,3
      A(1, 0 TO) = 4,5,6
      A(2, 0 TO) = 7,8,9
I think both of these methods are quite helpful, the code is also quite easy to understand :D.