Two Dimensional Arrays in VB.NET

I'd really appreciate it if anyone could help me out with two dimensional arrays. I am to enter a high and low temperature for five stations and find the average high and low. I understand that I need a two dimensional array to enter the values such as temp(5, 2) , and that I need to used a dual loop structure to enter the values, but I am having trouble figuring out how to initialize or execute the array. It is just a console application. Thanks for any suggestions.


  • seancampbellseancampbell Pennsylvania, USA
    2 Dimensional Arrays are fairly straight forward

    When you define an array, generally you declare the size immediately. If you do not specify the starting cell value, it starts at 0.

    For instance:
    Dim Arr(10) as String

    Arr has 11 cells. 0,1,2...10

    I could declare
    Dim Arr(1 to 10) as String

    Arr now has 10 cells. 1,2,3...10

    The same thing goes for declaring a double dimensioned array

    Dim Arr(4,1) As Double 'This will be big enough to handle your data
    'We can immediately use the values inside
    Arr(0,0) = 1.1
    Arr(0,1) = 1.2
    Arr(1,0) = 1.3 'Etc...

    For your program, you want to enter a High and Low value 5 times... So you will want to use a loop to collect data, and at the end of the loop do the summing

    Here is an example, that will require you to read the comments and add additional code:
    'I am not sure how you are inputting data through the console
    'So read the comments carefully, they will instruct you on what code to add

    Dim I as Integer = 0
    'We will use this to count the passes of the loop AS WELL AS
    'determine the cell that we want to store data in

    Dim WeatherData(4,1) as Double
    'WeatherData(I,0) = Low Temp
    'WeatherData(I,1) = High Temp

    'Start a Loop here, it will repeat until the "While" condition is met

    Do While I < 5
    Dim lowVal as Double = 0
    Dim highVal as Double = 0
    'Add code to wait for Low Input here, (lowVal = Input)
    WeatherData(I, 0) = lowVal

    'Add code to wait for High Input here, (highVal = Input)
    WeatherData(I, 1) = highVal
    I = I + 1 'This keeps us from getting stuck in an infinite loop

    'Ok, so now we have all of the weather data
    Dim SumLows as Double = 0
    Dim SumHighs as Double = 0
    For I = 0 to 4
    SumLows = SumLows + WeatherData(I, 0)
    SumHighs = SumHighs + WeatherData(I, 0)
    Next I

    'now you have the sum, you can do the average calculation
    'and print the data to the console!!


    Hope this helps,
  • Hi SeanCampbell,

    I guess you meant

    SumHighs = SumHighs + WeatherData(I, 1)

    in your last FOR NEXT loop.


    Dr M
  • Bless you guys... this is the clearest I have seen this concept explained & I have been looking everywhere... I am trying to teach myself VB.Net & arrays have really been giving me fits... I suspect I will need to be comfortable with them before I start trying to learn the database end of the deal, so I have really been putting lots of time in playing with arrays & trying to understand the 'why' behind each part of the process.

  • seancampbellseancampbell Pennsylvania, USA
    Thanks for the Thanks Ruth.

    Surprisingly, we don't get a lot of thank yous in here :)

    You bring up a good point, I have been teaching a student how to program and there isn't a lot of information out there for the complete-noob... most assumes you have some programming knowledge already.
    I was thinking about putting my information up as free tutorials on my website in the near future. This is encouraging ;)

    -Sean C
  • hi sean,
    i wanna ask you one question
    do you have any notes regarding to because i tried many times to understand what i'd googled but i got a bit input. do you have any easy tutorial to be understand?
  • this program will how to use a two dimensional array in ..


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!