Android Question Array Algorithm Help [SOLVED]

wonder

Expert
Licensed User
Longtime User
Hi guys,

This should be really simple, but for some reason I'm having a brain freeze today and I can't come-up with the right algorithm.

I have 17 living cells, which will die over time. The main cycle repeats until all cells are dead.
In this main cycle I have two arrays Cell_Status(17) and Living_Cells(17) as seen in the example below.

What's the fastest way to update the second array without creating (if possible) anymore (FOR / DO) cycles, than the one already present?

Please note that for my specific case, this cannot be done with LISTS or MYSQL.
Untitled.png

Many thanks in advance for your help. :)
 

walterf25

Expert
Licensed User
Longtime User
Hi guys,

This should be really simple, but for some reason I'm having a brain freeze today and I can't come-up with the right algorithm.

I have 17 living cells, which will die over time. The main cycle repeats until all cells are dead.
In this main cycle I have two arrays Cell_Status(17) and Living_Cells(17) as seen in the example below.

What's the fastest way to update the second array without creating (if possible) anymore (FOR / DO) cycles, than the one already present?

Please note that for my specific case, this cannot be done with LISTS or MYSQL.
Untitled.png

Many thanks in advance for your help. :)
Tricky, let me think about this for a moment............
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Hi guys,

This should be really simple, but for some reason I'm having a brain freeze today and I can't come-up with the right algorithm.

I have 17 living cells, which will die over time. The main cycle repeats until all cells are dead.
In this main cycle I have two arrays Cell_Status(17) and Living_Cells(17) as seen in the example below.

What's the fastest way to update the second array without creating (if possible) anymore (FOR / DO) cycles, than the one already present?

Please note that for my specific case, this cannot be done with LISTS or MYSQL.
Untitled.png

Many thanks in advance for your help. :)

B4X:
Dim j As Int = 0
For i = 0 To cell_status.lenght -1
    If cell_status(i)="Alive" Then
        Living_Cells(j)=i
        j = j + 1
    End If
Next
For i= j  To Living_Cells.lenght -1
    Living_Cells(j)= ""
Next
 
Last edited:
Upvote 0

wonder

Expert
Licensed User
Longtime User
B4X:
Dim j As Int 0
For i = 0 To cell_status.lenght -1
    If cell_status(i)="Alive" Then
        Living_Cells(j)=i
        j = j + 1
    End If
Next
For i= j  To Living_Cells.lenght -1
    Living_Cells(j)= ""
Next
I've just figured it out in QBasic!! :D My code is almost identical to yours! Never the less, thank you so much!!! :)
B4X:
'MAIN CYCLE DO
    DIM j = 0 as int
    FOR i = 0 TO 16
        Living_Cells(i) = -1    'this value represents "null"
        Cell_Status(i) = (0, 2) '0 is dead, 1 is alive
        IF Cell_Status(i) = 1 THEN
            Living_Cells(j) = i
            j = j + 1
        END IF
    NEXT
'MAIN CYCLE LOOP

You guys, rock! I love this forum! :)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
why not work with just one array with inplace swapping?

then you just need to loop and exit the for when it hits dead

B4X:
Dim cell_status() As String=Array As String ("alive","dead","alive","alive","alive","alive","dead")
Dim c As Int
Dim t As String
For x = 0 To cell_status.length-1
If cell_status(x)="alive" Then
  t=cell_status(x)
  cell_status(x)=cell_status(c)
  cell_status(c)=t
  c=c+1
End If
Next
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
why not work with just one array with inplace swapping?

then you just need to loop and exit the for when it hits dead

B4X:
Dim cell_status() As String=Array As String ("alive","dead","alive","alive","alive","alive","dead")
Dim c As Int
Dim t As String
For x = 0 To cell_status.length-1
If cell_status(x)="alive" Then
  t=cell_status(x)
  cell_status(x)=cell_status(c)
  cell_status(c)=t
  c=c+1
End If
Next
Thank you so much! I will also try your solution, Sorex! :)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you could use this instead

B4X:
Dim cell_status() As String=Array As String ("0,alive","1,dead","2,alive","3,alive","4,alive","5,alive","6,dead")

and use indexOf to see if it's alive or not
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I've just figured it out in QBasic!! :D My code is almost identical to yours! Never the less, thank you so much!!! :)
B4X:
'MAIN CYCLE DO
    DIM j = 0 as int
    FOR i = 0 TO 16
        Living_Cells(i) = -1    'this value represents "null"
        Cell_Status(i) = (0, 2) '0 is dead, 1 is alive
        IF Cell_Status(i) = 1 THEN
            Living_Cells(j) = i
            j = j + 1
        END IF
    NEXT
'MAIN CYCLE LOOP

You guys, rock! I love this forum! :)
QBASIC, wow haven't heard it in a long time, i started teaching myself about programming with QBASIC when i was around 14, time flies.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
I'm at work, I don't have B4A here, only at home.
Also, when I want to test something rather small and simple in an isolated environment, it's faster to write
B4X:
CLS
INPUT "What's your name?", n$
PRINT "Hello, "; n$ ;". Have a nice day!"
than creating a new project, a label and a textbox... :)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I tried my theory and it also worked (with 2 parameter strings and indexof check)

** Activity (main) Pause, UserClosed = true **
** Activity (main) Create, isFirst = true **
2,alive
3,alive
4,alive
6,alive
0,dead
5,dead
1,dead
** Activity (main) Resume **
 
Upvote 0
Top