[solved] need help with stupid code :-)

General FreeBASIC programming questions.
Post Reply
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

[solved] need help with stupid code :-)

Post by D.J.Peters »

imagine I have 16 x 6 integer counters in an array(15,5)

now i increment the first of the 96 counters in a chain of counters

c(0,0)+=1

if it overruns reset it and increment then next counter

Code: Select all

if c(0,0)=6 then 
  c(0,0)=0:c(0,1)+=1 ...
  if c(0,1)=6 then 
    c(0,1)=0:c(0,2)+=1 ...
    if  c(0,2)=6 then ' ...
  end if
end if
I won't write all 96 If THEN blocks

how to replace all the "if then" blocks with macros or loops ?

Thank you

Joshy

Code: Select all

' 16x6=96 counters
dim as integer c(15,5)

print "increment 16x6 = 96 counters in a chain! ..."

do
  #if 0
  cls
  for y as integer = 0 to 15
    for x as integer = 0 to 5
      ' do something with the 16x6 counters
      'doit(c(y , x))
    next  
  next
  #endif
  
  ' increment first counter
  c(0,0)+=1
  ' if it overruns reset it and increment next counter and so one
  if c(0,0)=6 then
    'print "c(0,0) overrun"
    c(0,0)=0:c(0,1)+=1
    if c(0,1)=6 then
      'print "c(0,1) overrun"
      c(0,1)=0:c(0,2)+=1
      if c(0,2)=6 then
        'print "c(0,2) overrun"
        c(0,2)=0:c(0,3)+=1
        if c(0,3)=6 then
          'print "c(0,3) overrun"
          c(0,3)=0:c(0,4)+=1
          if c(0,4)=6 then
            'print "c(0,4) overrun"
            c(0,4)=0:c(0,5)+=1 
            if c(0,5)=6 then
              'print "c(0,5) overrun"
              c(0,5)=0:c(1,0)+=1 
              if c(1,0)=6 then
                'print "c(1,0) overrun"
                c(1,0)=0:c(1,1)+=1
                if c(1,1)=6 then
                  'print "c(1,1) overrun"
                  c(1,1)=0:c(1,2)+=1
                  if c(1,2)=6 then
                    'print "c(1,2) overrun"
                    c(1,2)=0:c(1,3)+=1
                    if c(1,3)=6 then
                      'print "c(1,3) overrun"
                      c(1,3)=0:c(1,4)+=1
                      if c(1,4)=6 then
                        'print "c(1,4) overrun"
                        c(1,4)=0:c(1,5)+=1
                        if c(1,5)=6 then
                          print "c(1,5) overrun"
                          beep
                          c(1,5)=0:c(2,0)+=1
                          ' and so one
                          '...
                          'if c(15,5)=5 then exit do
                          exit do
                        end if  
                      end if  
                    end if  
                  end if  
                end if  
              end if
            end if  
          end if           
        end if           
      end if           
    end if
  end if 
loop
print "done ..."
sleep  
Last edited by D.J.Peters on Jun 14, 2021 2:17, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: need help with stupid code :-)

Post by D.J.Peters »

i got it :-)

Code: Select all

' 16x6=93 counters
dim as integer c(15,5)
print "increment 16x6 = 96 counters in a chain! ..."
dim as integer i,j
do
  #if 0
  for y as integer = 0 to 15
    for x as integer = 0 to 5
      ' do something with the 16x6 counters
      'doit(c(y,x))
    next  
  next
  #endif
  dim as boolean bRun=true
  while bRun
    bRun=false
    c(i,j)+=1
    if c(i,j)=6 then
      c(i,j)=0 ' reset counter
      ' prepare next counter to increment
      j+=1:if j=6 then j=0:i+=1
      if i<16 then 
        bRun=true
      else  
        ' last counter ready
        exit do
      end if
    else
      ' trigger first counter for next loop
      i=0:j=0
    end if  
  wend
loop
print "done ..."
sleep  
adeyblue
Posts: 300
Joined: Nov 07, 2019 20:08

Re: need help with stupid code :-)

Post by adeyblue »

Isn't that just a long way of saying

Code: Select all

For i As Long = 0 To 15
   For j As Long = 0 To 5
       dim temp As Integer = array(i, j) + 1
       if temp = 6 Then ' loops continue
           array(i, j) = 0
       Else ' no more cascades
           array(i, j) = temp
           Exit For,For ' think that's how that works
       End If
    Next
Next
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: [solved] need help with stupid code :-)

Post by D.J.Peters »

@adeyblue yes this would do it also :-)

Joshy
Post Reply