8 Replies - 1599 Views - Last Post: 18 February 2011 - 01:13 AM Rate Topic: -----

#1 slymatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-February 11

Multi dimensional arrays

Posted 16 February 2011 - 01:04 PM

Hello i'm pretty new to programming i've only been doing it for about 8 months and i'm making a pay roll system and im stuck with an array, this is what i need help with.

                ]Console.WriteLine("Total Hours : " & workerstotalhours = workershours(workersnumber, 0) + workershours(workersnumber, 1) + workershours(workersnumber, 2) + workershours(workersnumber, 3) + workershours(workersnumber, 4))


                If workerstotalhours >= "44" Then
                    workerstotalhours = workerstotalhours * 15.66

                    If workerstotalhours >= "64" Then
                        MsgBox("You Have Overworked This Week")
                        Console.WriteLine("      Gross Income: £" & workerstotalhours & vbTab & "    Tax: £")
                        Console.WriteLine("National Insurance: £" & vbTab & "Net Pay: £")
                    Else
                        Console.WriteLine("      Gross Income: £" & workerstotalhours & vbTab & "    Tax: £")
                        Console.WriteLine("National Insurance: £" & vbTab & "Net Pay: £")

                    End If

                ElseIf workerstotalhours <= "43" Or workerstotalhours >= "37" Then
                    workerstotalhours = workerstotalhours * 10.32
                    Console.WriteLine("      Gross Income: £" & workerstotalhours & vbTab & "    Tax: £")
                    Console.WriteLine("National Insurance: £" & vbTab & "Net Pay: £")

                ElseIf workerstotalhours <= "36" Then

                    workerstotalhours = workerstotalhours * 7.45

                    Console.WriteLine("      Gross Income: £" & workerstotalhours & vbTab & "    Tax: £")
                    Console.WriteLine("National Insurance: £" & vbTab & "Net Pay: £")

                Else
                    Console.WriteLine("*****Problem With Payment*****")

                End If[/color]


The first piece of code is to work out the amount of hours that has been done and then to put it in to the variable workers total hours, but because its a multi dimensional array it wont allow me to do the sums that are in the second bit of code, I would be very happy if someone could help me, (all the dims are there just didn't copy and paste them)

This post has been edited by slymatt: 16 February 2011 - 01:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Multi dimensional arrays

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Multi dimensional arrays

Posted 16 February 2011 - 01:59 PM

This should work:
workerstotalhours = workershours(workersnumber, 0) + workershours(workersnumber, 1) + workershours(workersnumber, 2) + workershours(workersnumber, 3) + workershours(workersnumber, 4)) 
Console.WriteLine("Total Hours : " & workerstotalhours
A cooler way to do this would be:
dim i as integer
workerstotalhours = 0
for i = 0 to 4
    workerstotalhours += workershours(workersnumber, i)
next i
Console.WriteLine("Total Hours : " & workerstotalhours
This allows more flexibility for change.
Was This Post Helpful? 2
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Multi dimensional arrays

Posted 16 February 2011 - 02:10 PM

By the way, I'm expecting that Console would keep writing "False" in your code, or perhaps having a type mismatch error. If you explain the error you're getting, I'll have a go at explaining why.
Was This Post Helpful? 0
  • +
  • -

#4 TestSubjectRC2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-January 11

Re: Multi dimensional arrays

Posted 16 February 2011 - 02:15 PM

If I'm understanding the problem correctly, you're not able to get the sum into workerstotalhours? If so, BobRodes' answer is certainly workable or what worked for me is just adding an extra set of parenthesis to your first line so VB only tried to convert the final result rather than converting each variable individually.

Console.WriteLine("Total Hours : " & (workerstotalhours = workershours(workersnumber, 0) + workershours(workersnumber, 1) + workershours(workersnumber, 2) + workershours(workersnumber, 3) + workershours(workersnumber, 4)))


Or are you saying that workerstotalhours is an array as well?

This post has been edited by TestSubjectRC2: 16 February 2011 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#5 slymatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-February 11

Re: Multi dimensional arrays

Posted 16 February 2011 - 02:20 PM

Thanks BobRodes your way works =) and I use the cool way as-well thanks allot. its my first time using this site and I got an answer in an hour =) very grateful.
Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Multi dimensional arrays

