how to increment elements in an array

  • 15 years ago

    I am working on an exercise from the Deitel "Simply" Visual Basic book. p.379. Here is the objective :
    to create an application that determines how many salespeople earned salaries in each range (the reanges are 0-99, 100-199, 200-299 etc).

    create a string array and initialize it to contain the salary ranges

    create an array that represents the number of salaries in each range. this is a decimal array to store_the number of employees who earn salaries in each range.

    create an event handlers for a calculate button: obtain user input from an Enter Sales Text Box.

    calculate the commission due and add to the base salary.

    Increment the element in array decSalaries that corresponds to the the employees salary in the Total Salary : label

    if you would please take a look at the code I have so far written this morning, you will see that (I think) I have achieved most of the objectives. I am able to calculate the
    commission due and display it correctly.













    Public Class FrmSalarySurvey
       Inherits System.Windows.Forms.Form

       'string array stores the salary ranges
       Dim m_strSalaryRanges As String() = New String() _
       {"200 - 299", "300 - 399", "400 - 499", "500 - 599", "600 - 699", "700 - 799", _
       "800 - 899", "900 - 999", "1000 - "}

       'an empty array to store how many employees earn each salary range
       Dim m_decSalaries As Decimal() = New Decimal(m_strSalaryRanges.GetUpperBound(0)) {}

       Dim m_intCounter As Integer = 1 ' number of salaries

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
          Dim dblgrossSales As Double
          Dim dblcommission As Double = 0.09
          Dim dblSalary As Double
          Dim dblBaseSalary As Double = 200

          dblgrossSales = Val(txtInputSales.Text) ' assigning a property's value to a variable

          dblSalary = dblgrossSales * dblcommission + dblBaseSalary 'calculate the total salary due

          lblTotalSalary.Text = Convert.ToString(dblSalary) 'returning the variable to a property as a String

          'increment the element in m_decSalaries array that corresponds to the employee's salary range

          m_intCounter += 1 
    End Sub
    End Class ' FrmSalarySurvey




























    My problem is now this : how to "increment the element in array decSalaries that correspond to the employee's saalary range"
    I am supposed to create a loop , I heard yesterday from a friend who wont tell me anymore !
     
    Thanks for any tips.
    Dan Ashcroft.



     

  • 15 years ago

     m_intCounter += 1  ' This is your incrementer

     

    At the beginning of your code you'll need a for loop and after the above line you need to end the for loop.

  • 15 years ago
    You then need to calculate each persons salary range and then increment the corresponding array element.  To do this you need the index of that array element.  To find that you determine the index in the salary range array using Array.IndexOf.

  • 15 years ago

    in order to "increment the element in array decSalaries that corresponds to the employees salary range" , am I on the right track doing this ?
     'string array stores the salary ranges
       Dim mstrSalaryRanges As String() = New String() _
       {"200 - 299", "300 - 399", "400 - 499", "500 - 599", "600 - 699", "700 - 799"}

     'an empty array to store how many employees earn each salary range
       Dim m
    decSalaries As Decimal() = New Decimal(mstrSalaryRanges.GetUpperBound(0)) {}

      Private Sub btnCalculate
    Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
          Dim dblgrossSales As Double
          Dim dblcommission As Double = 0.09
          Dim dblSalary As Double
          Dim dblBaseSalary As Double = 200

          dblgrossSales = Val(txtInputSales.Text)

          dblSalary = dblgrossSales * dblcommission + dblBaseSalary

          lblTotalSalary.Text = Convert.ToString(dblSalary) 

    increment the element in mdecSalaries array that corresponds to the
          'employee's salary range
          Select Case dblSalary
             Case 200 To 299
                m
    decSalaries(0) += 1
             Case 300 To 399
                mdecSalaries(1) += 1
             Case 400 To 499
                m
    decSalaries(2) += 1
             Case 500 To 599
                mdecSalaries(3) += 1
             Case 600 To 699
                m
    decSalaries(4) += 1
             Case 700 To 799
                m_decSalaries(5) += 1
     End Select

    I get the feeling that I am going about this the long way round.






































  • 15 years ago
    Dim i As Integer
          For i = 0 To 6
             Dim m_Str As String

             m_Str = Convert.ToString(m_decSalaries(i))

             lstSalaryTotals.Items.Add(m_strSalaryRanges(i) + ControlChars.Tab & m_Str)

    was what I was missing, in order to display the incremented elements.











Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry