01/18/06

Array:

Case study back of the chapter in book. # 5 is array,

last question on the midterm was a search program.  converting array to function is very easy. The reason we start the array before function, so you may have the better understanding what functions can do. Function allows team work. Array makes you focus on one place because you have data at one place.

There are three program deal with payroll in chapter 5. Page #139,140 & 159

140 is array

139 is not array

159 is array, can also be converted to function. Every "for" loop can be one function. Also loops and array works together. Search program contains array, if statement and 'for' loops.

Let's do : page 140

line 7 has problem, you getting data from consol, what you should do, first loop for infinite, input box enter employee id

input box hours work then assign it to 'i'

enter hourly rate , assign it to 'i'

you need to put one of the outside of the loop , for example id

As long as employee id is not 'null'

now you need to repeat, add another employee id before you come back

rest are the same program.

Dim empid(100) As String

Dim i As Integer = 0

Dim hoursworked(100), overtimehours(100) As Integer

Dim hourlyrate(100), regularpay(100) As Single

Dim overtimepay(100), grosspay(100) As Single

empid(i) = InputBox("Enter Employee Id: ")

While empid(i) <> ""

hoursworked(i) = InputBox("Enter Hours Worked: ")

hourlyrate(i) = InputBox("Enter Hourly Rate: ")

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: " & grosspay(i))

empid(i) = InputBox("Enter Employee Id: ")

End While

End Sub

 

 

PAGE 159

PAYROLL PROGRAM WITH THE USE OF ARRAYS

 

Dim empid(100) As String

Dim i As Integer = 0

Dim hoursworked(100), overtimehours(100) As Integer

Dim hourlyrate(100), regularpay(100) As Single

Dim overtimepay(100), grosspay(100), netpay(100), taxrate(100), taxamount(100) As Single

Dim counter As Integer = 0

empid(counter) = InputBox("Enter Employee Id: ")

While empid(counter) <> ""

hoursworked(counter) = InputBox("Enter Hours Worked: ")

hourlyrate(counter) = InputBox("Enter Hourly Rate: ")

counter = counter + 1

empid(counter) = InputBox("Enter Employee Id: ")

End While

For i = 0 To counter

grosspay(i) = hoursworked(i) * hourlyrate(i)

Next

For i = 0 To counter

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

MsgBox("HOURS WORKED IS: " & hoursworked(i))

MsgBox("HOURLY RATE IS: " & hourlyrate(i))

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

Next

 

PAY ROLL PROGRAM WITH THE USE OF ARRAY

 

Dim empid(100) As String

Dim i As Integer = 0

Dim hoursworked(100), overtimehours(100) As Integer

Dim hourlyrate(100), regularpay(100) As Single

Dim overtimepay(100), grosspay(100), netpay(100), taxrate(100), taxamount(100) As Single

Dim counter As Integer = 0

empid(counter) = InputBox("Enter Employee Id: ")

While empid(counter) <> ""

hoursworked(counter) = InputBox("Enter Hours Worked: ")

hourlyrate(counter) = InputBox("Enter Hourly Rate: ")

counter = counter + 1

empid(counter) = InputBox("Enter Employee Id: ")

End While

For i = 0 To counter

grosspay(i) = hoursworked(i) * hourlyrate(i)

Next

For i = 0 To counter

If grosspay(i) > 500 Then

taxrate(i) = 0.3

ElseIf grosspay(i) > 200 Then

taxrate(i) = 0.2

Else : taxrate(i) = 0.1

End If

Next

For i = 0 To counter

taxamount(i) = grosspay(i) * taxrate(i)

Next

For i = 0 To counter

netpay(i) = grosspay(i) - taxamount(i)

Next

For i = 0 To counter

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

MsgBox("HOURS WORKED IS: " & hoursworked(i))

MsgBox("HOURLY RATE IS: " & hourlyrate(i))

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

MsgBox("TAX RATE IS: " & taxrate(i))

MsgBox("TAX AMOUNT IS: " & taxamount(i))

MsgBox("NET PAY IS: " & netpay(i))

 

Next

 

 

ON PAGE 203

readalldata( )
findgrosspay( )
findtaxrate( )
findtaxamount ( )
findnetpay ( )

 

What's wrong with this following program?

Dim empid(100) As String
dim
hoursworked(100) As Integer

Dim hourlyrate(100) As Single

Dim counter As Integer

counter = 0

empid(counter) = InputBox("Enter Employee Id: ")

While empid(counter) <> ""

hoursworked(counter) = InputBox("Enter Hours Worked: ")

hourlyrate(counter) = InputBox("Enter Hourly Rate: ")

empid(counter) = InputBox("Enter Employee Id: ")

counter = counter + 1

End While

 

 

 

 

 

 

on top screen program terminates by itself after entering the employee id on the second time.

This program gets a series of input data by input box such as employee id , hourly rate and store them into corresponding arrays. Program will terminate when cancel button is pressed which is null. However, program will terminate on the second round. Can you explain why? One reason the the increment should be before the next input. Because its after the next input, the next data will be re-written into the same spot (Zero location). Program will then be testing next location(1st) which contains NULL. In VB environment, a string is initialized to null by default.