Android Question Array Algorithm Help [SOLVED]

Discussion in 'Android Questions' started by wonder, Jun 10, 2015.

  1. wonder

    wonder Expert Licensed 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.
    [​IMG]
    Many thanks in advance for your help. :)
     
  2. walterf25

    walterf25 Well-Known Member Licensed User

    Tricky, let me think about this for a moment............
     
    wonder likes this.
  3. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Use another living_index to point to the last updated record in thw array and increment it only when the cell is alive.
     
    wonder likes this.
  4. imbault

    imbault Well-Known Member Licensed User

    Code:
    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: Jun 10, 2015
    thedesolatesoul and wonder like this.
  5. wonder

    wonder Expert Licensed User

    I've just figured it out in QBasic!! :D My code is almost identical to yours! Never the less, thank you so much!!! :)
    Code:
    'MAIN CYCLE DO
        DIM j = 0 as int
        
    FOR i = 0 TO 16
            Living_Cells(i) = -
    1    'this value represents "null"
            Cell_Status(i) = (02'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! :)
     
  6. sorex

    sorex Expert Licensed 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

    Code:
    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
     
    wonder likes this.
  7. wonder

    wonder Expert Licensed User

    Thank you so much! I will also try your solution, Sorex! :)
     
  8. sorex

    sorex Expert Licensed User

    oh it's multi dimensional, didn't see that
     
    wonder likes this.
  9. sorex

    sorex Expert Licensed User

    you could use this instead

    Code:
    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
     
    wonder likes this.
  10. wonder

    wonder Expert Licensed User

    I'll implement the best solution onto my project, as soon as I get home.
    All I have here now, is QBasic for quick prototyping. :D

    Thank you once again! :)
     
  11. walterf25

    walterf25 Well-Known Member Licensed User

    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.
     
  12. wonder

    wonder Expert Licensed User

    QBasic (QB64) is great tool for brainstorming ideas and new algorithms without much effort. :)
     
  13. sorex

    sorex Expert Licensed User

    why don't you do it in B4A from the first time?
     
  14. wonder

    wonder Expert Licensed 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
    Code:
    CLS
    INPUT 
    "What's your name?", n$
    PRINT 
    "Hello, "; n$ ;". Have a nice day!"
    than creating a new project, a label and a textbox... :)
     
  15. sorex

    sorex Expert Licensed 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 **
     
    wonder likes this.
Loading...