Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?

VB.NET

Moderators: seancampbell
Number of threads: 3618
Number of posts: 9200

This Forum Only
Post New Thread

Report
Two Dimensional Arrays in VB.NET Posted by TimCereja on 6 Nov 2009 at 7:18 PM
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.
Report
Re: Two Dimensional Arrays in VB.NET Posted by seancampbell on 11 Nov 2009 at 8:06 AM
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
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,
Campbell
Report
Re: Two Dimensional Arrays in VB.NET Posted by DrMarten on 12 Nov 2009 at 5:05 PM
Hi SeanCampbell,

I guess you meant

SumHighs = SumHighs + WeatherData(I, 1)


in your last FOR NEXT loop.


Regards,

Dr M
Report
Re: Two Dimensional Arrays in VB.NET Posted by iamrdbrown on 18 Nov 2009 at 10:39 AM
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.

Ruth
Report
Re: Two Dimensional Arrays in VB.NET Posted by seancampbell on 18 Nov 2009 at 11:05 AM
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



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.