October 25, 2021, 11:58:21

Author Topic: Two for one, NULL and setting all array elements to a default  (Read 975 times)

Offline twgonder

  • Jr. Member
  • **
  • Posts: 29
Some BASICs I've used in the past differentiate between NULL and "" (empty string).
for example a = null: print empty(a) [displays 1]
while a = "": print empty(a) [displays 0]
How does small BASIC handle these?

I'm used to error messages that say "Variable not assigned". Does smallBASIC allow for this type of situation rather than just assigning 0 to any variable that isn't explicitly assigned.

Also, some BASICs have a way to set all elements at once, without the need for loops
for example:
Dim a(100,100,100)
Mat a = NULL (or some other actual value)

I can't find such a command or syntax in the online reference manual. If it exists, it should be part of the sample program for  DIM

Not having such a command means I have to write 3 nested for...next loops to set all the array elements to a default.

Offline Aurel

  • Sr. Member
  • ****
  • Posts: 353
Re: Two for one, NULL and setting all array elements to a default
« Reply #1 on: July 05, 2021, 19:24:15 »
Quote
Also, some BASICs have a way to set all elements at once

what?
which basic can do that ..and even most programming languages cannot set each element to specific value
without using loop.

what you post is ..you declare 3 dimensional array with sizes 100
to set all element of 3 array to null you need only one loop not 3 loops

for i = 0 to 100
    a(i,i,i) = 0
next i

i guess that this work in SB
(Y)

Online Midimaster

  • Sr. Member
  • ****
  • Posts: 364
    • Midimaster Music Education Software
Re: Two for one, NULL and setting all array elements to a default
« Reply #2 on: July 06, 2021, 00:41:47 »
....to set all element of 3 array to null you need only one loop not 3 loops

for i = 0 to 100
    a(i,i,i) = 0
next i

This cannot be correct. A 3-dimensional array of size 100 has 100*100*100 = 1.000.000 elements. You cannot fill them with only 100 loops!

Your solution would only fill the 100 elements with "3 times the same order number" like:
[0,0,0] and [1,1,1] and [2,2,2] and...

but not the other 999.899 like [0,0,1] or [0,0,2 ] or ... or [1,0,0] or [37,16,4]...



Is has to be like this:
Code: [Select]
For i=0 to 99
   For j=0 to 99
     For k=0 to 99
         a(i,j,k) = NULL
      Next
   Next
Next

But also interations like this are done below 1msec on modern computers. So no performance problem.
« Last Edit: July 06, 2021, 00:50:39 by Midimaster »
See my current project on PlayStore: 20Tracks-Audio-Player https://play.google.com/store/apps/details?id=midimaster.twentytrackd

Offline Aurel

  • Sr. Member
  • ****
  • Posts: 353
Re: Two for one, NULL and setting all array elements to a default
« Reply #3 on: July 06, 2021, 08:00:26 »
Quote
This cannot be correct. A 3-dimensional array of size 100 has 100*100*100 = 1.000.000 elements. You cannot fill them with only 100 loops!

In your awkward brain 3 dim array which is declared with 3 row have only 300 elements
a.1 100
a.2 100
a.3 100

and not multiplay elements ,where you learn programming ?
(Y)

Online Midimaster

  • Sr. Member
  • ****
  • Posts: 364
    • Midimaster Music Education Software
Re: Two for one, NULL and setting all array elements to a default
« Reply #4 on: July 06, 2021, 09:51:45 »
...In your awkward brain 3 dim array which is declared with 3 row have only 300 elements
and not multiplay elements ,where you learn programming ?

This is completely different from BlitzMax, where a 3-dim-Array gives dim*dim*dim elements
Code: BlitzMax
  1. Global a:Int[100,100,100]
  2. Print a.length
returns:
Code: [Select]
1000000
Also in C or BlitzBasic the number of elements is always  the product of the dimension sizes:

Example in C
Code: BlitzBasic
  1. Dim a$(3,4,5)
  2.  
gives 3*4*5 = 60 Elements

Could you please re-think your statement and your unfriendly speak.


by the way: I learned BASC on one of the first PET. Then GFA-Basic on Atari, then Visual-Basic on Windows. Later BlitzMax and now often the new Android-Basic B4A . None of them had arrays like you describe them.


« Last Edit: July 06, 2021, 10:25:18 by Midimaster »
See my current project on PlayStore: 20Tracks-Audio-Player https://play.google.com/store/apps/details?id=midimaster.twentytrackd

Online iWasAdam

  • Hero Member
  • *****
  • Posts: 2483
Re: Two for one, NULL and setting all array elements to a default
« Reply #5 on: July 06, 2021, 10:50:51 »
yup - midimaster is utterly correct

dim [10] = 10 elements
dim [10,10] = 10x10 = 100 elements
dim [10,10,10] = 10x10x10 = 1000 elements

check out google or any programming book to learn what an array is
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

remember - just because YOU think it's right - doesn't make it so
« Last Edit: July 06, 2021, 10:53:09 by iWasAdam »

Offline bplus

  • Full Member
  • ***
  • Posts: 229
Re: Two for one, NULL and setting all array elements to a default
« Reply #6 on: July 06, 2021, 11:18:30 »
Quote
This cannot be correct. A 3-dimensional array of size 100 has 100*100*100 = 1.000.000 elements. You cannot fill them with only 100 loops!

In your awkward brain 3 dim array which is declared with 3 row have only 300 elements
a.1 100
a.2 100
a.3 100

and not multiplay elements ,where you learn programming ?

@Aurel they are correct.
Say you want to assign a color for 3D point. You have to go:
for x = 1 to xmax
 for y = 1 to ymax
  for z = 1 to zmax
   myColor(x,y,z) =rgb_formula(x, y, z)
  next
 next
