Results 1 to 4 of 4

Thread: How do I determine the size of a dynamic array?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    1

    How do I determine the size of a dynamic array?

    I'm trying to find the sum of all the digits that make up the number 2^1000, which you may have guessed is from Project Eula. I think it's good so far except I'm just learning about arrays and dynamic arrays, so how will I know what the size of the index will be so I can specify when the Do loop should stop so I don't get an indexoutofrange exception?
    vb.net Code:
    1. Dim BigNumber As Double = 2 ^ 1000
    2.         Dim DigitArray() As Char = BigNumber.ToString.ToCharArray
    3.         Dim Index As Integer = 0
    4.         Dim SumOfDigits As Integer = 0
    5.  
    6.         Do Until index
    7.             SumOfDigits += Integer.Parse(DigitArray(Index))
    8.             Index += 1
    9.         Loop
    10.  
    11.         TextBox1.Text = SumOfDigits.ToString

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: How do I determine the size of a dynamic array?

    Something you maybe have missed in your course then... For Each.

    And the answer to your actual question is here.

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How do I determine the size of a dynamic array?

    DigitArray.Length - will return the length of array (mind the fact that all arrays are zero based, so the last element's index in it will be .Length - 1).

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: How do I determine the size of a dynamic array?

    Note that your code will never work. While the Double has enough capacity to store the number 2^1000, it is not precise enough to store the exact amount. It can only tell you it is something like 1.071508 x 10^301. This means the number it stores is actually 1071508000000000000 (lots of 0's) 00000, which is not exactly 2^1000. Your code will tell you the sum of the digits is something in the order or 50 I guess, (assuming you handle the exception when your loop reaches the 'E' which it cannot parse into an integer) because you will simply be adding 0 + 0 + 0 + 0 + 0...

    There is no numeric integral type in .NET that can precisely store 2^1000. Maybe the BigInteger structure however will work for this goal (but it might be really slow). The BigInteger should theoretically be able to handle numbers of any size.


    EDT
    I just read that the ToString of the BigInteger does basically the same thing; it only shows the first 50 digits and replaces the rest with zeroes. To overcome this issue you need to specify the 'R' formatting in the ToString method. Read the comments on the bottom of the documentation here:
    http://msdn.microsoft.com/en-us/libr...iginteger.aspx

    and especially the documentation of the ToString method:
    http://msdn.microsoft.com/en-us/library/dd268260.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width