Class Notes

Topic of the day: Array pg139-140

 


When ever you see an array, you will see a loop, not the other way around.
There are things that you can not do easily without array.
Example: Sorting


How do you declare an array?
Page 139 does “Overtime Pay”

¤ Does this program use an array?
NO!
¤ Does this program use an array? Page 140
YES!
¤ Is the input/output the same?
YES!
¤ What is the difference between these two programs?

Parenthesis is the difference.
Parenthesis means three things
1) array 2) function 3) grouping


How will you convert this code into an array?
Dim empid, hoursworked, overtimehours As Integer

How many instances of employee id we have? 1-100 employees.

What’s the good thing about arrays?

The data will remain, no overwriting of data.

Do you know what does “end sub” means?
It means end of subroutine.


Conversion in this case is one-one. In arrays rooms are numbers from 0-99 in this case.
We can do it through textbox, labels and listbox.
We will use msgbox in this case.
Dim empid(100), hoursworked(100), overtimehours(100), i As Integer

        Dim hourlyrate(100), regularpay(100), overtimepay(100), grosspay(100) As Double

        Dim fin As IO.StreamReader = IO.File.OpenText("employees.txt")

 

        While (fin.Peek <> -1)

            empid(i) = fin.ReadLine

            hoursworked(i) = fin.ReadLine

            hourlyrate(i) = fin.ReadLine

 

            If (hoursworked(i) > 40) Then

                overtimehours(i) = hoursworked(i) - 40

                overtimepay(i) = overtimehours(i) * hourlyrate(i) * 1.5

                regularpay(i) = 40 * hourlyrate(i)

 

            Else

                overtimehours(i) = 0

                overtimepay(i) = 0

                regularpay(i) = hoursworked(i) * hourlyrate(i)

            End If

 

            grosspay(i) = regularpay(i) + overtimepay(i)

 

            MsgBox("EMPLOYEE ID IS " & empid(i))

            MsgBox("OVERTIME PAY IS " & overtimepay(i))

            MsgBox("GROSS PAY IS IS " & grosspay(i))

        End While

Now using labels to output our data
   Dim empid(100), hoursworked(100), overtimehours(100), i As
Integer

        Dim hourlyrate(100), regularpay(100), overtimepay(100), grosspay(100) As Double

        Dim fin As IO.StreamReader = IO.File.OpenText("employees.txt")

        Label1.Text = ""

        i = 0

        While (fin.Peek <> -1)

            empid(i) = Val(fin.ReadLine)

            hoursworked(i) = Val(fin.ReadLine)

            hourlyrate(i) = Val(fin.ReadLine)

 

            If (hoursworked(i) > 40) Then

                overtimehours(i) = hoursworked(i) - 40

                overtimepay(i) = overtimehours(i) * hourlyrate(i) * 1.5

                regularpay(i) = 40 * hourlyrate(i)

 

            Else

                overtimehours(i) = 0

                overtimepay(i) = 0

                regularpay(i) = hoursworked(i) * hourlyrate(i)

            End If

 

            grosspay(i) = regularpay(i) + overtimepay(i)

 

 

            Label1.Text = Label1.Text & empid(i) & " " & overtimepay(i) & " " & grosspay(i) & vbNewLine

            i = i + 1

        End While

 

 

Now using independent labels for each output.
  Dim empid(100), hoursworked(100), overtimehours(100), i As Integer

        Dim hourlyrate(100), regularpay(100), overtimepay(100), grosspay(100) As Double

        Dim fin As IO.StreamReader = IO.File.OpenText("employees.txt")

        Label1.Text = ""

        Label2.Text = ""

        Label3.Text = ""

 

        i = 0

        While (fin.Peek <> -1)

            empid(i) = Val(fin.ReadLine)

            hoursworked(i) = Val(fin.ReadLine)

            hourlyrate(i) = Val(fin.ReadLine)

 

            If (hoursworked(i) > 40) Then

                overtimehours(i) = hoursworked(i) - 40

                overtimepay(i) = overtimehours(i) * hourlyrate(i) * 1.5

                regularpay(i) = 40 * hourlyrate(i)

 

            Else

                overtimehours(i) = 0

                overtimepay(i) = 0

                regularpay(i) = hoursworked(i) * hourlyrate(i)

            End If

 

            grosspay(i) = regularpay(i) + overtimepay(i)

 

 

            Label1.Text = Label1.Text & empid(i) & vbNewLine

            Label2.Text = Label2.Text & FormatCurrency(overtimepay(i)) & vbNewLine

            Label3.Text = Label3.Text & FormatCurrency(grosspay(i)) & vbNewLine