1 Replies - 24671 Views - Last Post: 02 February 2011 - 10:43 PM Rate Topic: -----

#1 pxxpgoblin   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-January 11

Declaring a 2D Array

Posted 02 February 2011 - 09:32 PM

hey i was wondering if anyone could please help me out with this problem i will post it below i am kinda having a hard time understanding it.like could anyone kinda just get me started on this i just need some example or something to just start on i am so visual so i have a hard time just imagining this.

"declare a two dimensional array of integers named intgrades.it should have 30 rows and 10 columns

Thanks a lot any help will be appreciated.

This post has been edited by macosxnerd101: 02 February 2011 - 09:33 PM
Reason for edit:: Title renamed to be more descriptive

Is This A Good Question/Topic? 0
  • +

Replies To: Declaring a 2D Array

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5227
  • View blog
  • Posts: 14,010
  • Joined: 18-April 07

Re: Declaring a 2D Array

Posted 02 February 2011 - 10:43 PM

A two dimensional array can be thought of like a spreadsheet with rows and columns. The rows are one dimension and the columns are the second dimension. Yes you can have 3 or more dimensions in case you were wondering.

So when we declare one, we have to first say how many dimensions and how long those dimensions are. We do this using parenthesis and two values separated by a comma. Now keep in mind that arrays start at zero, so by specifying 29 we are using 29 numbered indexes, plus zero to make 30.

Dim rectArray(29, 9) As Integer

The first value is the length of the first dimension, or rows in our analogy. The second value is our second dimension or columns. What would be a three dimensional array? Simple. Add another comma and value...

Dim rectArray(29, 9, 5) As Integer

Now we can initialize these by using nested loops or by specifying groups of numbers in curly braces.

' 5 groups of 3 values (15 total values)
Dim rectArray(4, 2) As Integer = {{1,2,3}, {3,4,5}, {5,6,7}, {7,8,9}, {0,1,2}}

Here you can see we have created a list and in that list we have little groups of three values. So 1,2,3 goes in row 0. The value 1 is in column 0 of row 0. Just like in a spread sheet.

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1