## Array question [Solved]

General FreeBASIC programming questions.
Provoni
Posts: 393
Joined: Jan 05, 2014 12:33
Location: Belgium

### Array question [Solved]

Hey all,

Suppose that there is a multi-dim array as such: myarray(9,9,9).

To loop through each element:

Code: Select all

for i=0 to 9
for j=0 to 9
for k=0 to 9
'do something
next k
next j
next i

Is there a way to do the following without rewriting the array to a single-dim?

Code: Select all

for i=0 to 999
'do something
next i

Thanks
Last edited by Provoni on Jun 12, 2020 7:26, edited 1 time in total.
Provoni
Posts: 393
Joined: Jan 05, 2014 12:33
Location: Belgium

### Re: Array question

Solved my own problem:

Code: Select all

dim as integer ptr test=@myarray(0,0,0)

for i=0 to 999
*(test+i)=123
next i
fxm
Posts: 10058
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Array question [Solved]

Personally, I prefer the following syntax (with a pointer index), considering that the data to access is into an integer buffer pointed by test:
test[i] = 123
dodicat
Posts: 6765
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Array question [Solved]

rewriting the array to a single-dim

Code: Select all

dim as long myarray(9,9,9)
dim as long i,j,k
for i=0 to 9
for j=0 to 9
for k=0 to 9
myarray(i,j,k)=i*j*k+rnd*6-rnd*6
next k
next j
next i

for i=0 to 9
for j=0 to 9
for k=0 to 9
print myarray(i,j,k);
next k
next j
next i
print

#macro Get_Size(array,d)
d=Ubound(array,0)
For n As long=1 To d
If n=1 Then d=1
d=d*(Ubound(array,n)-Lbound(array,n)+1)
Next
#endmacro

#macro memcopy(dest,src,size)
For n As Long=0 To size-1
(dest)[n]=(src)[n]
Next
#endmacro

dim as long sz

get_Size(myarray,sz)

print
print
dim as long c(1 to sz)
memcopy(@c(1),@myarray(0,0,0),sz)
for n as long=lbound(c) to ubound(c)
print c(n);
next n
print
sleep

There is a new .bi file (array.bi) .
It can handle array pointers and array sizes.
Try it out

Code: Select all

#include "crt.bi"
#include "fbc-int/array.bi"

dim as long myarray(9,9,9)
dim as long i,j,k
for i=0 to 9
for j=0 to 9
for k=0 to 9
myarray(i,j,k)=i*j*k+rnd*6-rnd*6
next k
next j
next i

for i=0 to 9
for j=0 to 9
for k=0 to 9
print myarray(i,j,k);
next k
next j
next i
print

dim as long ptr fbp=fb_ArrayGetDesc(myarray())->base_ptr
dim as long sz=fb_ArrayGetDesc(myarray())->size
dim as long size=fb_ArrayGetDesc(myarray())->element_len
sz\=size 'number of elements
print
print

dim as long c(1 to sz)
dim as long ptr cp=fb_ArrayGetDesc(c())->base_ptr
memcpy(cp,fbp,sz*size)
for n as long=lbound(c) to ubound(c)
print c(n);
next n
print
sleep

fxm
Posts: 10058
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Array question [Solved]

The last code above only works with the fbc1.07 version because for the fbc1.08 version (and future ones), the user interface with fbc-int/array.bi must be modified.
dodicat
Posts: 6765
Joined: Jan 10, 2006 20:30
Location: Scotland

### Re: Array question [Solved]

Thanks fxm.
I only run the latest official build 1.07.
I assume the latest (future) interface is simpler.
srvaldez
Posts: 2578
Joined: Sep 25, 2005 21:54

### Re: Array question [Solved]

try using FBC
fxm
Posts: 10058
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Array question [Solved]

srvaldez wrote:try using FBC
Not only.

For the fbc1.08 version (and future ones):

Code: Select all

#include "crt.bi"
#include "fbc-int/array.bi"

dim as long myarray(9,9,9)
dim as long i,j,k
for i=0 to 9
for j=0 to 9
for k=0 to 9
myarray(i,j,k)=i*j*k+rnd*6-rnd*6
next k
next j
next i

for i=0 to 9
for j=0 to 9
for k=0 to 9
print myarray(i,j,k);
next k
next j
next i
print

dim as long ptr fbp=FBC.ArrayDescriptorPtr(myarray())->base_ptr
dim as long sz=FBC.ArrayDescriptorPtr(myarray())->size
dim as long size=FBC.ArrayDescriptorPtr(myarray())->element_len
sz\=size 'number of elements
print
print

dim as long c(1 to sz)
dim as long ptr cp=FBC.ArrayDescriptorPtr(c())->base_ptr
memcpy(cp,fbp,sz*size)
for n as long=lbound(c) to ubound(c)
print c(n);
next n
print
sleep
Provoni
Posts: 393
Joined: Jan 05, 2014 12:33
Location: Belgium

### Re: Array question [Solved]

fxm wrote:Personally, I prefer the following syntax (with a pointer index), considering that the data to access is into an integer buffer pointed by test:
test[i] = 123

dodicat wrote:rewriting the array to a single-dim

Thanks