Posted 16 February 2011 - 02:30 PM

I should point out also that in your code, anyone working 44 or more hours is overworking, since 44 * 15.66 is 689.04. You probably want to use two variables, one for workers total hours and one for gross pay.
Was This Post Helpful? 0
  • +
  • -

#7 slymatt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-February 11

Re: Multi dimensional arrays

Posted 16 February 2011 - 02:45 PM

well this is how it looks now;
these are my dims

Dim gross, tax, nat, net As Integer


and this is the rest of the code + would i need to change the Integer to double for it to work with decimals i would wouldnt i?
 Console.Write("Friday Hours   : ") : Console.WriteLine(UCase(workershours(workersnumber, 4))) 'writes the 8rd bit of the array


        workerstotalhours = 0

        For gross = 0 To 4
            workerstotalhours += workershours(workersnumber, gross)
        Next gross
        Console.WriteLine("Total Hours : " & workerstotalhours)

        Console.WriteLine()

        If workerstotalhours >= "64" Then

            MsgBox("You Have Overworked This Week")

            workerstotalhours = workerstotalhours * 15.66

            tax = workerstotalhours / 100 * 20%

            Console.WriteLine("      Gross Income: " & workerstotalhours & vbTab & "    Tax: " & tax)

            Console.WriteLine()

            Console.WriteLine("National Insurance: " & vbTab & "Net Pay: ")

        ElseIf workerstotalhours >= "44" Then

            workerstotalhours = workerstotalhours * 15.66

            tax = workerstotalhours / 100 * 20%

            Console.WriteLine("      Gross Income: " & workerstotalhours & vbTab & "    Tax: " & tax)

            Console.WriteLine()

            Console.WriteLine("National Insurance: " & vbTab & "Net Pay: ")


        ElseIf workerstotalhours <= "43" Or workerstotalhours >= "37" Then

            workerstotalhours = workerstotalhours * 10.32

            tax = workerstotalhours / 100 * 20%

            Console.WriteLine("      Gross Income: " & workerstotalhours & vbTab & "    Tax: " & tax)

            Console.WriteLine()

            Console.WriteLine("National Insurance: " & vbTab & "Net Pay: ")

        ElseIf workerstotalhours <= "36" Then

            workerstotalhours = workerstotalhours * 7.45

            tax = workerstotalhours / 100 * 20%

            Console.WriteLine("      Gross Income: " & workerstotalhours & vbTab & "    Tax: " & tax)

            Console.WriteLine()

            Console.WriteLine("National Insurance: " & vbTab & "Net Pay: ")

        Else
            Console.WriteLine("*****Problem With Payment*****")

        End If

Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Multi dimensional arrays

Posted 18 February 2011 - 01:01 AM

Yes, but I would use Decimal rather than Double. Look at this: http://msdn.microsof...y/xtba3z33.aspx . You're really not working with floating-point numbers: since you're working with money, you want a fixed number of decimal places. Double would be more for variable numbers of decimal places.
Was This Post Helpful? 1
  • +
  • -

#9 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Multi dimensional arrays

Posted 18 February 2011 - 01:13 AM

Also, Select Case is cleaner here. You're only evaluating workerstotalhours, and doing different things based on the one variable. That's when you use this:
Select Case workerstotalhours
     Case Is >= 64
          [do stuff]
     Case Is >= 44
          [do other stuff]
     Case Is >= 37
          [do other stuff]
     Case Is < 37
          [do other stuff]
     Case Else
          [error]
End Select 
Now, yours is messed up logically as well. If you think about it, EVERY number is either less than 43 or greater than 37! :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1