Weekly Egg Count Across Coops — Nested Loop Worksheet
Trace the program below by hand, one line at a time. The outer loop repeats once per coop; the inner loop repeats
once per day of that coop's week. Notice that total_eggs is never reset — it keeps accumulating across every coop,
so each coop's running total should carry straight on from the last.
The program
START -> [1. total_eggs = 0]
|
[2. coop = 1]
|
+--- loop x3 (outer, one per coop) --------------------------+
| 3. hens = hens_in(coop) |
| 4. day = 1 |
| |
| +--- loop x7 (inner, one per day) ---------------+ |
| | 5. read weather(day) [RAIN or SUN] | |
| | 6. IF weather = RAIN | |
| | -> eggs = hens x 0.6 | |
| | ELSE | |
| | -> eggs = hens x 0.9 | |
| | 7. total_eggs = total_eggs + eggs | |
| | 8. day = day + 1 | |
| +-------------------------^----------------------------------+ |
| |
| 9. print "Coop " + coop + " done" |
| 10. coop = coop + 1 |
+--------------------------------^-------------------------------------+
|
[11. print "Farm total: " + total_eggs]
|
END
Coop data
Use the hen count and week of weather below as the inputs to steps 5 through 8 for each coop.
Coop 1 — hens = 10
Carry-in total_eggs from the previous coop (0 if this is the first): _______
Day weather(day) IF/ELSE branch eggs_today total_eggs (running)
1 SUN hens x 0.9 = 10 x 0.9
2 SUN hens x 0.9 = 10 x 0.9
3 RAIN hens x 0.6 = 10 x 0.6
4 RAIN hens x 0.6 = 10 x 0.6
5 SUN hens x 0.9 = 10 x 0.9
6 RAIN hens x 0.6 = 10 x 0.6
7 SUN hens x 0.9 = 10 x 0.9
Coop 2 — hens = 8
Carry-in total_eggs from the previous coop (0 if this is the first): _______
Day weather(day) IF/ELSE branch eggs_today total_eggs (running)
1 RAIN hens x 0.6 = 8 x 0.6
2 SUN hens x 0.9 = 8 x 0.9
3 SUN hens x 0.9 = 8 x 0.9
4 RAIN hens x 0.6 = 8 x 0.6
5 SUN hens x 0.9 = 8 x 0.9
6 SUN hens x 0.9 = 8 x 0.9
7 RAIN hens x 0.6 = 8 x 0.6
Coop 3 — hens = 12
Carry-in total_eggs from the previous coop (0 if this is the first): _______
Day weather(day) IF/ELSE branch eggs_today total_eggs (running)
1 SUN hens x 0.9 = 12 x 0.9
2 RAIN hens x 0.6 = 12 x 0.6
3 SUN hens x 0.9 = 12 x 0.9
4 SUN hens x 0.9 = 12 x 0.9
5 RAIN hens x 0.6 = 12 x 0.6
6 SUN hens x 0.9 = 12 x 0.9
7 RAIN hens x 0.6 = 12 x 0.6
Your answer
Print output (fill in as steps 9 and 11 would produce it):
Coop 1 done — running total after coop 1: _______
Coop 2 done — running total after coop 2: _______
Coop 3 done — running total after coop 3: _______
Farm total (from step 11): _______
Bonus: what would change in the diagram if each coop should print its own weekly subtotal instead of one running
farm total? Which step would need a reset, and where would you add it?