next

if all max's are same for a cube say with side 100 that is 100^3 elements.
« Last Edit: July 06, 2021, 11:50:18 by bplus »

Offline bplus

  • Full Member
  • ***
  • Posts: 229
Re: Two for one, NULL and setting all array elements to a default
« Reply #7 on: July 06, 2021, 11:26:32 »
@twgonder

To my knowledge of sb, you can not do multiple assignments without loop but can depend that everything initialzed at 0 or "" at start, but be careful and warned sb has variable typing so can be confused whether something is number or string like try to input digits you want treated like a string not numbers.


Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2788
  • I mostly code at night... Mostly.
    • SyntaxBomb - Indie Coders
Re: Two for one, NULL and setting all array elements to a default
« Reply #8 on: July 06, 2021, 12:59:13 »
In your awkward brain 3 dim array which is declared with 3 row have only 300 elements..... where you learn programming ?
No need to insult or have digs at others. Even if Midimaster was wrong, which he isn't, then this kind of response is silly. Any further violations will result in a 30 day ban from SyntaxBomb.

Midimaster was correct and you are incorrect.

Dim[ 100, 100, 100 ] has 1,000,000 elements or 1,030,301 elements depending if Dim[ 100 ] goes from 0 to 99 or 0 to 100.
Mac mini ( 2018 / 2020 ), 3 GHz 6-Core Intel Core i5, 16 GB 2667 MHz DDR4, 1TB NVMe, eGPU Radeon Pro 580 8 GB, LG Ultragear 27GL83A-B 27 Inch
Mac mini (2020 ), M1 SoC, 8 GB LPDDR4, 512GB NVMe 
Commodore VIC-20, 1.1Mhz MOS 6502 CPU, 5KB RAM, VIC ( 6560 ) GPU

Until the next time.

Offline Aurel

  • Sr. Member
  • ****
  • Posts: 353
Re: Two for one, NULL and setting all array elements to a default
« Reply #9 on: July 06, 2021, 14:57:44 »
wait a moment
in a compiler which i use more than decade it is like i said
number of elements is not multiplied
so again 3 rows where each row or each list contain 100 elements
is 300 elements .
He jump from BlitzMax to BlizPlus then explained it wrong .
so..there is case where is not .
(Y)

Offline Aurel

  • Sr. Member
  • ****
  • Posts: 353
Re: Two for one, NULL and setting all array elements to a default
« Reply #10 on: July 06, 2021, 15:01:12 »
@Bplus
RGB formula is this

Function RGB(byval red as INT,byval green as INT,byval blue as INT) as INT
  INT color
  color = red
  color = color + green*256
  color = color + blue*65536
  Return color
End Function

and as you can see values are multiplied
(Y)

Offline twgonder

  • Jr. Member
  • **
  • Posts: 29
Re: Two for one, NULL and setting all array elements to a default
« Reply #11 on: July 06, 2021, 15:15:37 »
yup - midimaster is utterly correct

dim [10] = 10 elements
dim [10,10] = 10x10 = 100 elements
dim [10,10,10] = 10x10x10 = 1000 elements

check out google or any programming book to learn what an array is
https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

remember - just because YOU think it's right - doesn't make it so

The original example I made had 100 not 10.
So he was correct.

okay, so I wanted to delete this meaningless post, how does one do that? I can't see it as an option.
Also I don't see a "cancel" button to escape from changing a posting.
« Last Edit: July 06, 2021, 15:24:46 by twgonder »

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: Two for one, NULL and setting all array elements to a default
« Reply #12 on: July 06, 2021, 15:26:05 »
In BlitzMax you have
Multidimensional Arrays
and
Array of Arrays.

Maybe the confusion here is about something similar. So [100,100,100] is in the minds of some of you an array consisting of arrays with each 100 elements.


bye
Ron

Offline jsalai

  • Jr. Member
  • **
  • Posts: 41
Re: Two for one, NULL and setting all array elements to a default
« Reply #13 on: July 06, 2021, 19:11:49 »
In BlitzMax you have
Multidimensional Arrays
and
Array of Arrays.

Maybe the confusion here is about something similar. So [100,100,100] is in the minds of some of you an array consisting of arrays with each 100 elements.


bye
Ron

For SmallBASIC I kindly propose to forget everything about arrays someone ever learned:

eg:
Code: [Select]
a=seq(1,10,10)
b=seq(2,16,8)
c=seq(5,60,12)

h << a
h << "whatever"
h << b
h << "whatever"
h << c
h << "whatever"
h(0) << b
h(0)(3) << b
insert h(0)(3),3,"whatever"
insert h(0),3,a
insert h(0),5,"whatever"
?h
pause
end

one should read something like:

[1,2,3,[1,2,3,4,5,6,7,8,9,10],[[2,4,6,8,10,12,14,16],whatever],whatever,5,6,7,8,
9,10,[2,4,6,8,10,12,14,16]],whatever,[2,4,6,8,10,12,14,16],whatever,[5,10,15,20,25,30,35,40,45,50,55,60],whatever]

nested, valiable length, numbers, strings, whatever.....
I won't belong to any organization that would have me as a member.
[Groucho Marx]

Online Midimaster

  • Sr. Member
  • ****
  • Posts: 364
    • Midimaster Music Education Software
Re: Two for one, NULL and setting all array elements to a default
« Reply #14 on: July 07, 2021, 07:45:26 »
but this looks more like a list not like an array. Are users able to really build usable 3dimensional arrays with this?

The fundamental property of an array is, that the position of the values is fixed. 
See my current project on PlayStore: 20Tracks-Audio-Player https://play.google.com/store/apps/details?id=midimaster.twentytrackd

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal