Results 1 to 24 of 24

Thread: 4 Parallel Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    4 Parallel Arrays

    How do I create 4 parallel arrays using this table:

    VB Code:
    1. ' Declare 4 parallel arrays for zones from this table:
    2.  
    3.     '   Weight  Zone A  Zone B  Zone C  Zone D
    4.     '   1       1.00    1.50    1.65    1.85
    5.     '   3       1.58    2.00    2.40    3.05
    6.     '   5       1.71    2.52    3.10    4.00
    7.     '   10      2.04    3.12    4.00    5.01
    8.     '   >10     2.52    3.75    5.10    7.25
    Twisted

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Assuming those values are CONSTANT, you can hard code them into your program. If you plan to change these values during you runtime then delete the red part of this code...

    VB Code:
    1. Dim table(,) As Single = New Single(4, 4) {[color=red]{1, 3, 5, 10, 10}, {!2nd column here!}, {!3rd column here!}, {!4th column here!}, {!5th column here!}[/color]}

    I can't be bothered typing the values in but you get the point from the above code.

    Experiment with the coordinates to make sure they are where they are where they should be, you may have to type the values in in row groups instead of column groups like above. I can't remember when I last initialised an array like this
    Last edited by wossname; Jun 30th, 2004 at 07:54 AM.
    I don't live here any more.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Is this not correct:

    VB Code:
    1. Dim dblCosts(4, 3) As Double
    2.         dblCosts(0, 0) = 1.0
    3.         dblCosts(0, 1) = 1.5
    4.         dblCosts(0, 2) = 1.65
    5.         dblCosts(0, 3) = 1.85
    6.         dblCosts(1, 0) = 1.58
    7.         dblCosts(1, 1) = 2.0
    8.         dblCosts(1, 2) = 2.4
    9.         dblCosts(1, 3) = 3.05
    10.         dblCosts(2, 0) = 1.71
    11.         dblCosts(2, 1) = 2.52
    12.         dblCosts(2, 2) = 3.1
    13.         dblCosts(2, 3) = 4.0
    14.         dblCosts(3, 0) = 2.04
    15.         dblCosts(3, 1) = 3.12
    16.         dblCosts(3, 2) = 4.0
    17.         dblCosts(3, 3) = 5.01
    18.         dblCosts(4, 0) = 2.52
    19.         dblCosts(4, 1) = 3.75
    20.         dblCosts(4, 2) = 5.1
    21.         dblCosts(4, 3) = 7.25
    Twisted

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    That's basically what wossname posted
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    This might help:

    1) I have 5 Radio buttons for Weight

    1
    3
    5
    10
    >10

    2) I have 4 Radio buttons for Zones

    Zone A
    Zone B
    Zone C
    Zone D

    So users will select a radio button for Weight and one for the Zone and depending on which ones they select will determine the data pulled from the arrays.

    Ex: Radio button 1 and Zone B selected

    then MessageBox.Show("("The shipping rate is " & strValue, "Cost to Ship")

    Where strValue will be the value from the arrays(which I also don't know how to set up a statement for) Please help!
    Twisted

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by plenderj
    That's basically what wossname posted
    Does it matter which way you populate it??

    Is this still 4 parallel arrays or a 2D array??
    Twisted

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    There is no such thing (to my knowledge) as "parralel arrays" in VB.net. Not unless you write a class to do that yourself.

    This is a 2 dimensional array.

    dblCosts(4) would be a 1 dimensional array
    dblCosts(4,4,4) would be a 3 dimensional array
    dblCosts(4,4,4,4) would be a 4 dimensional array
    I don't live here any more.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Thats what I was wondeting but my instructor said I needed to decalre 4 parallel arrays for zones. What the hell does she mean by that??
    Twisted

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Originally posted by twisted
    Thats what I was wondeting but my instructor said I needed to decalre 4 parallel arrays for zones. What the hell does she mean by that??
    Why don't you ask her? That's what teachers are for.

    IMO, you can't teach programming. You either teach yourself proper coding or someone teaches you their own style. Tutors rarely get time to actually DO any programming themselves, in the same way that Physics teachers seldom build lasers and aeroplanes. There is no susbstitute for getting stuck into a good programming book and learning stuff your own way. That's how I did it, and I reckon I'm not bad.
    I don't live here any more.

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    My guess is she means 4 separate arrays for the zones, where the values at any particular index match the correct line in weight.
    i.e.,
    VB Code:
    1. (1.0, 1.58, 1.71, 2.04, 2.52)
    2. (1.50, 2.00, 2.52, 3.12, 3.75)
    3. ' etc
    The values in the first index (index 0) are the values for weight 1, values in the second column are the values for weight 3, etc.
    Then you could do something like loop through the arrays and get all the values for index(0), index(1), etc.

  11. #11
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    VB Code:
    1. Dim zoneA ( 0 to 4 ) as whatever
    2. Dim zoneB ( 0 to 4 ) as whatever
    3. Dim zoneC ( 0 to 4 ) as whatever
    4. Dim zoneD ( 0 to 4 ) as whatever
    5.  
    6. zoneA(0) = 1.00
    7. zoneB(0) =1.5
    8. zoneC(0) = 1.65
    9. zoneD(0) = 1.85
    10.  
    11. ...

    That is probably what she meant by parallel arrays.

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    As mentioned in other posts, Parallel Arrays is NOT a term which has any technical meaning in VB.NET. Therefore we have to assume your instructor is using the term as an ordinary english expression. If she said "Four parallel arrays" for the zones from the table, then she probably means four single dimension arrays, as salvelinus posted but where each element (in this case row) number refers to the item Weight. (If you assume that element (0) referred to the weight 1 - which would be a more efficient use of the arrays - then she would have asked for five parallel arrays)Therefore several elements will be empty. For example in

    VB Code:
    1. Dim arrA(10), arrB(10), arrC(10), arrD(10) as Double

    elements 0, 2, 4, 6, 7, 8, 9 of all the arrays will be zero
    arrA(1) will contain 1.00
    arrA(3) ,, ,, 1.58

    arrB(1) ,, ,, 1.50
    arrB(3) ,, ,, 2.00

    etc.
    Last edited by taxes; Jun 30th, 2004 at 04:13 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I wasn't using the Weight column as an array, or the numbers in Weight as an index. The original question mentioned 4 arrays for the zone data. If you included Weight as an array, there would be 5. But nothing in the question says weight should be in the parallel array (true, not a VB term, but it sounds familiar from VB 5 schoolwork).
    Taxes, I don't get why you have a 10 element array. I only see 5 elements in each array.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by salvelinus
    My guess is she means 4 separate arrays for the zones, where the values at any particular index match the correct line in weight.
    i.e.,
    VB Code:
    1. (1.0, 1.58, 1.71, 2.04, 2.52)
    2. (1.50, 2.00, 2.52, 3.12, 3.75)
    3. ' etc
    The values in the first index (index 0) are the values for weight 1, values in the second column are the values for weight 3, etc.
    Then you could do something like loop through the arrays and get all the values for index(0), index(1), etc.
    This is what she wants I asked her. Thanks!

    Then I can loop through the arrays..HAHA do you know the kinda VB newb I am. You make it sound so easy. I will definitely try to write a statement to do so though. Think I should use a For...Next statement??
    Twisted

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by salvelinus

    Taxes, I don't get why you have a 10 element array. I only see 5 elements in each array.
    I would have thought that keeping the Weight figure equal to the index of the array was the logical approach . That would make locating the required values much easier, e.g. if you want the values for Weight 5:

    Pseudo Code

    Dim sTemp As String
    Dim iCount as Integer=5
    sTemp=str(arrA(iCount)) & " " & str(arrB(iCount))& " " & str(arrC(iCount)) & " " & str(arrD(iCount))

    I also assumed that the number of Weight figures might be increased.

    BUT it seems from twisted's last post that you are right and I am wrong. I was looking at it with commercial practicability in mind
    and, bearing in mind twisted's previous threads, was trying to anticipate future questions on the subject.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Yes, that would be logical, but only if the Weight value increased in regular increments. If the weight increases by an irregular number, you couldn't correlate the weight value with the index.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by salvelinus
    My guess is she means 4 separate arrays for the zones, where the values at any particular index match the correct line in weight.
    i.e.,
    VB Code:
    1. (1.0, 1.58, 1.71, 2.04, 2.52)
    2. (1.50, 2.00, 2.52, 3.12, 3.75)
    3. ' etc
    The values in the first index (index 0) are the values for weight 1, values in the second column are the values for weight 3, etc.
    Then you could do something like loop through the arrays and get all the values for index(0), index(1), etc.
    How do I set it up this way?

    Dim dblCosts() As Double = (1.0, 1.58, 1.71, 2.04, 2.52)

    Like this?? Or this:

    VB Code:
    1. Dim table(,) As Single = New Single(3, 4) {{1.0, 1.58, 1.71, 2.04, 2.52}, {1.5, 2.0, 2.52, 3.12, 3.75}, {1.65, 2.4, 3.1, 4.0, 5.1}, {1.85, 3.05, 4.0, 5.01, 7.25}}
    Last edited by twisted; Jul 1st, 2004 at 08:57 AM.
    Twisted

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    How would I pull info from my arrays using two radio buttons??

    If rad1.Checked And rad2.Checked Then
    strValue = "1.00"
    MessageBox.Show("The shipping value is " &strValue, "Cost")

    This would be a lot of if then statements...

    Any other way this can be done.

    User select a Weight (5 radio buttons) and selects a Zone ( 4 radio buttons) and then it should be the value in the table

    IM LOST!!
    Twisted

  19. #19
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    If she wants 4 parallel arrays, dim 4 separate arrays like in your first example. She's not asking for a multidimensional array.
    I don't see how you could select more than 4 separate values with 2 radio buttons (both checked, neither checked, first only checked, second only checked, and this certainly wouldn't be intuitive to a user).
    I'd use two comboxes, one to select the weight, one to select the zone. Using radio buttons, you'd have to have one for each row and one for each column.
    Last edited by salvelinus; Jul 1st, 2004 at 09:07 AM.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Thanks!
    VB Code:
    1. Private Sub btnRate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRate.Click
    2.  
    3.         Dim intRow, intColumn As Integer
    4.  
    5.         Dim dblArray1() As Double = {1.0, 1.58, 1.71, 2.04, 2.52}
    6.         Dim dblArray2() As Double = {1.5, 2.0, 2.52, 3.12, 3.75}
    7.         Dim dblArray3() As Double = {1.65, 2.4, 3.1, 4.0, 5.1}
    8.         Dim dblArray4() As Double = {1.85, 3.05, 4.0, 5.01, 7.25}
    9.  
    10.        
    11.  
    12.         Dim strValue As String
    13.  
    14.         MessageBox.Show("The shipping rate is " & strValue, "Cost to Ship")
    15.        
    16.  
    17.     End Sub
    18. End Class
    Last edited by twisted; Jul 1st, 2004 at 09:11 AM.
    Twisted

  21. #21
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by twisted
    How would I pull info from my arrays using two radio buttons??

    If rad1.Checked And rad2.Checked Then
    strValue = "1.00"
    MessageBox.Show("The shipping value is " &strValue, "Cost")

    This would be a lot of if then statements...

    Any other way this can be done.

    User select a Weight (5 radio buttons) and selects a Zone ( 4 radio buttons) and then it should be the value in the table

    IM LOST!!
    Hi, what is your instructor actually asking for? Two buttons or nine?

    How does she want the choice of weight and zone to be made - through input into a text box or by selection through a button, listview or combo?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Twisted

  23. #23
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    OK, you've got two group boxes. One, Weight has five radio buttons, and the other, Zone, has 4. Then you have a button that shows a message. And you have to have 4 parallel arrays to hold the Zone data.
    For example:
    Call your Weight radio buttons rbWeight0, rbWeight1, etc. Call your Zone radio buttons rbZone0, rbZone1, etc, and the arrays myArray0, myArray1, and so on. You could use other names, this just helps to make it clear what value you want later.
    You can initialize your arrays in a couple places. You could declare them form level variables and initialize them in the Form_Load event. You could declare and initialize them in the Button1_Click event. You could initialize a/o declare them in a separate function that returns the arrays. Ideally this data would be in a database, but that's not the assignment.
    The differences between declaring them at the form level or in an event or sub/function relate to performance issues. Form level variables will take up memory and are, potentially, a weak spot in your code (although they're certainly necessary and appropriate in some cases). But, you only deal with creating and initializing them once.
    Using an event/sub/function to declare and initialize them may save on memory, but will result in more processing overhead, because they have to be declared, initialized, and disposed of each time. For this app, the difference is almost nonexistent, but if you were dealing with a large app, lots of db stuff, it might be an issue. Memory isn't supposed to be as big an issue in .Net, but doesn't hurt to code as efficiently as possible.
    So anyway, somewhere you declare and initialize the arrays. Then, when the button is clicked, check which Weight button is checked and which Zone button is checked (and check to make sure at least one in each group is checked, or set one in each to checked as default). Then you can retrieve the correct array value.
    Then, to check which value to retrieve, use code like:
    VB Code:
    1. If rbWeight2.Checked Then
    2.     If rbZone2.Checked then
    3.         Messagebox.Show(myArray(2, 2))
    4.     End If
    5.     If rbZone3.Checked Then
    6.         Mesagebox.Show(myarray(2, 3))
    7. 'etc
    8. End If
    There's more efficient ways to check, that's just to give you an idea.
    [edit]
    Now that I've switched browsers from IE to FireFox (Mozilla), why is the reply box so much smaller?
    And why can't I get Airhead by Thomas Dolby out of my head? It's in my head for days anytime I play it.
    Last edited by salvelinus; Jul 1st, 2004 at 06:01 PM.

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    You are the man! Thanks for putting me on the right track.
    Twisted

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