Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Three teams play three games each. Points[Team, Game] holds the points of a team in a game. The nine inputs, row by row, are 3, 1, 0, then 0, 3, 1, then 1, 1, 3.
DECLARE Points : ARRAY[1:3, 1:3] OF INTEGER
DECLARE Team : INTEGER
DECLARE Game : INTEGER
DECLARE Sum : INTEGER
FOR Team ← 1 TO 3
FOR Game ← 1 TO 3
INPUT Points[Team, Game]
NEXT Game
NEXT Team
FOR Game ← 1 TO 3
Sum ← 0
FOR Team ← 1 TO 3
Sum ← Sum + Points[Team, Game]
NEXT Team
OUTPUT Sum
NEXT GameWhat are the three outputs? Are they row totals or column totals?
DECLARE Name : ARRAY[1:Rows, 1:Columns] OF TYPE has rows times columns elements. An element is Name[Row, Column]. Nested loops visit every element: the outer loop for the rows, the inner loop for the columns. A row total is set to zero inside the outer loop, before the inner one.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The table. Team 1 scored 3, 1, 0; team 2 scored 0, 3, 1; team 3 scored 1, 1, 3.
Step 2. The totals. The outer loop is over Game, so each total is a column total:
Answer: 4, 5 and 4, the column totals.
Check. The three outputs add up to 13, the total of all nine inputs. Row totals would be 4, 4 and 5, which also add up to 13, so check the order too.
Log in to join the discussion.
Loading…