locked
array - can't access first array data after filling second array RRS feed

  • Question

  • I have three subroutines. Each sub fills an array with floating point numbers after some calcs.
    In sub #3, I need the elements from sub #1 (array #1) to do calcs and fill array #3.
    But, when I access array #1 I instead get the elements from array #2 (the last array accessed by my program)

    I've initialized the array #1 again at the beginning of sub #3 and made sure I have no syntax errors
    in addressing the array I want to use.

    This seems to be an initialization problem but I can't figure it out.

    If all else fails, I could use a single array and simply offset into it the necessary number of elements for each array
    as they are all the same length. But that's harder for me to follow than 3-seperate arrays.

    Anyone have a suggestion?

    thanks,

    moorem34
    Wednesday, April 1, 2009 12:49 AM

Answers

  • Arrays use names to keep track of values.  In your case, all of your array names are initialized to "0" making them point to the same array.

    Try initializing your array names to something unique, instead of 0.

    For example:
      yc = "yc"  
      ytu = "ytu"
      theta = "theta"

    Or alternatively you can forgo the variable completely, and use
      Array.SetValue("theta", a, i)

    Let us know if this solution worked.

    • Marked as answer by moorem34 Wednesday, April 1, 2009 6:03 PM
    Wednesday, April 1, 2009 3:47 PM

