none
3 Dimensional Array RRS feed

  • Question

  • When it comes to a need for multi-dimensional arrays I think most of us would be satisfied with just 2 or 3 dimensions.  An example would be that you wanted to create a file for a spreadsheet.  For that generally just 2 dimensions is all you need for putting data in columns and rows. 

    Here's a code snippet that creates what is effectively a 3 dimensional array.  The key to how it works has to do with the for loops and the step increments.  This snippet is setup to do a 3X3X3 array.  So, to try it out enter the 27 values.  Notice that if I opened up the to values in the for loops I could actually enter in: InfiniteX99X99 values which far exceeds what I would ever need.  Another nice thing about using this method is there is only one array name.  So, you can still use array.getitemcount to find out how many items are in the array.

     For x = 1 To 3
      For y=.01 To .03 Step .01
        For z=.0001 To .0003 Step .0001
          TextWindow.Write("Enter Value: ")
          Data=TextWindow.Read()
          Array.SetValue("MyArray",x+y+z,Data)
        endfor
      endfor
    EndFor
    TextWindow.WriteLine(Array.GetItemCount("MyArray"))
    For x = 1 To 3
      For y=.01 To .03 Step .01
        For z=.0001 To .0003 Step .0001
          Data=Array.GetValue("MyArray",x+y+z)
          TextWindow.WriteLine(Data)
        endfor
      endfor
    EndFor

    To import the snippet:  LTT372

    Tuesday, May 5, 2009 1:12 AM
    Answerer

Answers

  • The way you handled the step to keep indexes of the array separate is clever, but a little confusing.

    There is a simple change that would let you keep the Steps for the For-Loops one, and still get the results you want.

    Array.SetValue("MyArray",x+","+y+","+z,Data)
    Data=Array.GetValue("MyArray",x+","+y+","+z)

    I republished the change at: LTT372-0


    The more I work with arrays in SmallBasic and the more experience I get teaching my Introduction to Programming class, the more I'm convinced that the current array structure is too confusing to beginners.

    Here is my big suggestion for the next release of small basic: Rename the current array structure to something like Map. Then develop a wrapper for Map with a little "Syntactical Sugar" to make it function more like a standard array. myArray = Array.new(), myArray.set(index,value), value = myArray.get(index).

    That may be a little too Object Oriented for SmallBasic, but the current structure is too far from what arrays are thought of in other languages to be a good teaching tool. I am of the opinion that a wrapper is needed that makes the grid/index structure of an array more obvious.

    Tuesday, May 5, 2009 1:40 PM
    Answerer

All replies

  • The way you handled the step to keep indexes of the array separate is clever, but a little confusing.

    There is a simple change that would let you keep the Steps for the For-Loops one, and still get the results you want.

    Array.SetValue("MyArray",x+","+y+","+z,Data)
    Data=Array.GetValue("MyArray",x+","+y+","+z)

    I republished the change at: LTT372-0


    The more I work with arrays in SmallBasic and the more experience I get teaching my Introduction to Programming class, the more I'm convinced that the current array structure is too confusing to beginners.

    Here is my big suggestion for the next release of small basic: Rename the current array structure to something like Map. Then develop a wrapper for Map with a little "Syntactical Sugar" to make it function more like a standard array. myArray = Array.new(), myArray.set(index,value), value = myArray.get(index).

    That may be a little too Object Oriented for SmallBasic, but the current structure is too far from what arrays are thought of in other languages to be a good teaching tool. I am of the opinion that a wrapper is needed that makes the grid/index structure of an array more obvious.

    Tuesday, May 5, 2009 1:40 PM
    Answerer
  • Coding Cat,

    Great suggestion on a better way to setup the index values!  So, now we have a simple way of emulating multi-dimensional arrays.  Maybe, the next release will have true array capability.  In some ways though, at least the current array structure has some flexibility that makes it workable.

    JR  
    Tuesday, May 5, 2009 5:13 PM
    Answerer
  • Great idea, but I just couldn't let something go without remark. Please do not use floating point values as a loop counter. Rounding errors can lead to loop over or underruns which could be extremely hard to find as to the human eye everything is logically correct. I believe Visual Basic takes care of this for you by making an allowance for inaccuracies, but I don't know what SB would do.
    It is always better to prefer integers over floating point variables, as they are smaller, faster and more predictable. It may not matter in the SB environment, but in my mind it is far more important to restrict the inappropriate use of floating point than any concern over the use of Goto.

    Tuesday, May 5, 2009 8:24 PM
  • Stendec,

    By incorporating Coding Cat's suggestion the index value becomes a string with the x,y and z values being separated by commas.  This works great and is an easier way to do the setup.  Using his suggestion the loop counters can be set to their normal values (1,2,3, etc).  Also, by building the index as a string this would eliminate the possible floating point issue that you suggest.  I was just wanting to show a way of emulating mutiple dimension arrays.  I think through our comments we have accomplished that.

    Thanks,

    JR
    Tuesday, May 5, 2009 10:40 PM
    Answerer
  • Yes, I understand that there is no problem with the final version for the reasons you state. I just wanted to urge caution over using floating point variables as loop counters. For instance, if you wished to generate the numbers 0.1 , 0.2 ... up to 1.0 inclusive, instead of writing

    For i = 0.1 to 1 Step 0.1
      'Use i here
      TextWindow.WriteLine(i)
    Endfor

     'it is safer to use

    For i = 1 To 10
      num = i / 10
      'Use num here
      TextWindow.WriteLine(num)
    Endfor

    Interestingly, if you run the above code snippet, the final value printed for the first loop is 1.0 while for the second it is just 1.
    Perhaps Vijaye could put my mind at rest over this one.

    Wednesday, May 6, 2009 6:26 PM
  • Stendec,

    You're absolutely right.  Dealing with floating point numbers is tricky and equality comparisons have to be made carefully.

    In the examples, you have above, the reason for the weird behavior is the result of using the System.Decimal datatype for storing numbers internally.  I believe (I don't claim to fully understand this) there is logic that takes two different code paths in each of these cases.

    The same problem can be repro'ed by using the "decimal" type in C# (or VB).

            static void Main(string[] args)
            {
                for (Decimal d = 0.1M; d <= 1; d += 0.1M)
                {
                    Console.WriteLine(d);
                }

                for (Decimal d = 1; d <= 10; d += 1)
                {
                    Console.WriteLine((Decimal)d / 10);
                }
            }


    Wednesday, May 6, 2009 7:51 PM
    Moderator