Results 1 to 3 of 3

Thread: Need help with two-dimensional arrays.

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    34

    Need help with two-dimensional arrays.

    Hi I have a question about a practice problem I am doing in my Visual Basic Reloaded:second edition school book. Ok here is all the information the case problem gives me and what it wants me to do.

    JM Sales employs 10 salespeople. The sales made by the salespeople during the months of January, February, and March are shown in Figure 8.52. The sales manager wants an application that allows him to enter the current bonus rate. The application should display each salesperson's number (1 through 10), total sales ammount, and total bonus ammount. It also should display the total bonus paid to all salespeople. Be sure to use one or more array in the application.

    Then it gives you a table with the salespersons 1-10 sales amount january February and March.(this is the figure 8.52)Here is a duplicate table of it below.

    I'm really rough on arrays and I would appreciate a little help on how exactly to do this problem. Thanks


    Code:
    Salesperson       January      February        March
    -----------       -------       --------        -------
    1                     2400             3500           2000
    2                     1500            7000            1000
    3                     600              450             2100
    4                     790              240             500
    5                     1000            1000            1000
    6                     6300            7000            8000
    7                     1300            450              700
    8                     2700            5500            6000
    9                     4700            4800            4900
    10                   1200            1300            1400
    Here's far as i could get

    Code:
     Dim januarySales(,) As Integer = {{1, 2400}, _
            {2, 1500}, _
            {3, 600}, _
            {4, 790}, _
            {5, 1000}, _
            {6, 6300}, _
            {7, 1300}, _
            {8, 2700}, _
            {9, 4700}, _
            {10, 1200}}
    
            Dim februarySales(,) As Integer = {{1, 3500}, _
            {2, 7000}, _
            {3, 450}, _
            {4, 240}, _
            {5, 1000}, _
            {6, 7000}, _
            {7, 450}, _
            {8, 5500}, _
            {9, 4800}, _
            {10, 1300}}
    
    
            Dim marchSales(,) As Integer = {{1, 2000}, _
            {2, 1000}, _
            {3, 2100}, _
            {4, 500}, _
            {5, 1000}, _
            {6, 8000}, _
            {7, 700}, _
            {8, 6000}, _
            {9, 4900}, _
            {10, 1400}}

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Need help with two-dimensional arrays.

    Food for thought. To try it out you need a form with a button.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'test
            Dim myTable As New AllSalesPeople
            'add some sales people
            Dim sp As New SalesPerson("dbasnett")
            myTable.Add(sp)
            sp = New SalesPerson("pisotmo")
            myTable.Add(sp)
    
            'random sales data
            Dim r As New Random
    
            For x As Integer = 1 To 12
                For Each sp In myTable.SalesPeople
                    sp.MonthSales(x, CDec(Math.Round(r.NextDouble * 1000, 2)))
                Next
            Next
    
            'display totals
            For Each sp In myTable.SalesPeople
                Debug.Write(sp.SalesPersonNumber.ToString & " " & sp.SalesPersonName & " ")
                For Each amt In sp.MonthlySales
                    Debug.Write(amt.ToString("C2") & " ")
                Next
                Debug.WriteLine("   Total - " & sp.TotalSales.ToString("C2"))
            Next
        End Sub
    
        Private Class AllSalesPeople
            Const _firstNum As Integer = 1001 'the first sales person number to assign
            Private _num As Integer = _firstNum 'the current sales person number
            Private _people As New Dictionary(Of Integer, SalesPerson)
    
            Public Sub Add(ByVal person As SalesPerson)
                person.SalesPersonNumber = Me._num 'set the sales person number
                Me._people.Add(Me._num, person) 'add them to dictionary
                Me._num += 1 'for next sales person
            End Sub
    
            ReadOnly Property Count() As Integer
                Get
                    Return Me._people.Count
                End Get
            End Property
    
            ReadOnly Property SalesPeople() As List(Of SalesPerson)
                Get
                    Return Me._people.Values.ToList
                End Get
            End Property
    
        End Class
    
        Private Class SalesPerson
            Private _sales(11) As Decimal 'monthly sales
            Private _name As String = "" 'SP name
            Private _mySPnum As Integer 'SP number
    
            Public Sub New(ByVal Name As String)
                Me._name = Name
            End Sub
    
            Property SalesPersonNumber() As Integer
                Get
                    Return Me._mySPnum
                End Get
                Set(ByVal value As Integer)
                    Me._mySPnum = value
                End Set
            End Property
    
            ReadOnly Property SalesPersonName() As String
                Get
                    Return Me._name
                End Get
            End Property
    
            Public Sub MonthSales(ByVal theMonth As Integer, ByVal theAMT As Decimal)
                Me._sales(theMonth - 1) = theAMT
            End Sub
    
            ReadOnly Property TotalSales() As Decimal
                Get
                    Return Me._sales.Sum
                End Get
            End Property
    
            ReadOnly Property MonthlySales() As List(Of Decimal)
                Get
                    Dim rv As New List(Of Decimal)
                    rv.AddRange(Me._sales)
                    Return rv
                End Get
            End Property
        End Class
    End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Need help with two-dimensional arrays.

    This really isn't a case where a multi-dimensional array makes sense. What DBasnett showed is a better way to do the same thing using a class to hold the information. However, if you haven't dealt with classes, and HAVE to use a multi-dimensional array, then you need just one array, not one per month.

    Think of the first dimension as being the row from your example such that it holds the salesperson (1-10), then the second dimension is the column, or month. Probably that would be 1-12, eventually, but if the example is only going to use three months, then you could size that dimension to 3. Therefore:

    array(1,1) is January for salesperson 1
    array(1,2) is February for salesperson 1
    array(1,3) is March for salesperson 1
    array(2,1) is January for salesperson 2

    and so forth.
    My usual boring signature: Nothing

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