All replies

  • Could you please post some code that might help us track down the problem?
    Wednesday, April 1, 2009 1:15 AM
  • Vijaye,

    Here's the code. Thank you for the help.
    BTW...If you use the example (eg) numbers at the prompts in the opening text window,
    I print out what the array element SHOULD be in sub calc_theta for comparison against what actually is happening.
    i.e. enter the numeral 2, then 4, then 12 and finally 10 in that order at the opening prompts.

    GraphicsWindow.Width=640
    GraphicsWindow.Height=640
    GraphicsWindow.BackgroundColor=blue
    GraphicsWindow.PenColor=white
    GraphicsWindow.Hide()

    TextWindow.top=300
    TextWindow.Left=100
    TextWindow.title="ctrl C to exit"
    TextWindow.Show()

    TextWindow.WriteLine("All equations are normalized for a chord length of 1")
    chord=1
    TextWindow.WriteLine(" ")

    TextWindow.WriteLine("Enter the the maximum camber (m) in percentage of the chord eg 2")
    maxcam=TextWindow.ReadNumber()

    TextWindow.WriteLine("Enter the position of the maximum camber (p) in tens of percent of chord eg 4")
    maxcampos=TextWindow.ReadNumber()


    TextWindow.WriteLine("Enter the maximum thickness (t) of the airfoil in percentage of chord eg 12")
    thick=TextWindow.ReadNumber()

    TextWindow.WriteLine("Enter the number of points to plot eg 10")
    npts=TextWindow.ReadNumber()

    c=chord
    m=c*(maxcam/100)
    p=c*10*(maxcampos/100)
    t=c*(thick/100)

    TextWindow.Hide()
    GraphicsWindow.Show()


    GraphicsWindow.DrawText(0,60,"Working on airfoil NACA ")
    GraphicsWindow.DrawText(150,60,maxcam)
    GraphicsWindow.DrawText(158,60,maxcampos)
    GraphicsWindow.DrawText(166,60,thick)
    GraphicsWindow.DrawText(185,60,"with a chord length of "+chord)
    GraphicsWindow.DrawText(0,80,"number of points to plot are "+npts)
    Program.Delay(2000)

    GraphicsWindow.hide()
    TextWindow.Show()
    TextWindow.title="ctrl C to exit"


    calc_yc()
    calc_yt()
    calc_theta()
    Program.Delay(1000000)
       
    Program.End()

    Sub calc_yc

    i=0
    yc=0

    'i = x in the formulas
    For i = 0 To c Step c/npts
     
        If i <= p Then
          value = (m/(p*p))*((2*p*i)-(i*i))
          Array.SetValue(yc, i, value)
          EndIf
         
          If i > p Then
            value = (m/((1-p)*(1-p)))*((1-2*P)+(2*p*i)-(i*i))
            Array.SetValue(yc,i,value)
            EndIf
                  
            TextWindow.Show()
            TextWindow.WriteLine("yc"+i+"is "+Array.GetValue(yc,i))
            Program.Delay(000)
             
            EndFor 
            ' Program.Delay(100000)
            ' Program.End()
    EndSub       
           
    Sub calc_yt
          'calc yt ABOVE the mean chord line and put in Array ytU
          i=0
          ytU=0
         

    For i = 0 To c Step c/npts
            xsqrt=math.SquareRoot(i)
            xsqrd=Math.Power(i,2)
            xcub=math.Power(i,3)
            xquad =Math.Power(i,4)
           
           
             Uvalue= (t/0.2)*(0.2969*xsqrt-0.126*i-0.3516*xsqrd+0.2843*xcub-0.1015*xquad)
             Array.SetValue(ytU, i, Uvalue)
           
           
            TextWindow.WriteLine(" "+"ytU("+i+")= "+Array.GetValue(ytU,i))
           
          EndFor 
               'Program.Delay(100000)
          'Program.End()
         
           'calc yt BELOW the mean chord line and put in Array ytL
         i=0
          ytL=0
     
        For i = 0 To c Step c/npts
          xsqrt=math.SquareRoot(i)
            xsqrd=Math.Power(i,2)
            xcub=math.Power(i,3)
            xquad =Math.Power(i,4)
           
           
             Lvalue = (-1)*(t/0.2)*(0.2969*xsqrt-0.126*i-0.3516*xsqrd+0.2843*xcub-0.1015*xquad)
             Array.SetValue(ytL, i,Lvalue)
           
             TextWindow.WriteLine(" "+"ytL("+i+")= "+Array.GetValue(ytL,i))
          
       
      EndFor
           'Program.Delay(100000)
           'Program.End()
    endsub
        
     
     
     Sub calc_theta
    'Calc the angle theta in radians and put in Array theta
    tantest =0
    halfpi = Math.Pi/2
    oldy = 0
    theta = 0
    yc = 0
    a=0
    y=0
    ytL = 0


    For a = 0 To c Step c/npts
      y = Array.GetValue(yc,a)
      dy = y - oldy
      dx = c/npts
      rads=dy/dx
      TextWindow.WriteLine("rads"+a+"= "+rads)
      If a = .1 Then
            TextWindow.WriteLine(" ")
            TextWindow.WriteLine("yc"+a+"= "+y)
            TextWindow.WriteLine("For a 2412 airfoil with 10 points yc.1 should be 0.00875")
            TextWindow.WriteLine("and rads.1 should be 0.0875")
            TextWindow.WriteLine("HOWEVER, the values being read in from array yc are actually coming from")
            TextWindow.WriteLine("array ytL which is the last array I filled in sub calc_yt")
             TextWindow.WriteLine(" ")
           EndIf
     
          If rads > 0 then
            m = 0
            n = halfpi
            z = .00628
         
     
       
                  For i = m To n Step z

                      tantest  = Math.Tan(i)
             
                      If tantest < rads Then
                      EndIf
             
                      If tantest > rads Then
                          Goto jump
                      EndIf
                      
                  EndFor
      
               jump:
              
                While stop <> 1
                 i = i -.00000314
                  tantest2 = Math.Tan(i)
                  
                       If tantest2 > rads Then
                       EndIf
                
                       If tantest2 < rads Then
                         Goto jump2 
                       EndIf
                              
                
                EndWhile
     
                jump2:
               
                'TextWindow.WriteLine("BINGO...angle we're looking for is: "+i+" radians")
                Array.SetValue(theta,a,i)
                TextWindow.WriteLine("theta "+a+"  "+array.GetValue(theta,a))
                'Program.Delay(1000)
          EndIf  'If rads > 0 then
               
               
          If rads < 0 then
            m =0
            n = -1*halfpi
            z = -.00628
           
                  For i = m To n Step z

                          tantest  = Math.Tan(i)
                 
                          If tantest > rads Then
                          EndIf
                 
                          If tantest < rads Then
                             Goto jump3
                          EndIf
                         
                  EndFor
           
                    jump3:
              
                      While stop <> 1
                       i = i +.00000314
                        tantest2 = Math.Tan(i)
                        
                         If tantest2 < rads Then
                         'TextWindow.WriteLine("tantest2 is LESS THAN rads:  tantest = "+tantest2+" rads is "+rads)
                         EndIf
                      
                         If tantest2 > rads Then
                         'TextWindow.WriteLine("tantest2 is greater than rads:  tantest2 = "+tantest2+" rads is "+rads)
                         Goto jump4 
                         EndIf
                       'TextWindow.WriteLine(" ")
                       'TextWindow.WriteLine(" ")
                       
                      
                      EndWhile
           
                    jump4:
           
                'TextWindow.WriteLine("BINGO...angle we're looking for is: "+i+" radians")
                Array.SetValue(theta,a,i)
                TextWindow.WriteLine("theta "+a+"  "+array.GetValue(theta,a))
                'Program.Delay(5000)
          EndIf  'If rads < 0 then
     
     oldy = y

    EndFor 
    EndSub

    Wednesday, April 1, 2009 11:39 AM
  • Arrays use names to keep track of values.  In your case, all of your array names are initialized to "0" making them point to the same array.

    Try initializing your array names to something unique, instead of 0.

    For example:
      yc = "yc"  
      ytu = "ytu"
      theta = "theta"

    Or alternatively you can forgo the variable completely, and use
      Array.SetValue("theta", a, i)

    Let us know if this solution worked.

    • Marked as answer by moorem34 Wednesday, April 1, 2009 6:03 PM
    Wednesday, April 1, 2009 3:47 PM
  • Vijaye Raji,

    Your suggestion worked perfectly.

    I understand now about initializing arrays using unique names.

    Much appreciated!

    moorem34
    Wednesday, April 1, 2009 6:02 PM