# Need help with a equation



## Couriant (Mar 26, 2002)

I don't know where to ask this but since it's part of my Programming Logic class i figured I can ask you 

I need an equation to work out the total hours in a week that a person works.

I know that MOD and the \ operators are used but I just can't get my head around it. Any help will be appreciated


----------



## Elvandil (Aug 1, 2003)

We could use a bit more information. This could be anything from Hopf Algebras to Modal Logic. What course is it, first of all? What are you trying to equate and what are the variables?


----------



## Couriant (Mar 26, 2002)

Well that would help 

I need to calcuate the total hours that an employee works. I would assume the variables would be AM_IN, AM_OUT, PM_IN and PM_OUT and the time would have to be in military time (obviously ) I thought that the equation would have been (AM_OUT-AM_IN)+(PM_OUT-PM_IN) but for an 8 hour day (say 0800 - 1230 and 1330 to 1700). HOURS variable is the total amount of hours in a day TOTAL_HOURS is another variable which will be all the hours calcuated for the week.

What i need is to work out the total hours in a week. (The value needs to be in 2 decimal places too) 

I was looking at a loop to loop back to add in another timesheet. Then I need to calc all of the hours together. etc. 

Hope this is understandable


----------



## Couriant (Mar 26, 2002)

bump


----------



## Mosaic1 (Aug 17, 2001)

Do you have any code which you have tried? This is for a class. 
Which programming language are you using?


----------



## Mosaic1 (Aug 17, 2001)

I use Visual Basic and so will show a quick example of how the datediff function can be used.


```
Private Sub Command1_Click()
Text1 = DateDiff("n", "10:00 am", "3:15 pm") / 60 & " hrs"

End Sub
```
This gives a nice clean result. This example uses minutes as the interval and then divides the result by 60 to get the hours worked.

5.25 hrs


----------



## Mosaic1 (Aug 17, 2001)

If you want to round off each interval to 2 decimal places then go a bit further like this:


```
Private Sub Command1_Click()
Dim M As Single
M = DateDiff("n", "10:01 am", "3:15 pm") / 60
Text1 = Format(M, "00.00") & " hrs"
End Sub
```
To get

05.23 hrs

This is just an example of getting the length and then formatting it. Using a text box is for convenience. You Likely not add the String "hrs" to the result or store it in a text box. But oif you did, reformat the results back to single to do math.

Also, how are the time cards formatted? There are a lot of ways to do this. The real trick is reading the daily information from the time card and doing the math.

How are you doing that?

Monday = 6.45
Tuesday = 3.25
Wednesday = 4.45

etc 
Or like this. 
Total = Total + M

M is each interval and gets added to a running total.

Or MMon could be Monday Morning work.

A could be afternoon. AMon = Mon Afternoon hours. 
on and on for each day of the week. 
These are just a few ideas.


----------



## Couriant (Mar 26, 2002)

yeah uhh..  The 'class' is Programming Logic. We are not using a program persay, we need to do flowcharts and algorithms to show the modules READ, CALC and PRINT etc. We need to also show the calcuations in the flowcharts too. I have a file I did to show you what I mean. We covered the formula in a class where we done what I posted earlier like AM_OUT - AM_IN etc and then we used the MOD and \ functions, but I don't have any clue on how to use those. And now that the weather has turned bad I don't think school will be open today (monday).


----------



## Mosaic1 (Aug 17, 2001)

I don't have Word installed and the word Viewer I used to use will not reinstall. Word Pad doesn't open your doc and show anything. Must be the graphics.


----------



## Couriant (Mar 26, 2002)

try this


----------



## Mosaic1 (Aug 17, 2001)

I know what a flowchart is. The problem you have is in Figuring out how many hours a person has worked. I am not sure what you need. 

Sorry. It's very late here. Almost 2 am. I have to sign off.


----------



## Couriant (Mar 26, 2002)

