Exercise 10 of 10 · Combining concepts
Tip Jar Split
What you will make
A program that splits a night's tip jar among three cafe workers, proportional to the hours each one worked.
The one new idea: Combining a struct, a helper function, and a loop to compute a proportional split with float formatting (%.1f and %.2f)
Real programs rarely use just one concept at a time — this exercise combines a struct, a function, and a loop the way a small real-world tool actually would.
Go straight to the code ↓This last exercise ties several ideas together the way a small real tool would: a Worker struct holds each person's name and hours, totalHours is a function that loops over a slice of those structs to add up a total, and main uses that total to compute each worker's fair share of a shared amount. The proportion pattern — whole * (part / total) — comes up constantly any time you split something fairly: workers[i].Hours / hours gives a fraction between 0 and 1, and multiplying the tip jar by that fraction scales it down to just that worker's slice.
Worked example
Here's the same split logic with two workers and a smaller tip jar:
package main
import "fmt"
type Worker struct {
Name string
Hours float64
}
func totalHours(workers []Worker) float64 {
total := 0.0
for i := 0; i < len(workers); i++ {
total = total + workers[i].Hours
}
return total
}
func main() {
workers := []Worker{
{"Sam", 2.0},
{"Lee", 6.0},
}
tipJar := 40.0
hours := totalHours(workers)
fmt.Println("Tip jar split:")
for i := 0; i < len(workers); i++ {
share := tipJar * (workers[i].Hours / hours)
fmt.Printf("%s (%.1f hrs): $%.2f\n", workers[i].Name, workers[i].Hours, share)
}
fmt.Printf("Total: $%.2f\n", tipJar)
}Tip jar split:
Sam (2.0 hrs): $10.00
Lee (6.0 hrs): $30.00
Total: $40.00Sam worked a quarter of the total 8 hours, so Sam gets a quarter of the $40 jar; Lee gets the rest.
Your turn
The starter program is meant to add up all workers' hours inside totalHours and then give each worker their proportional share of the tip jar — but the accumulator in totalHours overwrites instead of adding, and the share formula divides where it should multiply. Fix both marked lines.
If something goes wrong
If hours ends up equal to just the last worker's hours (rather than everyone's combined), the accumulator in totalHours is still overwriting on each pass instead of building up a sum. If the printed shares look far too large or don't add up anywhere close to the tip jar total, check the share formula in main — it should multiply the tip jar by each worker's fraction of the total hours, not divide by that fraction.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Overwriting an accumulator variable instead of adding to it.
- total = x replaces whatever total held before; building a running sum across a loop needs total = total + x (or total += x) so each pass keeps what came before.
- Dividing by a fraction when the goal is to scale down by it.
- Multiplying by (part / whole) scales a value down proportionally; dividing by that same fraction does the opposite, inflating the result instead of shrinking it.
- Mixing int and float64 values without conversion.
- Go doesn't silently convert between numeric types, so combining an int literal with a float64 variable in the same expression is a compile error unless one is explicitly converted.
- Assuming the individual shares always sum to exactly the printed total due to floating-point rounding.
- float64 arithmetic can leave tiny rounding differences, which is why this program prints the total from tipJar directly rather than by summing the rounded shares back together.
Want a blank editor instead? Open the Go playground.