I hear ya, I'm on the same time zone. I was told that I could work out the hours by first calculating the minutes by doing the AM_IN etc and then using the MOD and \ function. If I can work that out then I can make a loop in it to do the rest of the weeks timesheet. I just need the calcuation... Man this is harder than I thought 

OK, to make this as simple as I can make it.  By typing in the time sheet, I need to calcuate the total hours an employee worked. The hours will be used to calcuate their pay for the week.

Need to go to bed.. me sleepy


----------



## Mosaic1 (Aug 17, 2001)

If we are playing at pseudo code then here, have fun 
For each Day in Days 
Hours worked = (AM_ OUT - AM_IN ) + (PM_OUT - PM_IN)
Total = Total + Hours Worked
Next


----------



## Couriant (Mar 26, 2002)

I got that part, but how to I get the value into hours? Here is what would happen in that equation

800 = (1230-0800) + (1700-1330)

How do i get the hours from 800? See why its confusing me?


----------



## Mosaic1 (Aug 17, 2001)

Because you want decimals and time doesn't work that way. Not the minutes. There are 60 minutes in an hour. 1 min = 1/60th hour. 1 2/3 * minutes = percentage You'll see in a second. 


Aren't time cards generally not in military hours? You would have to convert if you use this method. If PM then add 1200 to the PM entry. (Also this assumes it is not a midnight shift. If in at 10 pm and out at 2 am (Date Change) then we would need a different logic where we got the difference for start and then end from midnight and added together those differences.)

Are you not able to simulate the fact that most programming languages have a time function to figure this out? Ok I take it you want to use Mathematical operators to do this. You will have a bit of round off error. But here's the general gist. You want to get the hours and then convert the leftover minutes to decimal so 430 becomes 4.5 hrs. Correct?

Like this, if the worker worked from 7:30 am to 11:45 am 
Difference = (1145 - 730) / 100
Hours = Difference Mod 100
Minutes = (Difference - Hours)

Finally Convert the minutes to decimal form. 
Decimal Hours = Hours + (Minutes * 1.6666667) 

The result is 4.25 hours worked. A lot of places have a majority rule. If you work 8 minutes, then that is counted 15. Less than 8 and that is 0. That avoids a lot of extra math. 

Also, that picture you attached is causing a terrible scroll. If you get a chance, could you remove or resize it please? Thanks.


----------



## Mosaic1 (Aug 17, 2001)

EDIT When I said 
Finally Convert the minutes to decimal form. 
Decimal Hours = Hours + (Minutes * 1.6666667) 


I should have said Finally Convert the minutes to decimal form and add them to the hours.


----------



## Mosaic1 (Aug 17, 2001)

For one entire day:
Difference = ((1130 - 800) + (1700 - 1200)) / 100
Hours = Difference Mod 100
Minutes = (Difference - Hours)
DecHours = Hours + (Minutes * 1.6666667)
Final result (DecHours) = 8.5 hours.

EDIT> No. It didn't take me that long to figure it out. I just came by to see if you had answered. 


Here's one for you. It's about 10 degrees here. How many lumps of coal will Santa have to leave to keep this house warm?


----------



## haplo210 (Nov 23, 2004)

answer not as many as we need here, minus 3.


----------



## Mosaic1 (Aug 17, 2001)

LOL Ouch. Now that's cold. 

Merry Christmas.


----------



## haplo210 (Nov 23, 2004)

its like a message...welcome to scotland...not only does your car freeze on the outside, but also the inside of the windscreen lol.

Merry Christmas.


----------



## Couriant (Mar 26, 2002)

LOL @ you guys

I have taken what you have done for me so thank you. I will let you know what I get in my project on Thursday! 

BTW that's -3oC = 15-20oF


----------



## Mosaic1 (Aug 17, 2001)

haplo210, 
LOL welcome to scotland... That's hilarious. Good luck keeping warm.



Tidus4Yuna,

Good luck. Oh no! Fatal Flaw in logic. It only works on alternate Tuesdays between 2 and 4 pm. I'll be interested to see how you do. 


Mo


----------

